In firebase - How to generate an idToken on the server for testing purposes?

后端 未结 1 824
灰色年华
灰色年华 2020-12-30 00:55

I want to test a a cloud function that creates users.

In normal cases, inside the browser i generate an idToken and i send it to server via headers:

相关标签:
1条回答
  • 2020-12-30 01:19

    From Exchange custom token for an ID and refresh token, you can transform a custom token to an id token with the api. Hence, you just have to generate a custom token first from the uid, then transform it in a custom token. Here is my sample:

    const admin = require('firebase-admin');
    const config = require('config');
    const rp = require('request-promise');
    
    module.exports.getIdToken = async uid => {
      const customToken = await admin.auth().createCustomToken(uid)
      const res = await rp({
        url: `https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=${config.get('firebase.apiKey')}`,
        method: 'POST',
        body: {
          token: customToken,
          returnSecureToken: true
        },
        json: true,
      });
      return res.idToken;
    };
    
    0 讨论(0)
提交回复
热议问题