MCPcopy
hub / github.com/fastapi/full-stack-fastapi-template / useAuth

Function useAuth

frontend/src/hooks/useAuth.ts:18–67  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

16}
17
18const useAuth = () => {
19 const navigate = useNavigate()
20 const queryClient = useQueryClient()
21 const { showErrorToast } = useCustomToast()
22
23 const { data: user } = useQuery<UserPublic | null, Error>({
24 queryKey: ["currentUser"],
25 queryFn: UsersService.readUserMe,
26 enabled: isLoggedIn(),
27 })
28
29 const signUpMutation = useMutation({
30 mutationFn: (data: UserRegister) =>
31 UsersService.registerUser({ requestBody: data }),
32 onSuccess: () => {
33 navigate({ to: "/login" })
34 },
35 onError: handleError.bind(showErrorToast),
36 onSettled: () => {
37 queryClient.invalidateQueries({ queryKey: ["users"] })
38 },
39 })
40
41 const login = async (data: AccessToken) => {
42 const response = await LoginService.loginAccessToken({
43 formData: data,
44 })
45 localStorage.setItem("access_token", response.access_token)
46 }
47
48 const loginMutation = useMutation({
49 mutationFn: login,
50 onSuccess: () => {
51 navigate({ to: "/" })
52 },
53 onError: handleError.bind(showErrorToast),
54 })
55
56 const logout = () => {
57 localStorage.removeItem("access_token")
58 navigate({ to: "/login" })
59 }
60
61 return {
62 signUpMutation,
63 loginMutation,
64 logout,
65 user,
66 }
67}
68
69export { isLoggedIn }
70export default useAuth

Callers 10

DeleteConfirmationFunction · 0.85
UserInformationFunction · 0.85
UserActionsMenuFunction · 0.85
UserFunction · 0.85
AppSidebarFunction · 0.85
SignUpFunction · 0.85
LoginFunction · 0.85
UsersTableContentFunction · 0.85
DashboardFunction · 0.85
UserSettingsFunction · 0.85

Calls 3

useCustomToastFunction · 0.85
isLoggedInFunction · 0.85
registerUserMethod · 0.80

Tested by

no test coverage detected