Skip to content
Snippets Groups Projects
Commit d68e6411 authored by David Noah Donges's avatar David Noah Donges
Browse files

Enable route protection without specifying roles

parent 124e47d2
No related merge requests found
......@@ -26,8 +26,7 @@ const routes: Routes = [
{
path: 'room/:roomId',
component: RoomComponent,
canActivate: [AuthenticationGuard],
data: { roles: [UserRole.PARTICIPANT, UserRole.CREATOR] }
canActivate: [AuthenticationGuard]
},
{ path: '**', component: PageNotFoundComponent }
];
......
......@@ -18,8 +18,13 @@ export class AuthenticationGuard implements CanActivate {
canActivate(next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> {
return this.authenticationService.getUser().map(user => {
// Get roles having access to this route
// undefined if every logged in user should have access regardless of its role
const requiredRoles = next.data['roles'] as Array<UserRole>;
if (user && requiredRoles.includes(user.role)) {
// Allow access when user is logged in AND
// the route doesn't require a specific role OR
// the user's role is one of the required roles
if (user && (!requiredRoles || requiredRoles.includes(user.role))) {
return true;
}
this.notificationService.show(`You're not authorized to view this page.`);
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment