gpt4 book ai didi

node.js - Firebase 函数错误 的函数

转载 作者:太空宇宙 更新时间:2023-11-03 23:00:30 25 4
gpt4 key购买 nike

我对 Firebase 和 Node.js 还很陌生。我在我的 Cloud Functions 中创建了此函数,以使用自定义 token 登录用户:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
var serviceAccount = require("./service-account.json");
admin.initializeApp({
credential: admin.credential.cert(serviceAccount),
databaseURL: functions.config().firebase.databaseURL
});
const cors = require('cors')({origin: true});

exports.login = functions.https.onRequest((req, res) => {
cors(req, res, () => {
//doing some validation..

//get password from db and match it with input password
var userRef = admin.firestore().collection('users')

userRef.where('username', '==', username).get()
.then(snapshot => {
if(snapshot.size > 1){
res.status(200).send("Invalid account!");
return;
}
snapshot.forEach(doc => {
var userPass = doc.data().password;

//if password matches, generate token, save it in db and send it
if(userPass && password == userPass){
var uid = doc.data().uid;
var admin = Boolean(doc.data().admin);
var server = Boolean(doc.data().server);

var additionalClaims = {
admin: admin,
server: server
};

admin.auth().createCustomToken(uid, additionalClaims)
.then(function(customToken) {
res.status(200).send("token:" + customToken);
})
.catch(function(error) {
res.status(200).send("Token creation failed!");
});

//save token in db..
}else{
res.status(200).send("Invalid credentials!");
}
});
})
.catch(err => {
res.status(200).send("User authentication failed!");
});
});
});

我在文档中使用了 token 生成方法,但是每当我尝试登录用户时,它都会抛出错误:

TypeError: admin.auth is not a function
at snapshot.forEach.doc (/user_code/index.js:128:27)
at QuerySnapshot.forEach (/user_code/node_modules/firebase-admin/node_modules/@google-cloud/firestore/src/reference.js:1012:16)
at userRef.where.get.then.snapshot (/user_code/index.js:110:13)
at process._tickDomainCallback (internal/process/next_tick.js:135:7)

我做错了什么?

最佳答案

admin 的此声明:

var admin = Boolean(doc.data().admin);

正在隐藏这个:

const admin = require('firebase-admin');

使用不同的名称,例如:

var docAdmin = Boolean(doc.data().admin);

关于node.js - Firebase 函数错误 <admin.auth 不是 ..> 的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49574881/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com