Home > Back-end >  Is it possible to retrieve Firebase ID Token from the CLI?
Is it possible to retrieve Firebase ID Token from the CLI?

Time:10-29

I have a Firebase function that is triggered onRequest(), using the firebase-admin SDK and using Express as middleware. This function is not expected to be tied to a Firebase client/frontend (meaning, it is expected that this function can be called via curl -X POST ... etc.).

I want to secure this function such that only users with access to the Firebase project can make requests. I've found Firebase ID Tokens, and see how to create them on the client side.

My issue is that I don't have a client to create them from.

Is there a way to create an ID Token for an authorized user using the CLI, or another similar method?

E.g., something like

firebase login
firebase use <project-id>
firebase generate-id-token // would return an ID token with standard expiration rules.

CodePudding user response:

There isn't a command in Firebase CLI to login as a user and print ID token. You can call Firebase Auth REST API using cURL and get the token.

curl --request POST \
  --url 'https://identitytoolkit.googleapis.com/v1/accounts:signInWithPassword?key=[FIREBASE_API_KEY]' \
  --header 'Content-Type: application/json' \
  --data '{
  "email": "[email protected]",
  "password": "12345678",
  "returnSecureToken": true
}'

I want to secure this function such that only users with access to the Firebase project can make requests.

Anyone can use the REST API but they must know the account's password to login.

  • Related