-
Notifications
You must be signed in to change notification settings - Fork 253
/
Copy pathemail_link_complete.js
42 lines (40 loc) · 1.83 KB
/
email_link_complete.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
// This snippet file was generated by processing the source file:
// ./auth-next/email-link-auth.js
//
// To update the snippets in this file, edit the source and then run
// 'npm run snippets'.
// [START email_link_complete_modular]
import { getAuth, isSignInWithEmailLink, signInWithEmailLink } from "firebase/auth";
// Confirm the link is a sign-in with email link.
const auth = getAuth();
if (isSignInWithEmailLink(auth, window.location.href)) {
// Additional state parameters can also be passed via URL.
// This can be used to continue the user's intended action before triggering
// the sign-in operation.
// Get the email if available. This should be available if the user completes
// the flow on the same device where they started it.
let email = window.localStorage.getItem('emailForSignIn');
if (!email) {
// User opened the link on a different device. To prevent session fixation
// attacks, ask the user to provide the associated email again. For example:
email = window.prompt('Please provide your email for confirmation');
}
// The client SDK will parse the code from the link for you.
signInWithEmailLink(auth, email, window.location.href)
.then((result) => {
// Clear email from storage.
window.localStorage.removeItem('emailForSignIn');
// You can access the new user by importing getAdditionalUserInfo
// and calling it with result:
// getAdditionalUserInfo(result)
// You can access the user's profile via:
// getAdditionalUserInfo(result)?.profile
// You can check if the user is new or existing:
// getAdditionalUserInfo(result)?.isNewUser
})
.catch((error) => {
// Some error occurred, you can inspect the code: error.code
// Common errors could be invalid email and invalid or expired OTPs.
});
}
// [END email_link_complete_modular]