-
Notifications
You must be signed in to change notification settings - Fork 0
/
app-v9.js
89 lines (76 loc) · 2.47 KB
/
app-v9.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import logo from './logo.svg';
import './App.css';
import React, { useEffect } from 'react';
import {initializeApp} from 'firebase/app'
import{getAuth, signOut, onAuthStateChanged, signInWithCredential, signInAnonymously, GoogleAuthProvider,EmailAuthProvider} from'firebase/auth';
import * as firebaseui from 'firebaseui';
function App() {
let firebaseAuth;
useEffect(() => {
initializeApp({
});
firebaseAuth = getAuth();
const uiConfig = {
signInSuccessUrl: 'http://localhost:3000/',
autoUpgradeAnonymousUsers: true,
signInFlow: 'popup',
signInOptions: [
GoogleAuthProvider.PROVIDER_ID,
EmailAuthProvider.PROVIDER_ID
],
callbacks: {
signInFailure: function (error) {
// Temp variable to hold the anonymous user data if needed.
let data = null;
// Hold a reference to the anonymous current user.
let anonymousUser = firebaseAuth.currentUser;
// For merge conflicts, the error.code will be
// 'firebaseui/anonymous-upgrade-merge-conflict'.
if (error.code != 'firebaseui/anonymous-upgrade-merge-conflict') {
return Promise.resolve();
}
console.log('v9: ',"I'm here.")
// The credential the user tried to sign in with.
var cred = error.credential;
// Copy data from anonymous user to permanent user and delete anonymous
// user.
// ...
// Finish sign-in after data is copied.
return signInWithCredential(firebaseAuth, cred)
.then(anonymousUser.delete())
}
}
};
if (firebaseui.auth.AuthUI.getInstance()) {
const ui = firebaseui.auth.AuthUI.getInstance()
ui.start('#firebaseui-auth-container', uiConfig)
}
else {
const ui = new firebaseui.auth.AuthUI(firebaseAuth)
ui.start('#firebaseui-auth-container', uiConfig)
}
onAuthStateChanged(firebaseAuth, (user) => {
if (user) {
console.log(user.uid);
}
else {
console.log('User is signed out')
}
})
}, []);
const goSignInAnonymously = async (e) => {
signInAnonymously(firebaseAuth);
}
const signOutGoogle = async (e) => {
signOut(firebaseAuth);
}
return (
<div >
<div id='firebaseui-auth-container'>
</div>
<button onClick={goSignInAnonymously}>Sign-In Anonymously</button>
<button onClick={signOutGoogle}>Sign out</button>
</div>
);
}
export default App;