Skip to content

Move some auth snippets from quickstart-js #57

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions auth/anonymous.js
Original file line number Diff line number Diff line change
@@ -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]
}
21 changes: 21 additions & 0 deletions auth/custom.js
Original file line number Diff line number Diff line change
@@ -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]
}
5 changes: 4 additions & 1 deletion auth/email-link-auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}
Expand Down
64 changes: 64 additions & 0 deletions auth/email.js
Original file line number Diff line number Diff line change
@@ -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 = "[email protected]";
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 = "[email protected]";
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 = "[email protected]";
// [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]
}
55 changes: 55 additions & 0 deletions auth/facebook.js
Original file line number Diff line number Diff line change
@@ -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]
22 changes: 19 additions & 3 deletions auth/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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(() => {
Expand All @@ -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]
}