Firebase云端函数 - createCustomToken [英] Firebase Cloud Functions - createCustomToken

查看:435
本文介绍了Firebase云端函数 - createCustomToken的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想使用admin.auth()。createCustomToken()函数。调用这个函数会产生一个错误信息:

pre $ 错误:createCustomToken()需要一个设置了private_key的证书。 FirebaseAuthError.Error处为
(native)$ firebaseAuthError.FirebaseError处为
[构造器](/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
at新的FirebaseAuthError(/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23)FirebaseTokenGenerator.createCustomToken中的
(/ user_code / node_modules / firebase-admin / lib / auth / token-generator。 js:62:19)
at Auth.createCustomToken(/user_code/node_modules/firebase-admin/lib/auth/auth.js:89:37)
at /user_code/index.js:29: 26
at process._tickDomainCallback(internal / process / next_tick.js:129:7)

如何配置云功能使用private_key?

  admin.initializeApp(functions.config()。firebase); 


解决方案

不幸的是, createCustomToken() 方法需要使用私钥来创建自定义令牌,而默认凭据目前不提供(这恰好是 Application Default Credential )。如使用Firebase管理软件开发工具包创建自定义令牌所述,您需要提供证书凭证才能创建自定义令牌。



您可以按照将Firebase添加到您的应用。一旦获得了关键的JSON文件,就需要将其导入到Firebase的Cloud Functions中。



您可以将关键的JSON文件存储在 / functions 文件夹作为 service-account.json 。然后,在您定义函数的文件中,使用 admin.credential.cert() 来初始化Admin SDK,如下所示:

  const functions = require('firebase-functions'); 
const admin = require('firebase-admin');

var serviceAccount = require(./ service-account.json);
admin.initializeApp({
凭证:admin.credential.cert(serviceAccount),
databaseURL:https://< DATABASE_NAME> .firebaseio.com
}) ;

有关如何执行此操作的完整示例,请参阅更详细的说明和代码示例, Instagram登录示例注意,我们希望添加对 createCustomToken()缺省凭据的支持。未来,但现在,你将不得不把自己的证书,这种特定的方法工作。


Using the new Firebase Cloud Functions in combination with the admin sdk.

I want to use the admin.auth().createCustomToken() function. Calling this function results in a error message

Error: createCustomToken() requires a certificate with "private_key" set.
    at FirebaseAuthError.Error (native)
    at FirebaseAuthError.FirebaseError [as constructor] (/user_code/node_modules/firebase-admin/lib/utils/error.js:25:28)
    at new FirebaseAuthError (/user_code/node_modules/firebase-admin/lib/utils/error.js:90:23)
    at FirebaseTokenGenerator.createCustomToken (/user_code/node_modules/firebase-admin/lib/auth/token-generator.js:62:19)
    at Auth.createCustomToken (/user_code/node_modules/firebase-admin/lib/auth/auth.js:89:37)
    at /user_code/index.js:29:26
    at process._tickDomainCallback (internal/process/next_tick.js:129:7)

How do I config the cloud functions to use a private_key?

admin.initializeApp(functions.config().firebase);

解决方案

Unfortunately, the createCustomToken() method requires a private key to mint custom tokens, which is not currently available with the default credential (which happens to be an Application Default Credential). As noted in Create custom tokens using the Firebase Admin SDKs, you need to provide a certificate credential to be able to create custom tokens.

You can generate the certificate needed for this credential by following the instructions in Add Firebase to your app. Once you have the key JSON file, you need to get it into Cloud Functions for Firebase.

You can do this by storing the key JSON file in your /functions folder as service-account.json. Then, in the file where you define your Functions, use admin.credential.cert() to initialize the Admin SDK, like this:

const functions = require('firebase-functions');
const admin = require('firebase-admin');

var serviceAccount = require("./service-account.json");
admin.initializeApp({
  credential: admin.credential.cert(serviceAccount),
  databaseURL: "https://<DATABASE_NAME>.firebaseio.com"
});

For a full example of how to do this, with more detailed instructions and a code sample, check out the Instagram sign in sample.

Note that we hope to add support for createCustomToken() from the default credential in the future, but for now, you will have to bring your own credential for this particular method to work.

这篇关于Firebase云端函数 - createCustomToken的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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