使用新的1.0 SDK使用云功能为Firebase生成自定义身份验证令牌 [英] Generating a custom auth token with a cloud function for firebase using the new 1.0 SDK
问题描述
从firebase-admin@5.11.0
和firebase-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屋!