-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAuthContext.tsx
37 lines (31 loc) · 978 Bytes
/
AuthContext.tsx
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
33
34
35
36
37
import React, { createContext, useState, useContext, ReactNode } from 'react';
interface User {
// id: string;
// name: string;
// email: string;
// photoURL: string;
uid: string,
}
interface AuthContextType {
signedIn: boolean;
setSignedIn: React.Dispatch<React.SetStateAction<boolean>>;
user: User | null;
setUser: React.Dispatch<React.SetStateAction<User | null>>;
}
const AuthContext = createContext<AuthContextType | undefined>(undefined);
export function AuthProvider({ children }: { children: ReactNode }) {
const [signedIn, setSignedIn] = useState(false);
const [user, setUser] = useState<User | null>(null);
return (
<AuthContext.Provider value={{ signedIn, setSignedIn, user, setUser }}>
{children}
</AuthContext.Provider>
);
}
export function useAuth() {
const context = useContext(AuthContext);
if (context === undefined) {
throw new Error('useAuth must be used within an AuthProvider');
}
return context;
}