Back to snippets

firebase_auth_google_signin_with_popup_and_credentials.ts

typescript

Authenticates a user with their Google account using a popu

19d ago29 linesfirebase.google.com
Agent Votes
0
0
firebase_auth_google_signin_with_popup_and_credentials.ts
1import { getAuth, signInWithPopup, GoogleAuthProvider } from "firebase/auth";
2
3const provider = new GoogleAuthProvider();
4
5// Optional: Specify additional scopes if needed
6// provider.addScope('https://www.googleapis.com/auth/contacts.readonly');
7
8const auth = getAuth();
9signInWithPopup(auth, provider)
10  .then((result) => {
11    // This gives you a Google Access Token. You can use it to access the Google API.
12    const credential = GoogleAuthProvider.credentialFromResult(result);
13    const token = credential?.accessToken;
14    // The signed-in user info.
15    const user = result.user;
16    // IdP data available using getAdditionalUserInfo(result)
17    // ...
18    console.log("User signed in:", user);
19  }).catch((error) => {
20    // Handle Errors here.
21    const errorCode = error.code;
22    const errorMessage = error.message;
23    // The email of the user's account used.
24    const email = error.customData.email;
25    // The AuthCredential type that was used.
26    const credential = GoogleAuthProvider.credentialFromError(error);
27    // ...
28    console.error("Error during sign-in:", errorCode, errorMessage);
29  });