簡體   English   中英

[ERR_INVALID_ARG_TYPE]:第一個參數必須是字符串類型或緩沖區的實例。 使用 admin.auth().verifyIdToken 時

[英][ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. when using admin.auth().verifyIdToken

我正在編寫一個后端服務器(使用 Node.js)來讓用戶與 Firebase 交互。 這是我到目前為止所擁有的:

  • 我可以讓用戶通過管理員 ADK 創建他們的 firebase 帳戶(使用電子郵件和密碼);
  • 我可以讓用戶通過 REST API 登錄他們的 firebase 帳戶(使用他們已經創建的電子郵件和密碼); 並返回他們的 idToken 和 refreshToken。

但是,我真的很奇怪,每次用戶登錄時,他們都會擁有一個全新的 idToken(持續 1 小時); 那么當新的 idToken 生成時,舊的會立即過期嗎? 所以,我想我會使用 firebase-admin SDK verifyIdToken 函數來做我需要的。

問題是,即使我輸入了新的 idToken,該功能也會失敗。 我真的不知道這里發生了什么。

如果我不執行捕獲,則會出現以下錯誤:

(node:9240) UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]: The first argument must be of type string or an instance of Buffer. Received type number (1598554002)
at write_ (_http_outgoing.js:653:11)
at ServerResponse.write (_http_outgoing.js:621:15)
at D:\Viet Properties\Coding\ViMath\spreadsheet.js:315:19
at processTicksAndRejections (internal/process/task_queues.js:97:5)
at async verifyid (D:\Viet Properties\Coding\ViMath\spreadsheet.js:314:5)
at async Server.<anonymous> (D:\Viet Properties\Coding\ViMath\spreadsheet.js:584:17)
(node:9240) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag`--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:9240) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

這是我調用的函數:

async function verifyid(data, serverres) {
   serverres.write(data.id);
   await admin.auth().verifyIdToken(data.id).then(function(token) {
       serverres.write(token.exp)
   }).catch(function(error) {
       serverres.write(`Error code: ${error.code} \r\nError message: ${error.message}`);
   });
};

主要代碼在這里:

var server = http.createServer(async function (req, res) {
    var urllink = url.parse(req.url, true);
    var urldata = urllink.query;

    if (Object.entries(urldata).length !== 0){
        switch(urldata.goal){
            //I dropped some cases here for clearance of the code
            case 'verifyid':
                await verifyid(urldata, res)
                res.end();
                break;
            default:
                res.write("Error: No action has been specified!")
                res.end();
        };
    };
  });


//initialize admin
var admin = require('firebase-admin');
var serviceAccount = require("./vimath_serviceAccountKey.json");
const { parse } = require('path');
const { userInfo } = require('os');

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: 'https://[my project].firebaseio.com'
});

server.listen(8080);

大家可以看看嗎?

非常感謝您提前,

祝你有愉快的一天, 在此處輸入圖片說明 在此處輸入圖片說明

在這一行拋出錯誤:

serverres.write(token.exp)

該 API 需要一個字符串或一個緩沖區,但您傳遞的是一個數字。

暫無
暫無

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

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