简体   繁体   English

如何在 express.js Vue.js 中使用 firebase-admin

[英]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.

相关问题 将express.js集成到现有的vue.js / webpack项目 - Integrating express.js to an Existing vue.js/webpack Project 使用Vue.js和express.js交付HTML文件 - Deliver Html file using Vue.js and express.js 如何使用 Node.js 和“firebase-admin”SDK 查询 Firebase? - How to query Firebase with Node.js and "firebase-admin" SDK? 当我使用 firebase 模拟器和 next.js 时如何使用 firebase-admin 获取 Firestore 数据 - how to get firestore data with firebase-admin when I use firebase emulators and next.js 如何在firebase-admin node.js中限制.once('value) - How to limit .once('value) in firebase-admin node.js 使用express.js的管理员帐户 - Admin account with express.js 使用 vue.js、node.js 和 express.js 上传原始文件 - Upload raw file with vue.js, node.js and express.js node.js,vue.js和express.js堆栈开发 - node.js, vue.js and express.js stack development firebase JS SDK 包是否与云函数的 Node.js 环境兼容? 为什么要使用 firebase-admin? - Is the firebase JS SDK package compatible with the Node.js environment of a cloud function? Why use firebase-admin? 如何使用 axios (Vue.js) 将我的参数 req.body 填充到我的 Express.js 服务器 - How do i fill my parameter req.body with axios (Vue.js) to my Express.js server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM