Feature/fixe bugs

This commit is contained in:
2024-12-01 13:08:16 +00:00
committed by AmirHossein Mahmoodi
parent 5bc366a30e
commit c40fccac6b
18 changed files with 767 additions and 74 deletions

View File

@@ -1,7 +1,7 @@
"use client";
import { GET_USER_ROUTE } from "@/core/utils/routes";
import axios from "axios";
import { createContext, useCallback, useContext, useEffect, useReducer } from "react";
import { createContext, useCallback, useContext, useEffect, useReducer, useState } from "react";
const initAuth = {
initAuthState: false,
@@ -27,6 +27,7 @@ const authReducer = (state, action) => {
const AuthContext = createContext();
export const AuthProvider = ({ children }) => {
const [errorState, setErrorState] = useState(null);
const [state, dispatch] = useReducer(authReducer, initAuth);
const clearUser = useCallback(() => {
@@ -57,12 +58,15 @@ export const AuthProvider = ({ children }) => {
changeUser(data.data);
changeAuthState(true);
changeInitAuth(true);
setErrorState(false);
} catch (error) {
if (error.response && error.response.status === 401) {
clearUser();
changeAuthState(false);
changeInitAuth(true);
return;
}
setErrorState(true);
}
}, [clearUser, changeUser, changeAuthState, changeInitAuth]);
@@ -78,6 +82,7 @@ export const AuthProvider = ({ children }) => {
initAuthState: state.initAuthState,
getUser,
logout,
errorState,
}}
>
{children}