How to get the Firebase ID token from Google oAuth token

192

My final solution is to only send the Google-token to the backend, and use the signInWithIdp to generate a Firebase ID token. This works on any server.

    const response = await fetch('https://identitytoolkit.googleapis.com/v1/accounts:signInWithIdp?key=[firebase_web_key]', 
    { 'method': 'POST', 
    body: JSON.stringify({
      postBody: "access_token="+req.token+"&providerId=google.com",
      requestUri: "https://[firebase-project].firebaseapp.com",
      returnIdpCredential: true,
      returnSecureToken: true,
    }) })

    // contains idToken!
    const idpData = await response.json()

    res.locals.decodedIdToken = await admin.auth().verifyIdToken(idpData.idToken);
    ```
Share:
192
Riël
Author by

Riël

Google Developer Expert in Google Apps Script; Google Apps Deployment Specialist; Company owner

Updated on December 30, 2022

Comments

  • Riël
    Riël over 1 year

    I am building a Flutter app that does a auth.SignInWithCredential({'accessToken'...,'idToken'...})

    I want the application to do backend request, sending a Bearer token. On the backend I need to authenticate this user using firebase AND use the oAuth2 token for Google-services, because it is scoped.

    I have no smart idea to do this (besides sending 2 tokens in the request headers, which seems to be overkill).

    • Riël
      Riël almost 3 years
      My final solution is to only send the Google-token to the backend, and use the signInWithIdp to generate a Firebase ID token. This works on any server.