-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
51 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,35 @@ | ||
import { AppDispatch, persistor } from "@/redux/store.ts"; | ||
import { authActions } from "@/redux/auth.slice.ts"; | ||
import { userActions } from "@/redux/user.slice.ts"; | ||
import {AppDispatch, persistor} from "@/redux/store.ts"; | ||
import {authActions} from "@/redux/auth.slice.ts"; | ||
import {userActions} from "@/redux/user.slice.ts"; | ||
import axiosInstance from "@/api/axiosInstance.ts"; | ||
import { toast } from "sonner"; | ||
import { useNavigate } from "@tanstack/react-router"; | ||
import { useAuth } from "@/components/auth-provider"; | ||
import { useDispatch } from "react-redux"; | ||
import {toast} from "sonner"; | ||
import {useNavigate} from "@tanstack/react-router"; | ||
import {useAuth} from "@/components/auth-provider"; | ||
import {useDispatch} from "react-redux"; | ||
import axios from "axios"; | ||
|
||
export const useLogout = () => { | ||
const dispatch: AppDispatch = useDispatch(); | ||
const auth = useAuth(); | ||
const navigate = useNavigate(); | ||
const dispatch: AppDispatch = useDispatch(); | ||
const auth = useAuth(); | ||
const navigate = useNavigate(); | ||
|
||
return async () => { | ||
try { | ||
await axiosInstance.post("/authentication/logout"); | ||
await persistor.purge(); | ||
dispatch(userActions.clearUser()); | ||
dispatch(authActions.onLogout()); | ||
auth.logout(); | ||
toast.success("Logged out successfully."); | ||
await navigate({ to: "/" }); | ||
} catch (error) { | ||
if (axios.isAxiosError(error) && error.response?.status === 409) { | ||
await navigate({ to: "/" }); | ||
return; | ||
} | ||
console.error("Logout failed:", error); | ||
toast.error("Logout failed."); | ||
await navigate({ to: "/" }); | ||
} | ||
}; | ||
return async () => { | ||
try { | ||
await axiosInstance.post("/authentication/logout"); | ||
await persistor.purge(); | ||
dispatch(userActions.clearUser()); | ||
dispatch(authActions.onLogout()); | ||
auth.logout(); | ||
toast.success("Logged out successfully."); | ||
await navigate({to: "/"}); | ||
} catch (error) { | ||
if (axios.isAxiosError(error) && error.response?.status === 409) { | ||
await navigate({to: "/"}); | ||
return; | ||
} | ||
console.error("Logout failed:", error); | ||
toast.error("Logout failed."); | ||
await navigate({to: "/"}); | ||
} | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.