Search code examples
node.jsfirebasefirebase-authenticationgoogle-signinfirebase-admin

How to verify Google signin (via Firebase) idToken in nodejs backend?


Trying to verify idToken of a user signed in via firebase authentication (Google signin) in nodejs server. Server throws Firebase ID token has invalid signature.

Tried verifying with firebase-admin as well as jsonwebtoken with public key from the url: https://www.googleapis.com/robot/v1/metadata/x509/securetoken@system.gserviceaccount.com. Both methods work perfect for users signed in with a password, but throws 'Invalid Signature' in case of a user signed in via google.

Is there anything I am doing wrong? Do I need to verify with google-auth-library instead?

Code:

import * as admin from "firebase-admin";

admin.initializeApp({
  credential: admin.credential.cert(require("../../serviceAccount")), // file received from firebase project settings page
  databaseURL: "as mentioned in the firebase project settings page",
});

// Some code here

var token = "token received from client side";
var decoded = await admin.auth().verifyIdToken(token);

PS:

  • All client side features (after signing in) are working fine.
  • Everything else on the backend is working fine.
  • Decoding the token in both cases gives expected JSON.
  • For test run, token is being forceRefreshed everytime before calling the API.

Solution

  • OP here,

    I am dumb.

    I was using the print() function of flutter to log the token and call the API myself. Didn't know Flutter's print function has an output character limit. Login using password gives smaller tokens thus the whole token was logged. But Google sign in gives a longer token, longer than the output character limit of print.

    Solution : Use log function from 'dart:developer' package.

    import 'dart:developer';
    
    // 
    
    log(await _auth.idToken);