/
auth_handle_recover_email.js
37 lines (32 loc) · 1.3 KB
/
auth_handle_recover_email.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
// This snippet file was generated by processing the source file:
// ./auth-next/custom-email-handler.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.
// [START auth_handle_recover_email_modular]
import { checkActionCode, applyActionCode, sendPasswordResetEmail } from "firebase/auth";
function handleRecoverEmail(auth, actionCode, lang) {
// Localize the UI to the selected language as determined by the lang
// parameter.
let restoredEmail = null;
// Confirm the action code is valid.
checkActionCode(auth, actionCode).then((info) => {
// Get the restored email address.
restoredEmail = info['data']['email'];
// Revert to the old email.
return applyActionCode(auth, actionCode);
}).then(() => {
// Account email reverted to restoredEmail
// TODO: Display a confirmation message to the user.
// You might also want to give the user the option to reset their password
// in case the account was compromised:
sendPasswordResetEmail(auth, restoredEmail).then(() => {
// Password reset confirmation sent. Ask user to check their email.
}).catch((error) => {
// Error encountered while sending password reset code.
});
}).catch((error) => {
// Invalid code.
});
}
// [END auth_handle_recover_email_modular]