From b9728addb5cf8f2be3f07294589318019dad35de Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 13 Nov 2020 15:08:49 +0000 Subject: [PATCH] Move auth snippets from quickstart-js --- auth/anonymous.js | 19 ++++++++++++ auth/custom.js | 21 ++++++++++++++ auth/email-link-auth.js | 5 +++- auth/email.js | 64 +++++++++++++++++++++++++++++++++++++++++ auth/facebook.js | 55 +++++++++++++++++++++++++++++++++++ auth/index.js | 22 ++++++++++++-- 6 files changed, 182 insertions(+), 4 deletions(-) create mode 100644 auth/anonymous.js create mode 100644 auth/custom.js create mode 100644 auth/email.js create mode 100644 auth/facebook.js diff --git a/auth/anonymous.js b/auth/anonymous.js new file mode 100644 index 00000000..42d817dc --- /dev/null +++ b/auth/anonymous.js @@ -0,0 +1,19 @@ +// These samples are intended for Web so this import would normally be +// done in HTML however using modules here is more convenient for +// ensuring sample correctness offline. +import firebase from "firebase/app"; +import "firebase/auth"; + +function anonSignIn() { + // [START auth_anon_sign_in] + firebase.auth().signInAnonymously() + .then(() => { + // Signed in.. + }) + .catch((error) => { + var errorCode = error.code; + var errorMessage = error.message; + // ... + }) + // [END auth_anon_sign_in] +} diff --git a/auth/custom.js b/auth/custom.js new file mode 100644 index 00000000..1d1a6037 --- /dev/null +++ b/auth/custom.js @@ -0,0 +1,21 @@ +// These samples are intended for Web so this import would normally be +// done in HTML however using modules here is more convenient for +// ensuring sample correctness offline. +import firebase from "firebase/app"; +import "firebase/auth"; + +function signInCustom() { + var token = "token123"; + // [START auth_sign_in_custom] + firebase.auth().signInWithCustomToken(token) + .then((user) => { + // Signed in + // ... + }) + .catch((error) => { + var errorCode = error.code; + var errorMessage = error.message; + // ... + }) + // [END auth_sign_in_custom] +} diff --git a/auth/email-link-auth.js b/auth/email-link-auth.js index d008a0cf..ce1e22ec 100644 --- a/auth/email-link-auth.js +++ b/auth/email-link-auth.js @@ -35,9 +35,12 @@ function emailLinkSend(email, actionCodeSettings) { // Save the email locally so you don't need to ask the user for it again // if they open the link on the same device. window.localStorage.setItem('emailForSignIn', email); + // ... }) .catch((error) => { - // Some error occurred, you can inspect the code: error.code + var errorCode = error.code; + var errorMessage = error.message; + // ... }); // [END auth_email_link_send] } diff --git a/auth/email.js b/auth/email.js new file mode 100644 index 00000000..ba919819 --- /dev/null +++ b/auth/email.js @@ -0,0 +1,64 @@ +// These samples are intended for Web so this import would normally be +// done in HTML however using modules here is more convenient for +// ensuring sample correctness offline. +import firebase from "firebase/app"; +import "firebase/auth"; + +function signInWithEmailPassword() { + var email = "test@example.com"; + var password = "hunter2"; + // [START auth_signin_password] + firebase.auth().signInWithEmailAndPassword(email, password) + .then((user) => { + // Signed in + // ... + }) + .catch((error) => { + var errorCode = error.code; + var errorMessage = error.message; + }); + // [END auth_signin_password] +} + +function signUpWithEmailPasswoerd() { + var email = "test@example.com"; + var password = "hunter2"; + // [START auth_signup_password] + firebase.auth().createUserWithEmailAndPassword(email, password) + .then((user) => { + // Signed in + // ... + }) + .catch((error) => { + var errorCode = error.code; + var errorMessage = error.message; + // .. + }); + // [END auth_signup_password] +} + +function sendEmailVerification() { + // [START auth_send_email_verification] + firebase.auth().currentUser.sendEmailVerification() + .then(() => { + // Email verification sent! + // ... + }); + // [END auth_send_email_verification] +} + +function sendPasswordReset() { + const email = "sam@example.com"; + // [START auth_send_password_reset] + firebase.auth().sendPasswordResetEmail(email) + .then(() => { + // Password reset email sent! + // .. + }) + .catch((error) => { + var errorCode = error.code; + var errorMessage = error.message; + // .. + }); + // [END auth_send_password_reset] +} diff --git a/auth/facebook.js b/auth/facebook.js new file mode 100644 index 00000000..4a1fd5ef --- /dev/null +++ b/auth/facebook.js @@ -0,0 +1,55 @@ +// These samples are intended for Web so this import would normally be +// done in HTML however using modules here is more convenient for +// ensuring sample correctness offline. +import firebase from "firebase/app"; +import "firebase/auth"; + +// [START auth_facebook_callback] +function checkLoginState(response) { + if (response.authResponse) { + // User is signed-in Facebook. + var unsubscribe = firebase.auth().onAuthStateChanged((firebaseUser) => { + unsubscribe(); + // Check if we are already signed-in Firebase with the correct user. + if (!isUserEqual(response.authResponse, firebaseUser)) { + // Build Firebase credential with the Facebook auth token. + var credential = firebase.auth.FacebookAuthProvider.credential( + response.authResponse.accessToken); + + // Sign in with the credential from the Facebook user. + firebase.auth().signInWithCredential(credential).catch((error) => { + // Handle Errors here. + var errorCode = error.code; + var errorMessage = error.message; + // The email of the user's account used. + var email = error.email; + // The firebase.auth.AuthCredential type that was used. + var credential = error.credential; + // ... + }); + } else { + // User is already signed-in Firebase with the correct user. + } + }); + } else { + // User is signed-out of Facebook. + firebase.auth().signOut(); + } +} +// [END auth_facebook_callback] + +// [START auth_facebook_checksameuser] +function isUserEqual(facebookAuthResponse, firebaseUser) { + if (firebaseUser) { + var providerData = firebaseUser.providerData; + for (var i = 0; i < providerData.length; i++) { + if (providerData[i].providerId === firebase.auth.FacebookAuthProvider.PROVIDER_ID && + providerData[i].uid === facebookAuthResponse.userID) { + // We don't need to re-auth the Firebase connection. + return true; + } + } + } + return false; +} +// [END auth_facebook_checksameuser] diff --git a/auth/index.js b/auth/index.js index a1c9fb6b..d57d704f 100644 --- a/auth/index.js +++ b/auth/index.js @@ -4,7 +4,9 @@ import firebase from "firebase/app"; import "firebase/auth"; -// Docs: https://source.corp.google.com/piper///depot/google3/third_party/devsite/firebase/en/docs/auth/_includes/_get_credentials_web.html +// ========================================================================================== +// Docs: Snippets in this file are "general purpose" and are used on more than one docs page +// ========================================================================================== function makeGoogleCredential(googleUser) { // [START auth_make_google_credential] @@ -26,8 +28,6 @@ function makeEmailCredential(email, password) { // [END auth_make_email_credential] } -// Docs: https://source.corp.google.com/piper///depot/google3/third_party/devsite/firebase/en/docs/auth/_includes/_next_steps_web.html - function signOut() { // [START auth_sign_out] firebase.auth().signOut().then(() => { @@ -37,3 +37,19 @@ function signOut() { }); // [END auth_sign_out] } + +function authStateListener() { + // [START auth_state_listener] + firebase.auth().onAuthStateChanged((user) => { + if (user) { + // User is signed in, see docs for a list of available properties + // https://firebase.google.com/docs/reference/js/firebase.User + var uid = user.uid; + // ... + } else { + // User is signed out + // ... + } + }); + // [END auth_state_listener] +}