MCPcopy Index your code
hub / github.com/coder/coder / AuthProvider

Function AuthProvider

site/src/contexts/auth/AuthProvider.tsx:44–133  ·  view source on GitHub ↗
({ children })

Source from the content-addressed store, hash-verified

42);
43
44export const AuthProvider: FC<PropsWithChildren> = ({ children }) => {
45 const { metadata } = useEmbeddedMetadata();
46 const userMetadataState = metadata.user;
47
48 const meOptions = me(userMetadataState);
49 const userQuery = useQuery(meOptions);
50 const hasFirstUserQuery = useQuery(hasFirstUser(userMetadataState));
51
52 const permissionsQuery = useQuery({
53 ...checkAuthorization<Permissions>(
54 { checks: permissionChecks },
55 metadata.permissions,
56 ),
57 enabled: userQuery.data !== undefined,
58 });
59
60 const queryClient = useQueryClient();
61 const loginMutation = useMutation(
62 login({ checks: permissionChecks }, queryClient),
63 );
64
65 const logoutMutation = useMutation(logout(queryClient));
66 const updateProfileMutation = useMutation({
67 ...updateProfileOptions("me"),
68 onSuccess: (user) => {
69 queryClient.setQueryData(meOptions.queryKey, user);
70 },
71 });
72
73 const isSignedOut =
74 userQuery.isError &&
75 isApiError(userQuery.error) &&
76 userQuery.error.response.status === 401;
77 const isSigningOut = logoutMutation.isPending;
78 const isLoading =
79 userQuery.isLoading ||
80 hasFirstUserQuery.isLoading ||
81 (userQuery.isSuccess && permissionsQuery.isLoading);
82 const isConfiguringTheFirstUser =
83 !hasFirstUserQuery.isLoading && !hasFirstUserQuery.data;
84 const isSignedIn = userQuery.isSuccess && userQuery.data !== undefined;
85 const isSigningIn = loginMutation.isPending;
86 const isUpdatingProfile = updateProfileMutation.isPending;
87
88 const signOut = useCallback(() => {
89 logoutMutation.mutate();
90 }, [logoutMutation]);
91
92 const signIn = useCallback(
93 async (email: string, password: string) => {
94 await loginMutation.mutateAsync({ email, password });
95 },
96 [loginMutation],
97 );
98
99 const updateProfile = useCallback(
100 (req: UpdateUserProfileRequest) => {
101 const mutation = updateProfileMutation.mutateAsync(req);

Callers

nothing calls this directly

Calls 6

meFunction · 0.90
hasFirstUserFunction · 0.90
checkAuthorizationFunction · 0.90
loginFunction · 0.90
logoutFunction · 0.90
isApiErrorFunction · 0.90

Tested by

no test coverage detected