Search code examples
firebasefirebase-authenticationnestjsfirebase-admin

Get unsigned token for user from firebase auth emulator without frontend


I am writing a custom backend (nestjs) in which I want to verify if the token from firebase auth is valid and retrieve user information too.

I do not want to use the actual firebase auth so I ended up using firebase local emulator.

Now I want to test my endpoint written in nestjs using postman wherein I send the unsigned token from postman for nestjs to verify from local emulator. But I couldn't find a way to create an unsigned token without creating a UI for the same, I really do not want to spend time in creating a react application to just console.log a token. Is there any better way to do this that I might be missing ??

Thanks for the help.


Solution

  • Assuming your Authentication emulator runs on port 9099 and you have a user created, you should be able to make the following HTTP POST request to get a token. The token is in the idToken field of the response object.

    http://localhost:9099/identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=any_key_you_want

    Body (JSON):

    {
      "email": "your-user@mail.com",
      "password": "some-password"   
    }
    

    Response:

    {
        "kind": "identitytoolkit#VerifyPasswordResponse",
        "registered": true,
        "localId": "yourUserId",
        "email": "your-user@mail.com",
        "idToken": "someIdToken",
        "refreshToken": "someRefreshToken",
        "expiresIn": "3600"
    }
    

    I found this solution playing with a React application with the firebase (^9.6.2) package installed, setting connectAuthEmulator(auth, "http://localhost:9099");, and looking at the request it made when I logged in.