Dmi*_*ich 6 javascript firebase-authentication google-cloud-functions
我正在为 firebase 自定义声明而苦苦挣扎。
我已经测试了很多方法都不起作用。显然,我错过了概念本身的一些重要内容。
所以我回到了根源。来自 google 示例的此脚本应该对新创建的用户应用海关规则
exports.processSignUp = functions.auth.user().onCreate(event => {
const user = event.data; // The Firebase user.
const customClaims = {
param: true,
accessLevel: 9
};
// Set custom user claims on this newly created user.
return admin.auth().setCustomUserClaims(user.uid, customClaims)
});
Run Code Online (Sandbox Code Playgroud)
然后在客户端上,我检查结果
firebase.auth().currentUser.getIdTokenResult()
.then((idTokenResult) => {
// Confirm the user is an Admin.
console.log(idTokenResult.claims)
if (!!idTokenResult.claims.param) {
// Show admin UI.
console.log("param")
} else {
// Show regular user UI.
console.log("no param")
}
})
.catch((error) => {
console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
一切只是原始复制粘贴仍然不起作用。我已经在本地机器上进行了测试(cors 可能有问题?)并已部署
这是一个种族情况。如果函数先结束,您将获得更新的数据。
该getIdTokenResult方法确实会强制刷新,但如果自定义声明尚未准备好,则毫无意义。
您需要设置另一个数据控制结构来触发客户端上的强制刷新。例如,rtd 的实时侦听器;
root.child(`permissions/${uid}`).on..
Run Code Online (Sandbox Code Playgroud)
侦听器内部的逻辑是: if the value for that node exists and is a number greater than some threshold, then trigger the user auth refresh
在此期间,如果没有数据快照或非管理员视图(如果数据快照存在但权限级别较低),则 ui 可以反映加载状态。
在函数中,您必须在设置声明后设置节点:
..setCustomUserClaims(..).then(
ref.setValue(9)
);
Run Code Online (Sandbox Code Playgroud)
我有一个关于pastebin的更详细的例子
| 归档时间: |
|
| 查看次数: |
6442 次 |
| 最近记录: |