-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAuth.js
67 lines (45 loc) · 1.37 KB
/
Auth.js
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { createContext, useContext, useEffect, useState } from 'react'
import React from 'react'
import {getAuth} from "firebase/auth"
import Login from './components/Login';
import { getDocs, onSnapshot } from 'firebase/firestore';
import { adminRef, pointRef } from './firebase';
import Loading from './components/Loading';
const AuthContext = createContext({});
export const AuthProvider = ({children}) => {
const [currentUser, setCurrentUser] = useState(null)
const [loading, setLoading] = useState(true)
const [admin, setAdmin] = useState(false)
let emails = [""];
let notes = [];
useEffect(() => {
const auth = getAuth();
return auth.onIdTokenChanged(async(user)=>{
if(!user){
// console.log('no user');
setCurrentUser(null);
setLoading(false);
return
}
const token = await user.getIdToken();
setCurrentUser(user);
setLoading(false);
// console.log('token', token);
// console.log('user', user);
})
}, [])
if(loading){
<Loading type="bubbles" color="blue"/>
}
else if(!currentUser){
return <Login />
}
else{
return(
<AuthContext.Provider value={{currentUser}}>
{children}
</AuthContext.Provider>
)
}
}
export const useAuth = () => useContext(AuthContext)