forked from firebase/quickstart-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustomauth.ts
98 lines (89 loc) · 2.87 KB
/
customauth.ts
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
90
91
92
93
94
95
96
97
98
import { initializeApp } from 'firebase/app';
import {
connectAuthEmulator,
getAuth,
onAuthStateChanged,
signInWithCustomToken,
signOut,
} from 'firebase/auth';
import { firebaseConfig } from './config';
initializeApp(firebaseConfig);
const auth = getAuth();
if (window.location.hostname === 'localhost') {
connectAuthEmulator(auth, 'http://127.0.0.1:9099');
}
const tokenTextArea = document.getElementById(
'tokentext',
)! as HTMLTextAreaElement;
const signInButton = document.getElementById(
'quickstart-sign-in',
)! as HTMLButtonElement;
const signInStatus = document.getElementById(
'quickstart-sign-in-status',
)! as HTMLSpanElement;
const accountDetails = document.getElementById(
'quickstart-account-details',
)! as HTMLDivElement;
/**
* Handle the sign in button press.
*/
function toggleSignIn() {
if (auth.currentUser) {
signOut(auth);
} else {
const token = tokenTextArea.value;
if (token.length < 10) {
alert('Please enter a token in the text area');
return;
}
// Sign in with custom token generated following previous instructions.
signInWithCustomToken(auth, token).catch(function (error) {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
if (errorCode === 'auth/invalid-custom-token') {
alert('The token you provided is not valid.');
} else {
console.error(error);
}
});
}
signInButton.disabled = true;
}
/**
* initApp handles setting up UI event listeners and registering Firebase auth listeners:
* - firebase.auth().onAuthStateChanged: This listener is called when the user is signed in or
* out, and that is where we update the UI.
*/
// Listening for auth state changes.
onAuthStateChanged(auth, function (user) {
if (user) {
// User is signed in.
const displayName = user.displayName;
const email = user.email;
const emailVerified = user.emailVerified;
const photoURL = user.photoURL;
const isAnonymous = user.isAnonymous;
const uid = user.uid;
const providerData = user.providerData;
signInStatus.textContent = 'Signed in';
signInButton.textContent = 'Sign out';
accountDetails.textContent = JSON.stringify(user, null, ' ');
} else {
// User is signed out.
signInStatus.textContent = 'Signed out';
signInButton.textContent = 'Sign in';
accountDetails.textContent = 'null';
}
signInButton.disabled = false;
});
signInButton.addEventListener('click', toggleSignIn, false);
function getHashValue(key: string): string | null {
const matches = location.hash.match(new RegExp(key + '=([^&]*)'));
return matches ? matches[1] : null;
}
// If a token has been passed in the hash fragment we display it in the UI and start the sign in process.
tokenTextArea.value = getHashValue('token') || '';
if (tokenTextArea.value) {
signInWithCustomToken(auth, tokenTextArea.value);
}