我正在开发一个应用程序,并试图通过使用react原生和firebase的getstream.io实现新闻馈送。有没有办法使用firebase云函数来生成用户令牌?如果有,你能给我一个指针,我怎么做呢?(云函数端和客户端的代码片段将非常有用。)我见过类似的问题,但没有找到具体的教程。如有任何帮助,我们不胜感激!
发布于 2019-09-06 01:15:42
对于云函数端,您需要创建一个调用createUserToken
的https.onRequest
端点,如下所示:
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或者其他类似的东西:
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或流本身中?有了更多的背景知识,我可以根据你的设置添加到上面的代码中!希望这能有所帮助!
更新:
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://stackoverflow.com/questions/57782468
复制相似问题