CFE-5 set token socket
This commit is contained in:
@@ -2,6 +2,7 @@ import {GET_USER_ROUTE} from "@/core/data/apiRoutes";
|
||||
import axios from "axios";
|
||||
import {createContext, useCallback, useEffect, useReducer} from "react";
|
||||
import {errorRequest, errorResponse, errorSetting} from "@/core/utils/errorHandler";
|
||||
import useSocket from "@/lib/app/hooks/useSocket";
|
||||
|
||||
const initialUser = {
|
||||
isAuth: false,
|
||||
@@ -39,6 +40,7 @@ const reducer = (state, action) => {
|
||||
export const UserContext = createContext();
|
||||
|
||||
export const UserProvider = ({children}) => {
|
||||
const socket = useSocket()
|
||||
const [state, dispatch] = useReducer(reducer, initialUser);
|
||||
|
||||
const clearUser = useCallback(() => {
|
||||
@@ -75,7 +77,7 @@ export const UserProvider = ({children}) => {
|
||||
headers: {authorization: `Bearer ${state.token}`},
|
||||
})
|
||||
.then(({data}) => {
|
||||
if (typeof callback === "function") callback(data);
|
||||
if (typeof callback === "function") callback(data.data);
|
||||
})
|
||||
.catch(error => {
|
||||
if (error.response) {
|
||||
@@ -100,12 +102,16 @@ export const UserProvider = ({children}) => {
|
||||
clearUser();
|
||||
changeAuthState(false);
|
||||
changeLanguageState(false);
|
||||
|
||||
socket.auth.token = ""
|
||||
return;
|
||||
}
|
||||
getUser((data) => {
|
||||
changeUser(data);
|
||||
changeAuthState(true);
|
||||
changeLanguageState(true);
|
||||
|
||||
socket.auth.token = data.telephone_id
|
||||
});
|
||||
}, [state.token]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user