首页 > 解决方案 > 从 Firebase Cloud Functions 访问 Auth

问题描述

我无法从 Firebase Cloud Functions 环境访问用户信息。

我正在尝试做的事情。

const admin = require('firebase-admin') 
admin.initializeApp() 
exports.test = functions.https.onRequest(() => {   
    admin.auth().getUser('<user_id>').then(user => {
        return console.log(user)   
    }).catch(e => {
        console.error(e)   
    })
 })

现在,当我运行该test函数时,出现以下错误。

{ code: 'auth/insufficient-permission',
 message: 'Credential implementation provided to initializeApp() via the "credential" property has insufficient permission to access the requested resource. See https://firebase.google.com/docs/admin/setup for details on how to authenticate this SDK with appropriate permissions.' }, 
 codePrefix: 'auth' }

现在,我的问题是,是否可以admin.auth().getUser从云功能环境中使用。

标签: node.jsfirebasefirebase-authenticationgoogle-cloud-functions

解决方案


您需要将凭证文件传递给 admin sdk 初始化函数。认证相关任务需要在云功能中进行此配置。

转到 Firebase控制台 -> 设置 -> 项目设置 -> 服务帐户 -> 生成新的私钥

下载文件并包含在您的代码中。

PS:确保此文件不以任何形式向公众共享(将其添加到 git ignore),因为拥有此文件可以完全访问您的 firebase 项目。


推荐阅读