简体   繁体   English

CanActivate Guard for multiple users in angular

[英]CanActivate guard for multiple users in angular

I've got a working canActivate auth-guard like this:我有一个像这样工作的 canActivate auth-guard:

    return this.auth.user$.map(user => {
      if (user) return true;

      this.router.navigate(["/login"], {queryParams: {returnUrl: state.url}});
      return false;

    });
  }

and use it for routes like this:并将其用于如下路线:

{path: 'my-project', component: ToolComponent, canActivate: [AuthGuard]}

i want to have multiple users be able to work on the same project我想让多个用户能够在同一个项目上工作

example: User A & B on project 1 User C & B on project 2 and so on示例:项目 1 上的用户 A 和 B 项目 2 上的用户 C 和 B 依此类推

what's the best way to to this?最好的方法是什么?

ty!泰!

the "typical" is assing to each user a "role" (admin/user), but in your case I imagine you has severals projects. “典型”是为每个用户分配一个“角色”(管理员/用户),但在你的情况下,我想你有几个项目。 So your "user" should has a property with the project can access and the path of your router can add "data".因此,您的“用户”应该具有项目可以访问的属性,并且路由器的路径可以添加“数据”。 Some like有些喜欢

  { path: 'project1/ComponentOne', component: Component1,
      canActivate: [CheckGuard],data:{project:"project1"} },

Your "checkGuard can take account the "user" and also the "data"您的“checkGuard”可以考虑“用户”以及“数据”

@Injectable()
export class CheckGuard implements CanActivate {
  constructor(private router: Router,private accountService:AccountService) { }
  canActivate(activatedRouteSnapshot: ActivatedRouteSnapshot, 
     routerStateSnapshot: RouterStateSnapshot) {

    //in activatedRouteSnapshot.data you has the "data" of the router
    console.log(activatedRouteSnapshot.data)

    ...your logic here..
  } 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM