diff --git a/auth-next/google-signin.js b/auth-next/google-signin.js index 1e1cee59..cd245925 100644 --- a/auth-next/google-signin.js +++ b/auth-next/google-signin.js @@ -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"); diff --git a/auth/google-signin.js b/auth/google-signin.js index fcc32dc8..22341a3c 100644 --- a/auth/google-signin.js +++ b/auth/google-signin.js @@ -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); diff --git a/snippets/auth-next/google-signin/auth_google_callback.js b/snippets/auth-next/google-signin/auth_google_callback.js index 27aa8dc6..917c5021 100644 --- a/snippets/auth-next/google-signin/auth_google_callback.js +++ b/snippets/auth-next/google-signin/auth_google_callback.js @@ -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] \ No newline at end of file diff --git a/snippets/auth-next/google-signin/auth_google_checksameuser.js b/snippets/auth-next/google-signin/auth_google_checksameuser.js deleted file mode 100644 index cc451991..00000000 --- a/snippets/auth-next/google-signin/auth_google_checksameuser.js +++ /dev/null @@ -1,23 +0,0 @@ -// This snippet file was generated by processing the source file: -// ./auth-next/google-signin.js -// -// To update the snippets in this file, edit the source and then run -// 'npm run snippets'. - -// [START auth_google_checksameuser_modular] -import { GoogleAuthProvider } from "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_modular] \ No newline at end of file