mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: resolve User is not unauthenticated error seen on logout (#10349)
* fix: do not cache getAuthenticatedUser call * use initialQuery, add back meta tag for initial load of users * lift initialUserData
This commit is contained in:
@@ -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 {
|
||||
@@ -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>;
|
||||
queryKey: ["me"],
|
||||
initialData: initialUserData,
|
||||
queryFn: API.getAuthenticatedUser,
|
||||
};
|
||||
};
|
||||
|
||||
export const hasFirstUser = () => {
|
||||
|
||||
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user