frontend: Update routes to use runGuardsSerially when having multiple guards and add checkParticipantRoleAndAuthGuard to enforce authentication based on participant role and auth mode
This commit is contained in:
parent
23cdea5ca1
commit
cf4b2ef340
@ -1,6 +1,7 @@
|
|||||||
import { inject } from '@angular/core';
|
import { inject } from '@angular/core';
|
||||||
import { ActivatedRouteSnapshot, CanActivateFn, Router, RouterStateSnapshot } from '@angular/router';
|
import { ActivatedRouteSnapshot, CanActivateFn, Router, RouterStateSnapshot } from '@angular/router';
|
||||||
import { AuthService, ContextService } from '../services';
|
import { AuthService, ContextService } from '../services';
|
||||||
|
import { AuthMode, ParticipantRole } from '@lib/typings/ce';
|
||||||
|
|
||||||
export const checkUserAuthenticatedGuard: CanActivateFn = async (
|
export const checkUserAuthenticatedGuard: CanActivateFn = async (
|
||||||
route: ActivatedRouteSnapshot,
|
route: ActivatedRouteSnapshot,
|
||||||
@ -42,6 +43,40 @@ export const checkUserAuthenticatedGuard: CanActivateFn = async (
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const checkParticipantRoleAndAuthGuard: CanActivateFn = async (
|
||||||
|
_route: ActivatedRouteSnapshot,
|
||||||
|
state: RouterStateSnapshot
|
||||||
|
) => {
|
||||||
|
const authService = inject(AuthService);
|
||||||
|
const contextService = inject(ContextService);
|
||||||
|
const router = inject(Router);
|
||||||
|
|
||||||
|
const participantRole = contextService.getParticipantRole();
|
||||||
|
const authMode = await contextService.getAuthModeToEnterRoom();
|
||||||
|
|
||||||
|
// If the user is a moderator and the room requires authentication for moderators only,
|
||||||
|
// or if the room requires authentication for all users,
|
||||||
|
// then check if the user is authenticated
|
||||||
|
const isAuthRequiredForModerators =
|
||||||
|
authMode === AuthMode.MODERATORS_ONLY && participantRole === ParticipantRole.MODERATOR;
|
||||||
|
const isAuthRequiredForAllUsers = authMode === AuthMode.ALL_USERS;
|
||||||
|
console.log('Participant role:', participantRole);
|
||||||
|
|
||||||
|
if (isAuthRequiredForModerators || isAuthRequiredForAllUsers) {
|
||||||
|
// Check if user is authenticated
|
||||||
|
const isAuthenticated = await authService.isUserAuthenticated();
|
||||||
|
if (!isAuthenticated) {
|
||||||
|
// Redirect to the login page with query param to redirect back to the room
|
||||||
|
return router.createUrlTree(['login'], {
|
||||||
|
queryParams: { redirectTo: state.url }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Allow access to the room
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
export const checkUserNotAuthenticatedGuard: CanActivateFn = async (
|
export const checkUserNotAuthenticatedGuard: CanActivateFn = async (
|
||||||
route: ActivatedRouteSnapshot,
|
route: ActivatedRouteSnapshot,
|
||||||
_state: RouterStateSnapshot
|
_state: RouterStateSnapshot
|
||||||
|
|||||||
@ -1,5 +1,4 @@
|
|||||||
import { Routes } from '@angular/router';
|
import { Routes } from '@angular/router';
|
||||||
|
|
||||||
import { UnauthorizedComponent, RoomCreatorDisabledComponent } from '../components';
|
import { UnauthorizedComponent, RoomCreatorDisabledComponent } from '../components';
|
||||||
import {
|
import {
|
||||||
checkUserAuthenticatedGuard,
|
checkUserAuthenticatedGuard,
|
||||||
@ -9,7 +8,9 @@ import {
|
|||||||
extractQueryParamsGuard,
|
extractQueryParamsGuard,
|
||||||
checkParticipantNameGuard,
|
checkParticipantNameGuard,
|
||||||
replaceModeratorSecretGuard,
|
replaceModeratorSecretGuard,
|
||||||
checkRoomCreatorEnabledGuard
|
checkRoomCreatorEnabledGuard,
|
||||||
|
checkParticipantRoleAndAuthGuard,
|
||||||
|
runGuardsSerially
|
||||||
} from '../guards';
|
} from '../guards';
|
||||||
import {
|
import {
|
||||||
AboutComponent,
|
AboutComponent,
|
||||||
@ -34,8 +35,14 @@ export const baseRoutes: Routes = [
|
|||||||
{
|
{
|
||||||
path: '',
|
path: '',
|
||||||
component: RoomCreatorComponent,
|
component: RoomCreatorComponent,
|
||||||
canActivate: [checkRoomCreatorEnabledGuard, checkUserAuthenticatedGuard],
|
canActivate: [
|
||||||
|
runGuardsSerially(
|
||||||
|
checkRoomCreatorEnabledGuard,
|
||||||
|
checkUserAuthenticatedGuard
|
||||||
|
)
|
||||||
|
],
|
||||||
data: {
|
data: {
|
||||||
|
checkSkipAuth: true,
|
||||||
expectedRoles: [UserRole.USER],
|
expectedRoles: [UserRole.USER],
|
||||||
redirectToUnauthorized: 'login',
|
redirectToUnauthorized: 'login',
|
||||||
redirectToInvalidRole: 'console'
|
redirectToInvalidRole: 'console'
|
||||||
@ -111,11 +118,14 @@ export const baseRoutes: Routes = [
|
|||||||
path: 'room/:room-name',
|
path: 'room/:room-name',
|
||||||
component: VideoRoomComponent,
|
component: VideoRoomComponent,
|
||||||
canActivate: [
|
canActivate: [
|
||||||
applicationModeGuard,
|
runGuardsSerially(
|
||||||
extractQueryParamsGuard,
|
applicationModeGuard,
|
||||||
checkParticipantNameGuard,
|
extractQueryParamsGuard,
|
||||||
validateRoomAccessGuard,
|
checkParticipantNameGuard,
|
||||||
replaceModeratorSecretGuard
|
validateRoomAccessGuard,
|
||||||
|
checkParticipantRoleAndAuthGuard,
|
||||||
|
replaceModeratorSecretGuard
|
||||||
|
)
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user