|
| 1 | +// These samples are intended for Web so this import would normally be |
| 2 | +// done in HTML however using modules here is more convenient for |
| 3 | +// ensuring sample correctness offline. |
| 4 | +import firebase from "firebase/app"; |
| 5 | +import "firebase/auth"; |
| 6 | + |
| 7 | +// Docs: https://source.corp.google.com/piper///depot/google3/third_party/devsite/firebase/en/docs/auth/web/email-link-auth.md |
| 8 | + |
| 9 | +function emailLinkActionCodeSettings() { |
| 10 | + // [START auth_email_link_actioncode_settings] |
| 11 | + var actionCodeSettings = { |
| 12 | + // URL you want to redirect back to. The domain (www.example.com) for this |
| 13 | + // URL must be in the authorized domains list in the Firebase Console. |
| 14 | + url: 'https://www.example.com/finishSignUp?cartId=1234', |
| 15 | + // This must be true. |
| 16 | + handleCodeInApp: true, |
| 17 | + iOS: { |
| 18 | + bundleId: 'com.example.ios' |
| 19 | + }, |
| 20 | + android: { |
| 21 | + packageName: 'com.example.android', |
| 22 | + installApp: true, |
| 23 | + minimumVersion: '12' |
| 24 | + }, |
| 25 | + dynamicLinkDomain: 'example.page.link' |
| 26 | + }; |
| 27 | + // [END auth_email_link_actioncode_settings] |
| 28 | +} |
| 29 | + |
| 30 | +function emailLinkSend(email, actionCodeSettings) { |
| 31 | + // [START auth_email_link_send] |
| 32 | + firebase.auth().sendSignInLinkToEmail(email, actionCodeSettings) |
| 33 | + .then(() => { |
| 34 | + // The link was successfully sent. Inform the user. |
| 35 | + // Save the email locally so you don't need to ask the user for it again |
| 36 | + // if they open the link on the same device. |
| 37 | + window.localStorage.setItem('emailForSignIn', email); |
| 38 | + }) |
| 39 | + .catch((error) => { |
| 40 | + // Some error occurred, you can inspect the code: error.code |
| 41 | + }); |
| 42 | + // [END auth_email_link_send] |
| 43 | +} |
| 44 | + |
| 45 | +function emailLinkComplete() { |
| 46 | + // [START email_link_complete] |
| 47 | + // Confirm the link is a sign-in with email link. |
| 48 | + if (firebase.auth().isSignInWithEmailLink(window.location.href)) { |
| 49 | + // Additional state parameters can also be passed via URL. |
| 50 | + // This can be used to continue the user's intended action before triggering |
| 51 | + // the sign-in operation. |
| 52 | + // Get the email if available. This should be available if the user completes |
| 53 | + // the flow on the same device where they started it. |
| 54 | + var email = window.localStorage.getItem('emailForSignIn'); |
| 55 | + if (!email) { |
| 56 | + // User opened the link on a different device. To prevent session fixation |
| 57 | + // attacks, ask the user to provide the associated email again. For example: |
| 58 | + email = window.prompt('Please provide your email for confirmation'); |
| 59 | + } |
| 60 | + // The client SDK will parse the code from the link for you. |
| 61 | + firebase.auth().signInWithEmailLink(email, window.location.href) |
| 62 | + .then((result) => { |
| 63 | + // Clear email from storage. |
| 64 | + window.localStorage.removeItem('emailForSignIn'); |
| 65 | + // You can access the new user via result.user |
| 66 | + // Additional user info profile not available via: |
| 67 | + // result.additionalUserInfo.profile == null |
| 68 | + // You can check if the user is new or existing: |
| 69 | + // result.additionalUserInfo.isNewUser |
| 70 | + }) |
| 71 | + .catch((error) => { |
| 72 | + // Some error occurred, you can inspect the code: error.code |
| 73 | + // Common errors could be invalid email and invalid or expired OTPs. |
| 74 | + }); |
| 75 | + } |
| 76 | + // [END email_link_complete] |
| 77 | +} |
| 78 | + |
| 79 | +function emailLinkLink(email) { |
| 80 | + // [START auth_email_link_link] |
| 81 | + // Construct the email link credential from the current URL. |
| 82 | + var credential = firebase.auth.EmailAuthProvider.credentialWithLink( |
| 83 | + email, window.location.href); |
| 84 | + |
| 85 | + // Link the credential to the current user. |
| 86 | + firebase.auth().currentUser.linkWithCredential(credential) |
| 87 | + .then((usercred) => { |
| 88 | + // The provider is now successfully linked. |
| 89 | + // The phone user can now sign in with their phone number or email. |
| 90 | + }) |
| 91 | + .catch((error) => { |
| 92 | + // Some error occurred. |
| 93 | + }); |
| 94 | + // [END auth_email_link_link] |
| 95 | +} |
| 96 | + |
| 97 | +function emailLinkReauth(email) { |
| 98 | + // [START auth_email_link_reauth] |
| 99 | + // Construct the email link credential from the current URL. |
| 100 | + var credential = firebase.auth.EmailAuthProvider.credentialWithLink( |
| 101 | + email, window.location.href); |
| 102 | + |
| 103 | + // Re-authenticate the user with this credential. |
| 104 | + firebase.auth().currentUser.reauthenticateWithCredential(credential) |
| 105 | + .then((usercred) => { |
| 106 | + // The user is now successfully re-authenticated and can execute sensitive |
| 107 | + // operations. |
| 108 | + }) |
| 109 | + .catch((error) => { |
| 110 | + // Some error occurred. |
| 111 | + }); |
| 112 | + // [END auth_email_link_reauth] |
| 113 | +} |
| 114 | + |
| 115 | +function emailLinkDifferentiate() { |
| 116 | + // [START email_link_diferentiate] |
| 117 | + // After asking the user for their email. |
| 118 | + var email = window.prompt('Please provide your email'); |
| 119 | + firebase.auth().fetchSignInMethodsForEmail(email) |
| 120 | + .then((signInMethods) => { |
| 121 | + // This returns the same array as fetchProvidersForEmail but for email |
| 122 | + // provider identified by 'password' string, signInMethods would contain 2 |
| 123 | + // different strings: |
| 124 | + // 'emailLink' if the user previously signed in with an email/link |
| 125 | + // 'password' if the user has a password. |
| 126 | + // A user could have both. |
| 127 | + if (signInMethods.indexOf( |
| 128 | + firebase.auth.EmailAuthProvider.EMAIL_PASSWORD_SIGN_IN_METHOD) != -1) { |
| 129 | + // User can sign in with email/password. |
| 130 | + } |
| 131 | + if (signInMethods.indexOf( |
| 132 | + firebase.auth.EmailAuthProvider.EMAIL_LINK_SIGN_IN_METHOD) != -1) { |
| 133 | + // User can sign in with email/link. |
| 134 | + } |
| 135 | + }) |
| 136 | + .catch((error) => { |
| 137 | + // Some error occurred, you can inspect the code: error.code |
| 138 | + }); |
| 139 | + // [END email_link_diferentiate] |
| 140 | +} |
0 commit comments