Angularfire 2使用AngularFireAuthGuard进行基于角色的内容显示

时间:2019-06-11 12:06:09

标签: angular angularfire2

是否可以使用新的Angularfire2 guards(AngularFireAuthGuard)检查用户自定义声明(https://firebase.google.com/docs/auth/admin/custom-claims)的内容显示?

我有一个类似“编辑文章”的按钮,我只想向具有“编辑者”自定义声明的用户显示。

我想要这样的东西,但是用于内容显示而不是用于路由访问控制:

// This pipe will only allow users with the editor role to access the route
// { path: 'articles/:id/edit', component: ArticleEditComponent, ...canActivate(editorOnly) }
const editorOnly = pipe(customClaims, map(claims => claims.role === "editor"));

2 个答案:

答案 0 :(得分:0)

如果您不使用它进行路由访问控制,则不应使用AuthGuard。尝试将以下内容与* ngIf或* ngSwitch结合使用:

firebase.auth().currentUser.getIdTokenResult()
  .then((idTokenResult) => {
     // Confirm the user is an Admin.
     if (!!idTokenResult.claims.admin) {
       // Show admin UI.
       showAdminUI();
     } else {
       // Show regular user UI.
       showRegularUI();
     }
  })
  .catch((error) => {
    console.log(error);
  });

答案 1 :(得分:0)

这更像是一种自动对焦方法,因此可能不是您正在寻找的方法。这将为您提供可用于决定显示哪些内容的声明。

    this.af.currentUser.then((result) => {
        result.getIdTokenResult().then((result) => { console.log(result.claims) });
    });