Untitled

mail@pastecode.io avatar
unknown
jsx
a year ago
969 B
4
Indexable
Never
import { createContext, useContext, useEffect, useState } from "react";

export const AuthProdiverContext = createContext();
export const AuthProdiverContextDispatcher = createContext();

const AuthProdiver = ({ children }) => {
  const [userAuthData, setuserAuthData] = useState(false);

  useEffect(()=>{
    const userData = JSON.parse(localStorage.getItem('authData') || false);
    setuserAuthData(userData);
  },[]);

  useEffect(()=>{
    userAuthData && localStorage.setItem('authData', JSON.stringify(userAuthData));
  },[userAuthData]);
  
  return (
    <AuthProdiverContext.Provider value={userAuthData}>
      <AuthProdiverContextDispatcher.Provider value={setuserAuthData}>
        {children}
      </AuthProdiverContextDispatcher.Provider>
    </AuthProdiverContext.Provider>
  );
};

export default AuthProdiver;

export const useAuth = () => useContext(AuthProdiverContext);
export const useAuthAction = () => useContext(AuthProdiverContextDispatcher);