Skip to content

Commit 40b39fa

Browse files
committed
♻️ Refactor error handling in useAuth
1 parent 709ef21 commit 40b39fa

1 file changed

Lines changed: 5 additions & 12 deletions

File tree

frontend/src/hooks/useAuth.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"
22
import { useNavigate } from "@tanstack/react-router"
3-
import { useState } from "react"
43

54
import {
65
type Body_login_login_access_token as AccessToken,
7-
type ApiError,
86
LoginService,
97
type UserPublic,
108
type UserRegister,
119
UsersService,
1210
} from "@/client"
1311
import { handleError } from "@/utils"
12+
import useCustomToast from "./useCustomToast"
1413

1514
const isLoggedIn = () => {
1615
return localStorage.getItem("access_token") !== null
1716
}
1817

1918
const useAuth = () => {
20-
const [error, setError] = useState<string | null>(null)
2119
const navigate = useNavigate()
2220
const queryClient = useQueryClient()
21+
const { showErrorToast } = useCustomToast()
22+
2323
const { data: user } = useQuery<UserPublic | null, Error>({
2424
queryKey: ["currentUser"],
2525
queryFn: UsersService.readUserMe,
@@ -29,13 +29,10 @@ const useAuth = () => {
2929
const signUpMutation = useMutation({
3030
mutationFn: (data: UserRegister) =>
3131
UsersService.registerUser({ requestBody: data }),
32-
3332
onSuccess: () => {
3433
navigate({ to: "/login" })
3534
},
36-
onError: (err: ApiError) => {
37-
handleError(err)
38-
},
35+
onError: handleError.bind(showErrorToast),
3936
onSettled: () => {
4037
queryClient.invalidateQueries({ queryKey: ["users"] })
4138
},
@@ -53,9 +50,7 @@ const useAuth = () => {
5350
onSuccess: () => {
5451
navigate({ to: "/" })
5552
},
56-
onError: (err: ApiError) => {
57-
handleError(err)
58-
},
53+
onError: handleError.bind(showErrorToast),
5954
})
6055

6156
const logout = () => {
@@ -68,8 +63,6 @@ const useAuth = () => {
6863
loginMutation,
6964
logout,
7065
user,
71-
error,
72-
resetError: () => setError(null),
7366
}
7467
}
7568

0 commit comments

Comments
 (0)