1
1
import React , { useState } from "react" ;
2
2
import { auth } from "../database/firebase-config" ;
3
+ import { useHistory } from "react-router-dom" ;
3
4
// import { httpGetSettings } from "../hooks/request"
4
5
// import useHttp from "../hooks/useHttp"
5
- import { createUserWithEmailAndPassword , signInWithEmailAndPassword , onAuthStateChanged , signOut } from "firebase/auth" ;
6
+ import { createUserWithEmailAndPassword , onAuthStateChanged , signOut , GoogleAuthProvider , signInWithPopup , signInWithEmailAndPassword } from "firebase/auth" ;
6
7
//describes how the context looks like
8
+
9
+ const provider = new GoogleAuthProvider ( ) ;
10
+
11
+ const ADMINS = [ "Nwzxrf32Uee9i6hbTXSN2mWVzlC2" , "lHxJifUfgHhJkECibwAudvf3MGp1" , "lru8dL4JVWTycq0LHhHgyaWqX133" ] ;
12
+
7
13
const AuthContext = React . createContext ( {
8
14
name : "" ,
9
15
email : "" ,
10
16
isLoggedIn : false ,
17
+ isAdmin : false ,
18
+ userId : "" ,
19
+ user : null ,
11
20
onLogin : ( { email, password } ) => { } ,
12
21
onLogout : ( { email, password } ) => { } ,
13
22
onRegister : ( { email, password } ) => { } ,
14
23
} ) ;
15
24
16
25
export const AuthContextProvider = ( props ) => {
17
26
const [ user , setUser ] = useState ( null ) ;
27
+ const history = useHistory ( ) ;
18
28
19
29
const handleLogout = async ( ) => {
20
30
try {
@@ -33,6 +43,35 @@ export const AuthContextProvider = (props) => {
33
43
}
34
44
} ;
35
45
46
+ const handleCustomerLogin = async ( ) => {
47
+ signInWithPopup ( auth , provider )
48
+ . then ( ( result ) => {
49
+ // This gives you a Google Access Token. You can use it to access the Google API.
50
+ const credential = GoogleAuthProvider . credentialFromResult ( result ) ;
51
+ const token = credential . accessToken ;
52
+ // The signed-in user info.
53
+ const user = result . user ;
54
+ //alert("user logged in" + JSON.stringify(user));
55
+ setUser ( user ) ;
56
+ // IdP data available using getAdditionalUserInfo(result)
57
+ // ...
58
+ //TODO: handle customer user login
59
+ history . push ( "/new-booking" ) ;
60
+ } )
61
+ . catch ( ( error ) => {
62
+ // Handle Errors here.
63
+ const errorCode = error . code ;
64
+ const errorMessage = error . message ;
65
+ // The email of the user's account used.
66
+ const email = error . customData . email ;
67
+ // The AuthCredential type that was used.
68
+ const credential = GoogleAuthProvider . credentialFromError ( error ) ;
69
+ // ...
70
+ alert ( "error when logging in" + JSON . stringify ( error ) ) ;
71
+ setUser ( null ) ;
72
+ } ) ;
73
+ } ;
74
+
36
75
const handleRegister = async ( { email, password } ) => {
37
76
try {
38
77
const user = await createUserWithEmailAndPassword ( auth , email , password ) ;
@@ -43,21 +82,20 @@ export const AuthContextProvider = (props) => {
43
82
} ;
44
83
45
84
onAuthStateChanged ( auth , ( currentUser ) => {
46
- //TODO: admins should be put in firebase table not here
47
- const ADMINS = [ "Nwzxrf32Uee9i6hbTXSN2mWVzlC2" , "lHxJifUfgHhJkECibwAudvf3MGp1" , "lru8dL4JVWTycq0LHhHgyaWqX133" ] ;
48
- if ( currentUser && ADMINS . includes ( currentUser . uid ) ) {
49
- setUser ( currentUser ) ;
50
- } else {
51
- setUser ( null ) ;
52
- }
85
+ console . log ( "user" , currentUser ) ;
86
+ setUser ( currentUser ) ;
53
87
} ) ;
54
88
const contextValue = {
55
89
name : user ?. displayname ,
56
90
email : user ?. email ,
91
+ userId : user ?. uid ,
92
+ isAdmin : user && ADMINS . includes ( user ?. uid ) ,
57
93
isLoggedIn : user ? true : false ,
94
+ user : user ,
58
95
onLogin : handleLogin ,
59
96
onLogout : handleLogout ,
60
97
onRegister : handleRegister ,
98
+ handleCustomerLogin,
61
99
} ;
62
100
63
101
return < AuthContext . Provider value = { contextValue } > { props . children } </ AuthContext . Provider > ;
0 commit comments