javascript - 从 Firebase 管理员向客户端发送自定义令牌
问题描述
我正在使用适用于 Nodejs 的 Firebase Admin SDK,以便我可以创建自定义令牌以在 iOS 设备中进行身份验证。在 Nodejs 文档中,它声明您在创建令牌后将其发送回客户端。
let uid = 'some-uid';
admin.auth().createCustomToken(uid)
.then(function(customToken) {
// Send token back to client
})
.catch(function(error) {
console.log('Error creating custom token:', error);
});
我的问题是最有效的方法。我一直在考虑创建可能函数以将其发送回响应正文,但我觉得我可能想多了。这是推荐的方法还是我缺少一种更简单的方法?
解决方案
这是我的应用程序(云功能)中的工作代码,就像复制粘贴一样简单,供您参考。
exports.getCustomToken = functions.https.onRequest(async (req, res) => {
return cors(req, res, async () => {
try {
const token = await createCustomToken(req.body.uid);
return res.json(token);
} catch (error) {
res.status(500).json({ message: 'Something went wrong' });
}
});
});
async function createCustomToken(userUid, role = '') {
let createdCustomToken = '';
console.log('Ceating a custom token for user uid', userUid);
await firebaseAdmin.auth().createCustomToken(userUid)
.then(function (customToken) {
// Send token back to client
console.log('customToken is ', customToken)
createdCustomToken = customToken;
})
.catch(function (error) {
console.log('Error creating custom token:', error);
});
return createdCustomToken;
}
推荐阅读
- c# - 如何将 WinForms 的 DataGridView 绑定到 SQLite 数据库(不使用设计器)?
- node.js - 使用代理的 Node.JS HTTP 请求泄漏原始 IP 地址
- netlogo - 使用两个参数过滤所有列表
- octave - 八度元素明智的比较
- r - ggplot动态改变x轴标签颜色
- asp.net-core - 发布到 IIS 后无法使用 ASP.NET Core Web API
- php - PHP SQL Concat问题
- ios - 如何在 UIColor 中获得随机的暖色/冷色?
- ruby-on-rails - 在没有身份验证的情况下将我的网站限制为每位用户一个 - 比较数组中的日期
- python - 我该如何解决这个 numba 问题 - 函数的无效使用