Feature/fixe bugs
This commit is contained in:
@@ -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}
|
||||
|
||||
Reference in New Issue
Block a user