我可以使用 firebase 登录,但是在我的应用程序中,我有 3 个级别的用户。我们称他们为管理员、用户、编辑。我保留用户在 Firebase 提供的自定义声明中的角色。
在 AuthGuard 中,我传递 data expectedRoles = ['admin', 'editor']
,添加我希望允许特定路由的角色。我想做重定向到自己的页面,例如编辑器尝试管理路由将返回到编辑器仪表板。
这是我的 AuthGuard 的canActivate
功能:
return this.auth.user.pipe(
map(user => {
if (user) {
user.getIdTokenResult().then((idTokenResult) => {
if (idTokenResult.claims.admin) {
if (expectedRoles.indexOf('admin') > -1) {
return true;
} else {
// return back to admin dashboard
this.router.navigate(['/admin']);
}
} else if (idTokenResult.claims.editor) {
if (expectedRoles.indexOf('editor') > -1) {
return true;
} else {
// return back to editor dashboard
this.router.navigate(['/editor']);
}
} else if (idTokenResult.claims.user) {
if (expectedRoles.indexOf('user') > -1) {
return true;
} else {
// return back to user dashboard
this.router.navigate(['/user']);
}
} else {
// Unexpected claim, better logout, TODO; display something
this.router.navigate(['/auth/logout']);
}
});
} else {
// User is not authenticated
// Check if we are expecting a guest
if (expectedRoles.indexOf('guest') > -1) {
return true;
} else {
this.router.navigate(['/auth/login']);
}
}
})
).first();
在添加之前user.getIdTokenResult().then(...)
它工作正常,我知道这可能是由于没有等待异步调用来解析自定义声明。我该如何解决这个问题?