()
| 16 | } |
| 17 | |
| 18 | const 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 | |
| 69 | export { isLoggedIn } |
| 70 | export default useAuth |
no test coverage detected