簡體   English   中英

在 firebase - 如何在服務器上生成 idToken 用於測試目的?

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

我想測試一個創建用戶的雲功能。

在正常情況下,在瀏覽器中我生成一個idToken並通過標頭將其發送到服務器: Authorization : Bearer etcIdToken

但是我想在沒有瀏覽器的情況下測試這個功能。 在我的摩卡測試中,我有:

before(done => {
   firebase = require firebase.. -- this is suppose to be like the browser lib.
   admin = require admin.. 

    idToken = null;
    uid = "AY8HrgYIeuQswolbLl53pjdJw8b2";
    admin.auth()
        .createCustomToken(uid)               -- admin creates a customToken
        .then(customToken => {
            return firebase.auth()            -- this is like browser code. customToken get's passed to the browser.
                .signInWithCustomToken(customToken)     -- browser signs in.
                .then(signedInUser => firebase.auth()             -- now i want to get an idToken. But this gives me an error.
                    .currentUser.getIdToken())
        })
        .then(idToken_ => {
            idToken = idToken_
            done();
        })
        .catch(err => done(err));
})

我得到的錯誤是:

firebase.auth(...).currentUser.getIdToken is not a function -像這樣在客戶端上獲取 idToken - 並在此處記錄。

我直接嘗試了signedInUser.getIdToken() 同樣的問題:

signedInUser.getIdToken is not a function - 未記錄。 只是一個測試。

我認為這是因為firebase對象不適合node.js使用,就像我在這里做的那樣。 登錄時 - 東西會保存在瀏覽器本地存儲中 - 也許這就是原因。

但問題仍然存在。 如何在 node.js 中獲取 idToken 以便能夠測試:

return chai.request(myFunctions.manageUsers)
    .post("/create")
    .set("Authorization", "Bearer " + idToken)   --- i need the idToken here -  like would be if i'm getting it from the browser.
    .send({
          displayName: "jony",
          email: "jony@gmail.com",
          password: "123456"
    })

我接近這個錯誤嗎? 我知道如果我能得到idToken它將起作用。 我需要瀏覽器嗎? 謝謝 :)

用於 ID 和刷新令牌的 Exchange 自定義令牌中,您可以使用 api 將自定義令牌轉換為 id 令牌。 因此,您只需首先從 uid 生成自定義令牌,然后將其轉換為自定義令牌。 這是我的示例:

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;
};
curl 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=<FIREBASE_KEY>' -H 'Content-Type: application/json'--data-binary '{"email": "test@test.com","password":"test","returnSecureToken":true}'

如果此 curl 沒有運行,請嘗試在 Postman 上運行相同的操作。 有用!

L. Meyer 的回答對我有用。

但是, rp npm 包已被棄用並且不再使用。 這是使用 axios 修改后的工作代碼。

const axios = require('axios').default;
const admin = require('firebase-admin');
const FIREBASE_API_KEY = 'YOUR_API_KEY_FROM_FIREBASE_CONSOLE';

const createIdTokenfromCustomToken = async uid => {
  try {
    const customToken = await admin.auth().createCustomToken(uid);

    const res = await axios({
      url: `https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyCustomToken?key=${FIREBASE_API_KEY}`,
      method: 'post',
      data: {
        token: customToken,
        returnSecureToken: true
      },
      json: true,
    });

    return res.data.idToken;

  } catch (e) {
    console.log(e);
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM