aboutsummaryrefslogblamecommitdiffstats
path: root/panel/src/contexts/AuthContext.jsx
blob: f4f9383b7defebd876f67a68e147620d3e2177d6 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12











                                                                              
                                               



                                                            
                        








                       




                                        
  
import React, { createContext, useContext, useEffect, useState } from 'react';
import { auth } from '../firebase';
import { onAuthStateChanged } from 'firebase/auth';

const AuthContext = createContext();

export const useAuth = () => {
  return useContext(AuthContext);
};

export const AuthProvider = ({ children }) => {
  const [currentUser, setCurrentUser] = useState(null);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    const unsubscribe = onAuthStateChanged(auth, (user) => {
      setCurrentUser(user);
      setLoading(false);
    });

    return unsubscribe;
  }, []);

  const value = {
    currentUser,
  };

  return (
    <AuthContext.Provider value={value}>
      {!loading && children}
    </AuthContext.Provider>
  );
};