Firebase admin()。auth()。getUser(uid)错误:发生内部错误'auth / internal-error' [英] Firebase admin().auth().getUser(uid) Error: An internal error has occurred 'auth/internal-error'

查看:215
本文介绍了Firebase admin()。auth()。getUser(uid)错误:发生内部错误'auth / internal-error'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在节点服务器上使用 firebase-admin



初始化管理应用程序正常工作:

  const admin = require('firebase-admin')
const serviceAccount = require('../ service-account .json')
admin.initializeApp({
凭证:admin.credential.cert(serviceAccount),
databaseURL:// firebaseio.com url
})

admin.auth()。verifyIdToken(token).then((encoded)=> {
//这工作成功!解码的令牌
console.log('已解码的',已解码的)
//现在查看用户信息
admin.auth()。getUser(decoded.uid).then((userRecord)=> {
console.log('got user记录',userRecord)
))。catch((err)=> {
// {[错误:发生内部错误]
// errorInfo:
// {code:'auth / internal-error',
// message: '发生内部错误'}}
console.error(err)
})
})


$ b $ getUser 的最后部分失败,带有



errorInfo:
{code:'auth / internal-error',
message:'发生了一个内部错误。 } b


$ b

当我试图通过其他推荐方式的证书时,我得到了这:

  const admin = require('firebase-admin')
admin.initializeApp({
凭证:admin.credential.cert({
projectId:// projectid,
clientEmail:// client email,
privateKey:// admin private key
}),
databaseURL:// firebaseio.com url
})

然后当我尝试验证并查找用户:

  admin.auth()。verifyIdToken(token).then((encoded)=> {
//这工作成功!解码的令牌
console.log('已解码',已解码)
//现在查看用户信息
admin.auth()。getUser(decoded.uid).then((userRecord )=> {
console.log('got user record',userRecord)
})。catch((err)=> {
//错误:error:0906D06C:PEM例程:PEM_read_bio:no start line
console.error(err)
})
})

最后一部分 getUser 失败,并带有


错误:错误:0906D06C:PEM例程:PEM_read_bio:无起始行


解决方案

是所使用的服务帐户没有明确的权限在下载用户项目。

原来,服务帐户本身就是所有需要解码令牌,但其他操作需要额外的权限,如 getUser(uid)



为了解决这个问题,我必须进入IAM部分在项目的权限,并添加适当的权限(我做编辑器)的服务器帐户的电子邮件地址。

步骤




  • 设置图标 - >权限

  • IAM

  • +添加用户图标

  • 输入服务帐户凭证的电子邮件地址并分配一个角色

I'm using firebase-admin on a node server

Initializing the admin app works fine:

const admin = require('firebase-admin')
const serviceAccount = require('../service-account.json')
admin.initializeApp({                                                                                             
  credential: admin.credential.cert(serviceAccount),
  databaseURL: // firebaseio.com url
})

admin.auth().verifyIdToken(token).then((decoded) => {
  // THIS WORKS SUCCESS! a decoded token
  console.log('decoded', decoded)
  // now look up the user information
  admin.auth().getUser(decoded.uid).then((userRecord) => {
    console.log('got user record', userRecord)
  }).catch((err) => {
    // { [Error: An internal error has occurred.]
    //     errorInfo: 
    //      { code: 'auth/internal-error',
    //         message: 'An internal error has occurred.' } }
    console.error(err)
  })
})

The last part to getUser fails with

{ [Error: An internal error has occurred.] errorInfo: { code: 'auth/internal-error', message: 'An internal error has occurred.' } }


When I tried to pass in the credential the other recommended way, I got this:

const admin = require('firebase-admin')
admin.initializeApp({                                                                                             
  credential: admin.credential.cert({
    projectId: //projectid,
    clientEmail: //client email,
    privateKey: //admin private key
  }),
  databaseURL: // firebaseio.com url
})

Then when I try to verify and look up a user:

admin.auth().verifyIdToken(token).then((decoded) => {
  // THIS WORKS SUCCESS! a decoded token
  console.log('decoded', decoded)
  // now look up the user information
  admin.auth().getUser(decoded.uid).then((userRecord) => {
    console.log('got user record', userRecord)
  }).catch((err) => {
    // Error: error:0906D06C:PEM routines:PEM_read_bio:no start line
    console.error(err)
  })
})

The last part to getUser fails with

Error: error:0906D06C:PEM routines:PEM_read_bio:no start line

解决方案

The problem here is that the service account being used did not have explicit permissions on the project to download users.

Turns out, the service account alone is all that is needed to decode tokens, but extra permissions are required for other operations like getUser(uid)

To fix this, I had to go into the IAM section in the permissions on the project and add the email address for the server account with the appropriate permissions (I did editor).

Steps

  • Settings Icon -> Permissions
  • IAM
  • + Add user icon
  • enter the email address of the service account credentials and assign a role

这篇关于Firebase admin()。auth()。getUser(uid)错误:发生内部错误'auth / internal-error'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆