使用新的1.0 SDK使用云功能为Firebase生成自定义身份验证令牌 [英] Generating a custom auth token with a cloud function for firebase using the new 1.0 SDK

查看:83
本文介绍了使用新的1.0 SDK使用云功能为Firebase生成自定义身份验证令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

firebase-admin@5.11.0firebase-functions@1.0.0 firebase-admin开始,在应用初始化时不再接受应用配置.

As of firebase-admin@5.11.0 and firebase-functions@1.0.0 firebase-admin no longer takes in an application config when the app initializes.

我有一个firestore函数,该函数可以使用firebase-admin的createCustomToken生成自定义令牌.调用该函数将生成一个凭据,该凭据将传递给credential属性中的initializeApp.我现在该怎么做?

I had a firestore function that would generate a custom token using firebase-admin’s createCustomToken. Calling that function would generate a credential that I would pass into initializeApp in the credential attribute. How would I go about doing that now?

在调用initializeApp之前,我是否需要以某种方式编辑process.env.FIREBASE_CONFIG并在其中放置序列化凭据?

Do I need to edit process.env.FIREBASE_CONFIG somehow and put the serialized credential there before calling initializeApp?

推荐答案

基于Github中的此问题,它仍然有效.

Based on this issue in Github, it still works.

https://github.com/firebase/firebase-admin-node/Issues/224

以下示例对我有用:

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const serviceAccount = require('./serviceAccountKey.json');
admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: 'https://yourapplication.firebaseio.com/'
  });

exports.createToken = functions.https.onCall((data, context) => {
    const uid = context.auth.uid;
    return admin.auth()
                .createCustomToken(uid)
                .then(customToken => {
                    console.log(`The customToken is: ${customToken}`);
                    return {status: 'success', customToken: customToken};
                })
                .catch(error => {
                    console.error(`Something happened buddy: ${error}`)
                    return {status: 'error'};
                });
});

这篇关于使用新的1.0 SDK使用云功能为Firebase生成自定义身份验证令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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