'Firebase createCustomToken() returns an invalid token in local emulators

I am using firebase authentication to handle user accounts for my web application. When a user enters valid login credentials I am using the firebase admin SDK to generate a JWT in a cloud function, which is sent back to the client and then authenticated with firebase. I am running into an issue where the JWT generated by the firebase admin SDK is invalid. More specifically, when the token is decoded (I used jwt.io), I get the following:

Header: {
  "alg": "none",
  "typ": "JWT"
}

Payload: {
  "aud": "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
  "iat": 1614367953,
  "exp": 1614371553,
  "iss": "[email protected]",
  "sub": "[email protected]",
  "uid": "some uid here"
}

The token is not only missing a signature, but the iss and sub values are wrong. I am using admin.auth().createCustomToken(uid) to generate the token and the app is being initialized with a valid service account that has Service Token Creation privileges.

EDIT: I have just figured out that the token is generated incorrectly only while everything is run locally using firebase emulators. When I deploy the app, the token is generated and validated just fine.



Solution 1:[1]

Make sure you are instrumenting your app to talk to the authentication emulator ... and doing it before the authentication listeners do their thing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ehed