From 7dfbe0f363a434cfda5f9be996d194f03c36879c Mon Sep 17 00:00:00 2001 From: Biswakalyan Bhuyan Date: Wed, 31 Jul 2024 12:16:49 +0530 Subject: new project --- panel/src/contexts/AuthContext.jsx | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 panel/src/contexts/AuthContext.jsx (limited to 'panel/src/contexts') diff --git a/panel/src/contexts/AuthContext.jsx b/panel/src/contexts/AuthContext.jsx new file mode 100644 index 0000000..cf8c7b5 --- /dev/null +++ b/panel/src/contexts/AuthContext.jsx @@ -0,0 +1,27 @@ +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); + + useEffect(() => { + const unsubscribe = onAuthStateChanged(auth, (user) => { + setCurrentUser(user); + }); + + return unsubscribe; + }, []); + + const value = { + currentUser, + }; + + return {children}; +}; -- cgit v1.2.3-59-g8ed1b