Skip to content

Commit ee789da

Browse files
authored
fix: redirect users to /login if their oauth token is invalid (#19429)
[As mentioned in the issue](#12056 (comment)) the problem here is the fact this endpoint is returning a 401 instead of a 200 in this specific case. Since we actually have enough information before performing this mutation to know that it'll fail in the case of a bad auth token we'd ideally re-work the code not to call the mutation on logout and just perform the local clean up. Unfortunately it seems like the interactions that this mutation is having with React Query at large is necessary for our code to work as intended and thus it's not currently possible to move the local clean up (the code inside of the `onSuccess`) outside of the mutation. Shout out to @Parkreiner for helping me confirm this. So until we can re-work the `AuthProvider` to be less brittle this PR changes `onSuccess` to `onSettled` so that while the mutation still fails with a 401, the local clean up still runs. Closes #12056
1 parent 02fc173 commit ee789da

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

site/src/api/queries/users.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from "hooks/useEmbeddedMetadata";
1818
import type { UsePaginatedQueryOptions } from "hooks/usePaginatedQuery";
1919
import type {
20+
MutationOptions,
2021
QueryClient,
2122
UseMutationOptions,
2223
UseQueryOptions,
@@ -192,10 +193,15 @@ const loginFn = async ({
192193
};
193194
};
194195

195-
export const logout = (queryClient: QueryClient) => {
196+
export const logout = (queryClient: QueryClient): MutationOptions => {
196197
return {
197198
mutationFn: API.logout,
198-
onSuccess: () => {
199+
// We're doing this cleanup in `onSettled` instead of `onSuccess` because in the case where an oAuth refresh token has expired this endpoint will return a 401 instead of 200.
200+
onSettled: (_, error) => {
201+
if (error) {
202+
console.error(error);
203+
}
204+
199205
/**
200206
* 2024-05-02 - If we persist any form of user data after the user logs
201207
* out, that will continue to seed the React Query cache, creating
@@ -210,6 +216,14 @@ export const logout = (queryClient: QueryClient) => {
210216
* Deleting the user data will mean that all future requests have to take
211217
* a full roundtrip, but this still felt like the best way to ensure that
212218
* manually logging out doesn't blow the entire app up.
219+
*
220+
* 2025-08-20 - Since this endpoint is for performing a post logout clean up
221+
* on the backend we should move this local clean up outside of the mutation
222+
* so that it can be explicitly performed even in cases where we don't want
223+
* run the clean up (e.g. when a user is unauthorized). Unfortunately our
224+
* auth logic is too tangled up with some obscured React Query behaviors to
225+
* be able to move right now. After `AuthProvider.tsx` is refactored this
226+
* should be moved.
213227
*/
214228
defaultMetadataManager.clearMetadataByKey("user");
215229
queryClient.removeQueries();

0 commit comments

Comments
 (0)