fixed bug auth
This commit is contained in:
@@ -1,11 +1,8 @@
|
|||||||
import WithAuthMiddleware from "@/core/middlewares/withAuth";
|
import WithAuthMiddleware from "@/core/middlewares/withAuth";
|
||||||
import { AuthProvider } from "@/lib/contexts/auth";
|
|
||||||
|
|
||||||
const Layout = ({ children }) => {
|
const Layout = ({ children }) => {
|
||||||
return (
|
return (
|
||||||
<AuthProvider>
|
<WithAuthMiddleware>{children}</WithAuthMiddleware>
|
||||||
<WithAuthMiddleware>{children}</WithAuthMiddleware>
|
|
||||||
</AuthProvider>
|
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import { AuthProvider } from "@/lib/contexts/auth";
|
||||||
import { TableSettingProvider } from "@/lib/contexts/tableSetting";
|
import { TableSettingProvider } from "@/lib/contexts/tableSetting";
|
||||||
|
|
||||||
export const metadata = {
|
export const metadata = {
|
||||||
@@ -8,7 +9,9 @@ export default function RootLayout({ children }) {
|
|||||||
return (
|
return (
|
||||||
<html lang="fa" dir={"rtl"}>
|
<html lang="fa" dir={"rtl"}>
|
||||||
<body style={{ height: "100vh", width: '100vw' }}>
|
<body style={{ height: "100vh", width: '100vw' }}>
|
||||||
<TableSettingProvider>{children}</TableSettingProvider>
|
<AuthProvider>
|
||||||
|
<TableSettingProvider>{children}</TableSettingProvider>
|
||||||
|
</AuthProvider>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -16,9 +16,10 @@ const errorServer = (response, notification, toastContainer) => {
|
|||||||
if (notification) errorServerToast(toastContainer);
|
if (notification) errorServerToast(toastContainer);
|
||||||
};
|
};
|
||||||
|
|
||||||
const errorClient = (response, notification, toastContainer) => {
|
const errorClient = (response, notification, toastContainer, logout) => {
|
||||||
switch (response.status) {
|
switch (response.status) {
|
||||||
case 401:
|
case 401:
|
||||||
|
logout()
|
||||||
if (notification) errorUnauthorizedToast(toastContainer);
|
if (notification) errorUnauthorizedToast(toastContainer);
|
||||||
break;
|
break;
|
||||||
case 422:
|
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 (notification) toast.dismiss({ container: toastContainer });
|
||||||
if (isServerError(response.status)) {
|
if (isServerError(response.status)) {
|
||||||
errorServer(response, notification, toastContainer);
|
errorServer(response, notification, toastContainer);
|
||||||
} else if (isClientError(response.status)) {
|
} else if (isClientError(response.status)) {
|
||||||
errorClient(response, notification, toastContainer);
|
errorClient(response, notification, toastContainer, logout);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
"use client";
|
"use client";
|
||||||
import { createContext, useCallback, useContext, useEffect, useReducer } from "react";
|
|
||||||
import useRequest from "../hooks/useRequest";
|
|
||||||
import { GET_USER_ROUTE } from "@/core/utils/routes";
|
import { GET_USER_ROUTE } from "@/core/utils/routes";
|
||||||
|
import axios from "axios";
|
||||||
|
import { createContext, useCallback, useContext, useEffect, useReducer } from "react";
|
||||||
|
|
||||||
const initAuth = {
|
const initAuth = {
|
||||||
initAuthState: false,
|
initAuthState: false,
|
||||||
@@ -28,7 +28,6 @@ const AuthContext = createContext();
|
|||||||
|
|
||||||
export const AuthProvider = ({ children }) => {
|
export const AuthProvider = ({ children }) => {
|
||||||
const [state, dispatch] = useReducer(authReducer, initAuth);
|
const [state, dispatch] = useReducer(authReducer, initAuth);
|
||||||
const request = useRequest();
|
|
||||||
|
|
||||||
const clearUser = useCallback(() => {
|
const clearUser = useCallback(() => {
|
||||||
dispatch({ type: "CLEAR_USER" });
|
dispatch({ type: "CLEAR_USER" });
|
||||||
@@ -46,9 +45,15 @@ export const AuthProvider = ({ children }) => {
|
|||||||
dispatch({ type: "CHANGE_INIT_AUTH", initAuthState });
|
dispatch({ type: "CHANGE_INIT_AUTH", initAuthState });
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
const logout = () => {
|
||||||
|
clearUser();
|
||||||
|
changeAuthState(false);
|
||||||
|
changeInitAuth(true);
|
||||||
|
}
|
||||||
|
|
||||||
const getUser = useCallback(async () => {
|
const getUser = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const { data } = await request(GET_USER_ROUTE, "get");
|
const { data } = await axios.get(GET_USER_ROUTE, { withCredentials: true });
|
||||||
changeUser(data);
|
changeUser(data);
|
||||||
changeAuthState(true);
|
changeAuthState(true);
|
||||||
changeInitAuth(true);
|
changeInitAuth(true);
|
||||||
@@ -72,6 +77,7 @@ export const AuthProvider = ({ children }) => {
|
|||||||
isAuth: state.isAuth,
|
isAuth: state.isAuth,
|
||||||
initAuthState: state.initAuthState,
|
initAuthState: state.initAuthState,
|
||||||
getUser,
|
getUser,
|
||||||
|
logout
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ import { errorResponse } from "@/core/utils/errorResponse";
|
|||||||
import { GET_CSRF } from "@/core/utils/routes";
|
import { GET_CSRF } from "@/core/utils/routes";
|
||||||
import { successRequest } from "@/core/utils/successRequest";
|
import { successRequest } from "@/core/utils/successRequest";
|
||||||
import axios from "axios";
|
import axios from "axios";
|
||||||
|
import { useAuth } from "../contexts/auth";
|
||||||
|
|
||||||
const getCsrfToken = async () => {
|
const getCsrfToken = async () => {
|
||||||
try {
|
try {
|
||||||
@@ -26,6 +27,7 @@ const defaultOptions = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const useRequest = (initOptions) => {
|
const useRequest = (initOptions) => {
|
||||||
|
const { logout } = useAuth()
|
||||||
const instance = axios.create();
|
const instance = axios.create();
|
||||||
instance.interceptors.request.use(
|
instance.interceptors.request.use(
|
||||||
async function (config) {
|
async function (config) {
|
||||||
@@ -60,7 +62,7 @@ const useRequest = (initOptions) => {
|
|||||||
errorResponse(
|
errorResponse(
|
||||||
error.response,
|
error.response,
|
||||||
mergedOptions.notification.show && mergedOptions.notification.failed,
|
mergedOptions.notification.show && mergedOptions.notification.failed,
|
||||||
"request_data"
|
"request_data", logout
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
|
|||||||
Reference in New Issue
Block a user