fixed bug auth

This commit is contained in:
Amirhossein Mahmoodi
2024-07-14 10:47:36 +03:30
parent 8fead6db19
commit 6e4adf477f
5 changed files with 22 additions and 13 deletions

View File

@@ -1,11 +1,8 @@
import WithAuthMiddleware from "@/core/middlewares/withAuth";
import { AuthProvider } from "@/lib/contexts/auth";
const Layout = ({ children }) => {
return (
<AuthProvider>
<WithAuthMiddleware>{children}</WithAuthMiddleware>
</AuthProvider>
<WithAuthMiddleware>{children}</WithAuthMiddleware>
);
};

View File

@@ -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 (
<html lang="fa" dir={"rtl"}>
<body style={{ height: "100vh", width: '100vw' }}>
<TableSettingProvider>{children}</TableSettingProvider>
<AuthProvider>
<TableSettingProvider>{children}</TableSettingProvider>
</AuthProvider>
</body>
</html>
);

View File

@@ -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);
}
};

View File

@@ -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}

View File

@@ -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;