aboutsummaryrefslogtreecommitdiffstats
path: root/app/components/AuthContext.tsx
diff options
context:
space:
mode:
authorAlberto Duarte (PWC) <alberto.duarte.delgado@pwc.com>2023-10-09 17:32:25 +0100
committerAlberto Duarte (PWC) <alberto.duarte.delgado@pwc.com>2023-10-09 17:32:25 +0100
commit72cf36e033ba794db7982befa45f035b62fa6cd2 (patch)
tree3a6fd91976c976afcc8bc1a7cf6fc5c1bf25a791 /app/components/AuthContext.tsx
parent66412b7788f49def33fc2143f9e1bd5d25bac261 (diff)
Changes
Diffstat (limited to 'app/components/AuthContext.tsx')
-rw-r--r--app/components/AuthContext.tsx25
1 files changed, 25 insertions, 0 deletions
diff --git a/app/components/AuthContext.tsx b/app/components/AuthContext.tsx
new file mode 100644
index 0000000..9c83744
--- /dev/null
+++ b/app/components/AuthContext.tsx
@@ -0,0 +1,25 @@
+'use client'
+import { createContext, useState, useEffect } from 'react';
+import { onAuthStateChanged } from 'firebase/auth';
+import { auth } from '../../services/firebase';
+
+export const AuthContext = createContext();
+
+export const AuthProvider = ({ children }) => {
+ const [user, setUser] = useState(null);
+
+ useEffect(() => {
+ const unsubscribe = onAuthStateChanged(auth, (user) => {
+ setUser(user);
+ });
+
+ // Cleanup the subscription when the component unmounts
+ return () => unsubscribe();
+ }, []);
+
+ return (
+ <AuthContext.Provider value={user}>
+ {children}
+ </AuthContext.Provider>
+ );
+}; \ No newline at end of file