-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathemail-password.ts
165 lines (155 loc) · 4.6 KB
/
email-password.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import { initializeApp } from 'firebase/app';
import {
connectAuthEmulator,
createUserWithEmailAndPassword,
getAuth,
onAuthStateChanged,
sendEmailVerification,
sendPasswordResetEmail,
signInWithEmailAndPassword,
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 emailInput = document.getElementById('email')! as HTMLInputElement;
const passwordInput = document.getElementById('password')! as HTMLInputElement;
const signInButton = document.getElementById(
'quickstart-sign-in',
)! as HTMLButtonElement;
const signUpButton = document.getElementById(
'quickstart-sign-up',
)! as HTMLButtonElement;
const passwordResetButton = document.getElementById(
'quickstart-password-reset',
)! as HTMLButtonElement;
const verifyEmailButton = document.getElementById(
'quickstart-verify-email',
)! as HTMLButtonElement;
const signInStatus = document.getElementById(
'quickstart-sign-in-status',
)! as HTMLSpanElement;
const accountDetails = document.getElementById(
'quickstart-account-details',
)! as HTMLDivElement;
/**
* Handles the sign in button press.
*/
function toggleSignIn() {
if (auth.currentUser) {
signOut(auth);
} else {
const email = emailInput.value;
const password = passwordInput.value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
// Sign in with email and pass.
signInWithEmailAndPassword(auth, email, password).catch(function (error) {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
if (errorCode === 'auth/wrong-password') {
alert('Wrong password.');
} else {
alert(errorMessage);
}
console.log(error);
signInButton.disabled = false;
});
}
signInButton.disabled = true;
}
/**
* Handles the sign up button press.
*/
function handleSignUp() {
const email = emailInput.value;
const password = passwordInput.value;
if (email.length < 4) {
alert('Please enter an email address.');
return;
}
if (password.length < 4) {
alert('Please enter a password.');
return;
}
// Create user with email and pass.
createUserWithEmailAndPassword(auth, email, password).catch(function (error) {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
if (errorCode == 'auth/weak-password') {
alert('The password is too weak.');
} else {
alert(errorMessage);
}
console.log(error);
});
}
/**
* Sends an email verification to the user.
*/
function sendVerificationEmailToUser() {
sendEmailVerification(auth.currentUser!).then(function () {
// Email Verification sent!
alert('Email Verification Sent!');
});
}
function sendPasswordReset() {
const email = emailInput.value;
sendPasswordResetEmail(auth, email)
.then(function () {
// Password Reset Email Sent!
alert('Password Reset Email Sent!');
})
.catch(function (error) {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
if (errorCode == 'auth/invalid-email') {
alert(errorMessage);
} else if (errorCode == 'auth/user-not-found') {
alert(errorMessage);
}
console.log(error);
});
}
// Listening for auth state changes.
onAuthStateChanged(auth, function (user) {
verifyEmailButton.disabled = true;
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, ' ');
if (!emailVerified) {
verifyEmailButton.disabled = false;
}
} else {
// User is signed out.
signInStatus.textContent = 'Signed out';
signInButton.textContent = 'Sign in';
accountDetails.textContent = 'null';
}
signInButton.disabled = false;
});
signInButton.addEventListener('click', toggleSignIn, false);
signUpButton.addEventListener('click', handleSignUp, false);
verifyEmailButton.addEventListener('click', sendVerificationEmailToUser, false);
passwordResetButton.addEventListener('click', sendPasswordReset, false);