代码之家  ›  专栏  ›  技术社区  ›  vascobento

使用Aurelia进行Firebase角色授权

  •  0
  • vascobento  · 技术社区  · 7 年前

    目前,为了避免未登录的用户未经许可进入任何路由,我设置了此类:

    export class AuthorizeStep {
      run(navigationInstruction, next) {
        return new Promise((resolve, reject) => {
          firebase.auth().onAuthStateChanged(user => {
            let currentRoute = navigationInstruction.config;
            let loginRequired = currentRoute.auth && currentRoute.auth === true;
    
            if (!user && loginRequired) {
              return resolve(next.cancel(new Redirect('')));
            }
    
            return resolve(next());
          });
        });
      }
    

    此处称为:

    configureRouter(config, router) {
        this.router = router;
        config.title = 'title';
    
        config.map([
          { route: ['', 'inicio'], name: 'inicio', moduleId: './modules/contacts/components/inicio', title: 'au-samples.contacts.mainPage' },
          { route: 'conta', name: 'conta', moduleId: './modules/admin/conta', title: 'accountMessages.account', auth: true},
          { route: 'contacts', name: 'contacts', moduleId: './modules/contacts/components/list', title: 'au-samples.contacts.contacts', auth: true},
          { route: ':id', name: 'contact-details', moduleId: './modules/contacts/components/details', auth: true },
          { route: ':id/edit', name: 'contact-edition', moduleId: './modules/contacts/components/edition', auth: true },
          { route: ':id/photo', name: 'contact-photo', moduleId: './modules/contacts/components/photo', auth: true }
        ]);
        config.mapUnknownRoutes('not-found');
        config.addPipelineStep('authorize', AuthorizeStep);
      }
    

    所有这些都运行得很好,但我希望有一种方法可以提取用户的数据,我有办法这样做,获取其角色(我也可以这样做),并在访问特定路由之前检查用户是否具有该角色,我想知道是否需要使用addPipelineStep执行另一个类并在配置路由器中调用它,或者,通过检查变量来启用基于角色的授权的另一种方法是什么(在本例中,如果数组包含单词)。

    提前谢谢。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Jesse Hosain Ahmed    7 年前

    假设我正确地阅读了您的问题,并且您能够正确地检索用户角色,那么您正在寻找一种基于用户可以拥有的特定角色授权访问特定路由的方法。

    使用管道步骤,您可以非常自由地找出您喜欢的任何实现。您可以使用 settings -路由上的参数如下:

    config.map([
      { 
        route: 'conta', 
        name: 'conta', 
        moduleId: './modules/admin/conta', 
        title: 'accountMessages.account', 
        auth: true,
        settings: {
          role: 'admin' /* or any naming of choice */
        }
      }
    ]);
    

    然后,在管道步骤中,可以检查此属性,并根据用户是否具有该属性来限制访问:

    run(navigationInstruction, next) {
        return new Promise((resolve, reject) => {
          firebase.auth().onAuthStateChanged(user => {
            let currentRoute = navigationInstruction.config;
            let loginRequired = currentRoute.auth && currentRoute.auth === true;
    
            if (!user && loginRequired) {
              return resolve(next.cancel(new Redirect('')));
            }
    
            // hasRole checks the user roles for the role set on the route
            if (currentRoute.settings && currentRoute.settings.role) {
               if (!user.hasRole(currentRoute.settings.role) {
                  return resolve(next.cancel(new Redirect('')));
               }
            }
    
            return resolve(next());
          });
        });
      }
    

    很明显,您可以按照自己喜欢的任何方式编写代码,这更像是一个总体想法。