[英]How to use firebase-admin in express.js Vue.js
I just want to set custom claims in the user account.我只想在用户帐户中设置自定义声明。 I'm using express.js as a backend and Vue.js as a frontend.
我使用 express.js 作为后端,使用 Vue.js 作为前端。 I initialize the app with name, but still, it's showing the error ("The default firebase app does not exist").
我用名称初始化应用程序,但它仍然显示错误(“默认 firebase 应用程序不存在”)。
This is my code:这是我的代码:
const express = require('express')
const app = express()
const bodyParser = require('body-parser')
var cors = require('cors')
app.use(bodyParser.json())
app.use(bodyParser.urlencoded({ extended: true }))
app.use(cors())
var firebaseadmin = require('firebase-admin')
var serviceAccount = require('./adminconfig.json')
firebaseadmin.initializeApp({
credential: firebaseadmin.credential.cert(serviceAccount),
},
'appname'
)
app.post('/test', async(req, res) => {
await firebaseadmin
.auth()
.setCustomUserClaims(req.body.uid, req.body.role)
.then((res) => {
console.log('then', res)
res.send(req.body)
})
.catch((error) => {
console.log('error', error)
})
res.send('hello')
})
enter code here
module.exports = { path: '/api', handler: app }
You should just call admin.initializeApp() at the global scope of your code.您应该只在代码的全局 scope 处调用 admin.initializeApp() 。
Don't pass any parameters to accept the default service account, which should have permission to read and write your Cloud Firestore database.不要传递任何参数来接受默认服务帐户,该帐户应该有权读取和写入您的 Cloud Firestore 数据库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.