首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有办法通过firebase云函数在服务器端实例化一个新的客户端?

有没有办法通过firebase云函数在服务器端实例化一个新的客户端?
EN

Stack Overflow用户
提问于 2019-09-04 14:14:56
回答 1查看 201关注 0票数 1

我正在开发一个应用程序,并试图通过使用react原生和firebase的getstream.io实现新闻馈送。有没有办法使用firebase云函数来生成用户令牌?如果有,你能给我一个指针,我怎么做呢?(云函数端和客户端的代码片段将非常有用。)我见过类似的问题,但没有找到具体的教程。如有任何帮助,我们不胜感激!

EN

回答 1

Stack Overflow用户

发布于 2019-09-06 01:15:42

对于云函数端,您需要创建一个调用createUserTokenhttps.onRequest端点,如下所示:

代码语言:javascript
运行
复制
const functions = require('firebase-functions');
const stream = require('getstream');

const client = stream.connect('YOUR_STREAM_KEY', 'YOUR_STREAM_SECRET', 'YOUR_STREAM_ID');

exports.getStreamToken = functions.https.onRequest((req, res) => {
    const token = client.createUserToken(req.body.userId);
    return { token };
});

之后,在终端中使用firebase deploy --only functions进行部署&从firebase仪表板获取函数的url。

然后你可以在POST请求中使用这个url和axios或者fetch或者其他类似的东西:

代码语言:javascript
运行
复制
const { data } = axios({
    data: {
        userId: 'lukesmetham', // Pass the user id for the user you want to generate the token for here.
    },
    method: 'POST',
    url: 'CLOUD_FUNC_URL_HERE',
});

现在,data.token将是返回的流令牌,您可以将其保存到AsyncStorage或您想要存储它的任何地方。您是否将用户数据保存在firebase/firestore或流本身中?有了更多的背景知识,我可以根据你的设置添加到上面的代码中!希望这能有所帮助!

更新:

代码语言:javascript
运行
复制
const functions = require('firebase-functions');
const stream = require('getstream');

const client = stream.connect('YOUR_STREAM_KEY', 'YOUR_STREAM_SECRET', 'YOUR_STREAM_ID');

// The onCreate listener will listen to any NEW documents created
// in the user collection and will only run when it is created for the first time.

// We then use the {userId} wildcard (you can call this whatever you like.) Which will
// be filled with the document's key at runtime through the context object below.

exports.onCreateUser = functions.firestore.document('user/{userId}').onCreate((snapshot, context) => {
    // Snapshot is the newly created user data.
    const { avatar, email, name } = snapshot.val();
    const { userId } = context.params; // this is the wildcard from the document param above.

    // you can then pass this to the createUserToken function
    // and do whatever you like with it from here
    const streamToken = client.createUserToken(userId);
});

如果需要澄清,请让我知道,这些文档对这个主题也非常有帮助

https://firebase.google.com/docs/functions/firestore-events

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57782468

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档