[ERR_INVALID_ARG_TYPE]:第一个参数必须是字符串类型或 Buffer 的实例。使用 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 登录他们的 firebase 帐户(使用他们已经创建的电子邮件和密码)API; return 支持他们的 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 需要字符串或缓冲区,但您传递的是数字。
我正在编写一个后端服务器(使用 Node.js)让用户与 Firebase 交互。这是我目前所拥有的:
- 我可以让用户通过管理员 ADK 创建他们的 firebase 帐户(使用电子邮件和密码);
- 我可以让用户通过 REST 登录他们的 firebase 帐户(使用他们已经创建的电子邮件和密码)API; return 支持他们的 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 需要字符串或缓冲区,但您传递的是数字。