-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathREADME.txt
More file actions
85 lines (74 loc) · 2.32 KB
/
Copy pathREADME.txt
File metadata and controls
85 lines (74 loc) · 2.32 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# React Firebase Web Auth
## Install
```
npm i react-firebase-web-auth
```
## Demo
[react-firebase-web-auth-demo](https://react-firebase-web-auth-demo.web.app/)
## Demo usage
```js
import React, {useEffect, useState} from 'react';
import {getAuth, signOut} from "firebase/auth";
import firebase from "firebase/compat/app";
import StyledFirebaseAuth from "react-firebase-web-auth/StyledFirebaseAuth";
import parsePhoneNumber from "libphonenumber-js";
firebase.initializeApp({apiKey: "...", authDomain: "...", projectId: "...", ...});
const auth = getAuth();
const googleAuthProvider = new GoogleAuthProvider();
const githubAuthProvider = new GithubAuthProvider();
const phoneAuthProvider = new PhoneAuthProvider(auth);
const emailAuthProvider = new EmailAuthProvider();
const FirebaseDemo = (props) => {
const [user, setUser] = useState({});
const [isSignedIn, setIsSignedIn] = useState(false); // Local signed-in state.
// Listen to the Firebase Auth state and set the local state.
useEffect(() => {
const unregisterAuthObserver = auth.onAuthStateChanged((user) => {
setIsSignedIn(!!user);
});
return () => unregisterAuthObserver(); // Make sure we un-register Firebase observers when the component unmounts.
}, []);
if (isSignedIn) {
return (
<div>
<p>Xin chào {user?.displayName || user?.email || user?.phoneNumber || 'Unknown'}! Bạn hiện đã đăng nhập!</p>
<button onClick={() => signOut(auth)}>Đăng xuất</button>
</div>
);
}
return (
<StyledFirebaseAuth
uiConfig={{
defaultCountry: 'VN',
signInOptions: [
googleAuthProvider,
githubAuthProvider,
phoneAuthProvider,
emailAuthProvider
],
callbacks: {
signInSuccessWithAuthResult: (authResult) => {
console.log('signInSuccessWithAuthResult', authResult);
setUser(authResult.user);
},
signInFailure: (error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const {phone, email} = error.customData;
console.log('SignIn fail:', {errorCode, errorMessage, phone, email});
},
},
locale: {
parsePhoneNumber
}
}}
firebaseAuth={auth}
/>
)
}
export default FirebaseDemo;
```
## License & Credit
Publish under MIT license by [ngthuc](https://ngthuc.com)