Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
998 B
2
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);