Skip to content

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

Merged
merged 3 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 8 additions & 15 deletions site/src/api/queries/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import {
GetUsersResponse,
UpdateUserPasswordRequest,
UpdateUserProfileRequest,
User,
UsersRequest,
User,
} from "api/typesGenerated";
import { getMetadataAsJSON } from "utils/metadata";
import { getAuthorizationKey } from "./authCheck";
import { getMetadataAsJSON } from "utils/metadata";

export const users = (req: UsersRequest): UseQueryOptions<GetUsersResponse> => {
return {
Expand Down Expand Up @@ -89,21 +89,14 @@ export const authMethods = () => {
};
};

const initialMeData = getMetadataAsJSON<User>("user");
const meKey = ["me"] as const;
const initialUserData = getMetadataAsJSON<User>("user");

export const me = (queryClient: QueryClient) => {
export const me = () => {
return {
queryKey: meKey,
queryFn: async () => {
const cachedData = queryClient.getQueryData(meKey);
if (cachedData === undefined && initialMeData !== undefined) {
return initialMeData;
}

return API.getAuthenticatedUser();
},
} satisfies UseQueryOptions<User>;
Copy link
Collaborator

@BrunoQuaresma BrunoQuaresma Oct 19, 2023

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.

Copy link
Member

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

queryKey: ["me"],
initialData: initialUserData,
queryFn: API.getAuthenticatedUser,
};
};

export const hasFirstUser = () => {
Expand Down
2 changes: 1 addition & 1 deletion site/src/components/AuthProvider/AuthProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const AuthContext = createContext<AuthContextValue | undefined>(undefined);

export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
const queryClient = useQueryClient();
const meOptions = me(queryClient);
const meOptions = me();

const userQuery = useQuery(meOptions);
const authMethodsQuery = useQuery(authMethods());
Expand Down