Skip to content
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

Update Auth snippets for new Google library #257

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
67 changes: 19 additions & 48 deletions auth-next/google-signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,67 +96,38 @@ function googleBuildAndSignIn(id_token) {
}

function onSignIn_wrapper() {
// See real implementation below
function isUserEqual(x, y) {
return true;
}

// [START auth_google_callback]
const { getAuth, onAuthStateChanged, signInWithCredential, GoogleAuthProvider } = require("firebase/auth");
const auth = getAuth();

function onSignIn(googleUser) {
console.log('Google Auth Response', googleUser);
function onSignIn(googleResponse) {
console.log('Sign in with Google response', googleResponse);
// We need to register an Observer on Firebase Auth to make sure auth is initialized.
const unsubscribe = onAuthStateChanged(auth, (firebaseUser) => {
unsubscribe();
// Check if we are already signed-in Firebase with the correct user.
if (!isUserEqual(googleUser, firebaseUser)) {
// Build Firebase credential with the Google ID token.
const credential = GoogleAuthProvider.credential(
googleUser.getAuthResponse().id_token);

// Sign in with credential from the Google user.
// [START auth_google_signin_credential]
signInWithCredential(auth, credential).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The credential that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
});
// [END auth_google_signin_credential]
} else {
console.log('User already signed-in Firebase.');
}
// Build Firebase credential with the Google ID token.
const googleIdToken = googleResponse.credential;
const credential = GoogleAuthProvider.credential(googleIdToken);

// Sign in with credential from the Google user.
// [START auth_google_signin_credential]
signInWithCredential(auth, credential).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The credential that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
});
// [END auth_google_signin_credential]
});
}
// [END auth_google_callback]
}

function isUserEqual_wrapper() {
// [START auth_google_checksameuser]
const { GoogleAuthProvider } = require("firebase/auth");

function isUserEqual(googleUser, firebaseUser) {
if (firebaseUser) {
const providerData = firebaseUser.providerData;
for (let i = 0; i < providerData.length; i++) {
if (providerData[i].providerId === GoogleAuthProvider.PROVIDER_ID &&
providerData[i].uid === googleUser.getBasicProfile().getId()) {
// We don't need to reauth the Firebase connection.
return true;
}
}
}
return false;
}
// [END auth_google_checksameuser]
}

function googleProviderCredential(idToken) {
// [START auth_google_provider_credential]
const { GoogleAuthProvider } = require("firebase/auth");
Expand Down
59 changes: 19 additions & 40 deletions auth/google-signin.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,53 +96,32 @@ function googleBuildAndSignIn(id_token) {
}

// [START auth_google_callback]
function onSignIn(googleUser) {
console.log('Google Auth Response', googleUser);
function onSignIn(googleResponse) {
console.log('Sign in with Google response', googleResponse);
// We need to register an Observer on Firebase Auth to make sure auth is initialized.
var unsubscribe = firebase.auth().onAuthStateChanged((firebaseUser) => {
unsubscribe();
// Check if we are already signed-in Firebase with the correct user.
if (!isUserEqual(googleUser, firebaseUser)) {
// Build Firebase credential with the Google ID token.
var credential = firebase.auth.GoogleAuthProvider.credential(
googleUser.getAuthResponse().id_token);

// Sign in with credential from the Google user.
// [START auth_google_signin_credential]
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;
// ...
});
// [END auth_google_signin_credential]
} else {
console.log('User already signed-in Firebase.');
}
// Build Firebase credential with the Google ID token.
const googleIdToken = googleResponse.credential;
var credential = firebase.auth.GoogleAuthProvider.credential(googleIdToken);

// Sign in with credential from the Google user.
// [START auth_google_signin_credential]
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;
// ...
});
// [END auth_google_signin_credential]
});
}
// [END auth_google_callback]

// [START auth_google_checksameuser]
function isUserEqual(googleUser, firebaseUser) {
if (firebaseUser) {
var providerData = firebaseUser.providerData;
for (var i = 0; i < providerData.length; i++) {
if (providerData[i].providerId === firebase.auth.GoogleAuthProvider.PROVIDER_ID &&
providerData[i].uid === googleUser.getBasicProfile().getId()) {
// We don't need to reauth the Firebase connection.
return true;
}
}
}
return false;
}
// [END auth_google_checksameuser]

function googleProviderCredential(idToken) {
// [START auth_google_provider_credential]
var credential = firebase.auth.GoogleAuthProvider.credential(idToken);
Expand Down
37 changes: 16 additions & 21 deletions snippets/auth-next/google-signin/auth_google_callback.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,26 @@
import { getAuth, onAuthStateChanged, signInWithCredential, GoogleAuthProvider } from "firebase/auth";
const auth = getAuth();

function onSignIn(googleUser) {
console.log('Google Auth Response', googleUser);
function onSignIn(googleResponse) {
console.log('Sign in with Google response', googleResponse);
// We need to register an Observer on Firebase Auth to make sure auth is initialized.
const unsubscribe = onAuthStateChanged(auth, (firebaseUser) => {
unsubscribe();
// Check if we are already signed-in Firebase with the correct user.
if (!isUserEqual(googleUser, firebaseUser)) {
// Build Firebase credential with the Google ID token.
const credential = GoogleAuthProvider.credential(
googleUser.getAuthResponse().id_token);
// Build Firebase credential with the Google ID token.
const googleIdToken = googleResponse.credential;
const credential = GoogleAuthProvider.credential(googleIdToken);

// Sign in with credential from the Google user.
signInWithCredential(auth, credential).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The credential that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
});
} else {
console.log('User already signed-in Firebase.');
}
// Sign in with credential from the Google user.
signInWithCredential(auth, credential).catch((error) => {
// Handle Errors here.
const errorCode = error.code;
const errorMessage = error.message;
// The email of the user's account used.
const email = error.email;
// The credential that was used.
const credential = GoogleAuthProvider.credentialFromError(error);
// ...
});
});
}
// [END auth_google_callback_modular]
23 changes: 0 additions & 23 deletions snippets/auth-next/google-signin/auth_google_checksameuser.js

This file was deleted.