-
Notifications
You must be signed in to change notification settings - Fork 887
fix: resolve User is not unauthenticated error seen on logout #10349
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
site/index.html
Outdated
@@ -17,7 +17,6 @@ | |||
<meta property="og:type" content="website" /> | |||
<meta property="csrf-token" content="{{ .CSRF.Token }}" /> | |||
<meta property="build-info" content="{{ .BuildInfo }}" /> | |||
<meta property="user" content="{{ .User }}" /> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can I get a gut check here that this is safe to take out?? Seems fine but I am not very familiar with these tags and I don't want to break anything.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be taken out! It makes the page load faster. See:
Lines 302 to 307 in 21f8731
// Cookies are sent when requesting HTML, so we can get the user | |
// and pre-populate the state for the frontend to reduce requests. | |
// We use a noop response writer because we don't want to write | |
// anything to the response and break the HTML, an error means we | |
// simply don't pre-populate the state. | |
noopRW := noopResponseWriter{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kylecarbs @BrunoQuaresma Gotcha! So if I understand correctly, we return some API responses when the user requests index.html
. This is achieved via these meta
tags.
I see this pattern some places in the app:
getMetadataAsJSON<Entitlements>("entitlements") ?? API.getEntitlements()
And I'm confused because 1) wouldn't we always want fresh entitlement data? and 2) don't we already load entitlement data again in our DashboardProvider
which wraps all authenticated routes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Whenever the user loads index.html
(e.g. you navigate to dev.coder.com), that would provide fresh entitlement data. It's similar to making the request from the FE, we just do it on page load.
The DashboardProvider
re-fetching it would be a bug in that case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, but given this pattern:
async () => getMetadataAsJSON<User>("user") ?? API.getAuthenticatedUser(),
if a user doesn't refresh the page between login and logout, I think we'll see this bug because the user request is never rehydrated, right
|
||
return API.getAuthenticatedUser(); | ||
}, | ||
} satisfies UseQueryOptions<User>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm... @Parkreiner @Kira-Pilot maybe we should do something different like:
return {
queryKey: meKey,
initialData: initialMe,
queryFn: API.getAuthenticatedUser,
}
Because if the initialData
is undefined
the query will be loaded anyways.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah...This works perfectly. I double-checked the React Query type definitions, and somehow missed that initialData had an extra undefined
type tagged on to its allowable values, and that it wouldn't affect the type parameters at all. I also double-checked the documentation, and found a section on this use case
I'm going to make another PR simplifying all the other functions that work like this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If that works, that is great!
site/src/api/queries/users.ts
Outdated
}, | ||
} satisfies UseQueryOptions<User>; | ||
queryKey: ["me"], | ||
initialData: getMetadataAsJSON<User>("user"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My concern with this approach is that getMetadataAsJSON
will get recalculated every single render, even though the value is basically a constant embedded in the HTML
This should be safe to extract outside the function
Resolves the below error seen when logging out which was flagged in Slack:

I believe this issue stemmed from 0f2d4fd, which we could revert in its entiretyno seems broken before, although I'm not sure why were were caching the response from/api/v2/users/me
in the first place. For now, I've just ensured we always have fresh data when retrieving that endpoint. It appears we only call theme()
handler in ourAuthProvider
anyway.