-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAuthContext.js
More file actions
32 lines (25 loc) · 905 Bytes
/
AuthContext.js
File metadata and controls
32 lines (25 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"use client";
import { createContext, useContext, useEffect, useState } from "react";
import { onAuthStateChanged } from "firebase/auth";
import { auth } from "./firebase";
import { useSession } from "next-auth/react";
import SessionProvider from "./app/SessionProvider";
const AuthContext = createContext();
export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const { data: session, status } = useSession(); // NextAuth session
useEffect(() => {
const unsubscribe = onAuthStateChanged(auth, (firebaseUser) => {
setUser(firebaseUser);
setLoading(false);
});
return () => unsubscribe();
}, []);
return (
<AuthContext.Provider value={{ user, loading, session, status }}>
{children}
</AuthContext.Provider>
);
};
export const useAuth = () => useContext(AuthContext);