From 6e4adf477fe05549fad3098e5a023b4ec6d9d1c2 Mon Sep 17 00:00:00 2001 From: Amirhossein Mahmoodi Date: Sun, 14 Jul 2024 10:47:36 +0330 Subject: [PATCH] fixed bug auth --- src/app/(withAuth)/layout.js | 5 +---- src/app/layout.js | 5 ++++- src/core/utils/errorResponse.js | 7 ++++--- src/lib/contexts/auth.js | 14 ++++++++++---- src/lib/hooks/useRequest.js | 4 +++- 5 files changed, 22 insertions(+), 13 deletions(-) diff --git a/src/app/(withAuth)/layout.js b/src/app/(withAuth)/layout.js index 400bbc9..b9c3d01 100644 --- a/src/app/(withAuth)/layout.js +++ b/src/app/(withAuth)/layout.js @@ -1,11 +1,8 @@ import WithAuthMiddleware from "@/core/middlewares/withAuth"; -import { AuthProvider } from "@/lib/contexts/auth"; const Layout = ({ children }) => { return ( - - {children} - + {children} ); }; diff --git a/src/app/layout.js b/src/app/layout.js index 1f5a2c5..580ae26 100644 --- a/src/app/layout.js +++ b/src/app/layout.js @@ -1,3 +1,4 @@ +import { AuthProvider } from "@/lib/contexts/auth"; import { TableSettingProvider } from "@/lib/contexts/tableSetting"; export const metadata = { @@ -8,7 +9,9 @@ export default function RootLayout({ children }) { return ( - {children} + + {children} + ); diff --git a/src/core/utils/errorResponse.js b/src/core/utils/errorResponse.js index 47317bd..9eae51c 100644 --- a/src/core/utils/errorResponse.js +++ b/src/core/utils/errorResponse.js @@ -16,9 +16,10 @@ const errorServer = (response, notification, toastContainer) => { if (notification) errorServerToast(toastContainer); }; -const errorClient = (response, notification, toastContainer) => { +const errorClient = (response, notification, toastContainer, logout) => { switch (response.status) { case 401: + logout() if (notification) errorUnauthorizedToast(toastContainer); break; case 422: @@ -50,11 +51,11 @@ const errorClient = (response, notification, toastContainer) => { } }; -export const errorResponse = (response, notification, toastContainer) => { +export const errorResponse = (response, notification, toastContainer, logout) => { if (notification) toast.dismiss({ container: toastContainer }); if (isServerError(response.status)) { errorServer(response, notification, toastContainer); } else if (isClientError(response.status)) { - errorClient(response, notification, toastContainer); + errorClient(response, notification, toastContainer, logout); } }; diff --git a/src/lib/contexts/auth.js b/src/lib/contexts/auth.js index f2df0e6..074c8db 100644 --- a/src/lib/contexts/auth.js +++ b/src/lib/contexts/auth.js @@ -1,7 +1,7 @@ "use client"; -import { createContext, useCallback, useContext, useEffect, useReducer } from "react"; -import useRequest from "../hooks/useRequest"; import { GET_USER_ROUTE } from "@/core/utils/routes"; +import axios from "axios"; +import { createContext, useCallback, useContext, useEffect, useReducer } from "react"; const initAuth = { initAuthState: false, @@ -28,7 +28,6 @@ const AuthContext = createContext(); export const AuthProvider = ({ children }) => { const [state, dispatch] = useReducer(authReducer, initAuth); - const request = useRequest(); const clearUser = useCallback(() => { dispatch({ type: "CLEAR_USER" }); @@ -46,9 +45,15 @@ export const AuthProvider = ({ children }) => { dispatch({ type: "CHANGE_INIT_AUTH", initAuthState }); }, []); + const logout = () => { + clearUser(); + changeAuthState(false); + changeInitAuth(true); + } + const getUser = useCallback(async () => { try { - const { data } = await request(GET_USER_ROUTE, "get"); + const { data } = await axios.get(GET_USER_ROUTE, { withCredentials: true }); changeUser(data); changeAuthState(true); changeInitAuth(true); @@ -72,6 +77,7 @@ export const AuthProvider = ({ children }) => { isAuth: state.isAuth, initAuthState: state.initAuthState, getUser, + logout }} > {children} diff --git a/src/lib/hooks/useRequest.js b/src/lib/hooks/useRequest.js index 8805a56..477128e 100644 --- a/src/lib/hooks/useRequest.js +++ b/src/lib/hooks/useRequest.js @@ -2,6 +2,7 @@ import { errorResponse } from "@/core/utils/errorResponse"; import { GET_CSRF } from "@/core/utils/routes"; import { successRequest } from "@/core/utils/successRequest"; import axios from "axios"; +import { useAuth } from "../contexts/auth"; const getCsrfToken = async () => { try { @@ -26,6 +27,7 @@ const defaultOptions = { }; const useRequest = (initOptions) => { + const { logout } = useAuth() const instance = axios.create(); instance.interceptors.request.use( async function (config) { @@ -60,7 +62,7 @@ const useRequest = (initOptions) => { errorResponse( error.response, mergedOptions.notification.show && mergedOptions.notification.failed, - "request_data" + "request_data", logout ); } throw error;