f.k*_*sis 4 angularfire2 angular-router-guards
AngularFire 身份验证防护文档显示了允许身份验证的不同方法。
仅管理员用户:
const editorOnly = pipe(customClaims, map(claims => claims.role === "editor"));
Run Code Online (Sandbox Code Playgroud)
仅限自己:
const onlyAllowSelf = (next) => map(user => !!user && next.params.userId === user.uid);
Run Code Online (Sandbox Code Playgroud)
我的问题是如何将编辑器/管理员或用户自己可以打开组件的两者结合起来。
您可以创建一个combineAuthPipes函数,该函数forkJoin在幕后使用:
const combineAuthPipes = (authPipes: AuthPipe[]) =>
switchMap((t: Observable<firebase.User>) => forkJoin(authPipes.map(x => x(t))))
const loggedInThenRedirect = pipe(
map((t: firebase.User) => of(t)),
combineAuthPipes([
loggedIn,
customClaims as AuthPipe,
hasCustomClaim('admin'),
hasCustomClaim('editor')
]),
map(([isLoggedIn, customClaimList, admin, moderator]) => {
return {
loggedIn: isLoggedIn,
customClaims: customClaimList,
admin,
moderator
}
}),
map((t) => {
if (t.admin) {
return true
}
if (!t.loggedIn) {
return ['login']
}
return ['unauthorized']
})
)
Run Code Online (Sandbox Code Playgroud)
像这样使用:
export const routes: Routes = [
{
path: '', component: SomeComponent,
pathMatch: 'full',
canActivate: [
AngularFireAuthGuard
],
data: { authGuardPipe: () => loggedInThenRedirect }
}
]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1157 次 |
| 最近记录: |