Add a guard to deny access for some routes. #6
@@ -1,13 +1,16 @@
|
||||
import { Routes } from '@angular/router';
|
||||
import { alreadyAuthenticatedGuard } from './core/guard/already-authenticated.guard';
|
||||
|
||||
export const routes: Routes = [
|
||||
{
|
||||
path: 'login',
|
||||
loadComponent: () => import('./pages/login/login.component').then(module => module.LoginComponent)
|
||||
loadComponent: () => import('./pages/login/login.component').then(module => module.LoginComponent),
|
||||
canActivate: [alreadyAuthenticatedGuard]
|
||||
},
|
||||
{
|
||||
path: 'signin',
|
||||
loadComponent: () => import('./pages/signin/signin.component').then(module => module.SigninComponent)
|
||||
loadComponent: () => import('./pages/signin/signin.component').then(module => module.SigninComponent),
|
||||
canActivate: [alreadyAuthenticatedGuard]
|
||||
},
|
||||
{
|
||||
path: 'disconnect',
|
||||
|
||||
18
frontend/src/app/core/guard/already-authenticated.guard.ts
Normal file
18
frontend/src/app/core/guard/already-authenticated.guard.ts
Normal file
@@ -0,0 +1,18 @@
|
||||
import { inject } from "@angular/core";
|
||||
import { CanActivateFn, Router } from "@angular/router";
|
||||
import { AuthenticationService } from "../service/authentication.service";
|
||||
import { MatSnackBar } from "@angular/material/snack-bar";
|
||||
|
||||
export const alreadyAuthenticatedGuard: CanActivateFn = () => {
|
||||
const authenticationService = inject(AuthenticationService);
|
||||
const router = inject(Router);
|
||||
const snackBar = inject(MatSnackBar);
|
||||
|
||||
if (authenticationService.isAuthenticated()) {
|
||||
router.navigate(['/home']);
|
||||
snackBar.open($localize`You can't access to this page because you are already authenticated.`, $localize`Close`, { duration: 5000 });
|
||||
return false;
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user