Commit 92c4497b authored by Almouhannad's avatar Almouhannad

(F) Add error pages

parent ba3c5ec5
...@@ -3,6 +3,8 @@ import { RouterModule, Routes } from '@angular/router'; ...@@ -3,6 +3,8 @@ import { RouterModule, Routes } from '@angular/router';
import { HomeComponent } from './components/home/home.component'; import { HomeComponent } from './components/home/home.component';
import { RoleGuard } from './services/authentication/guards/role-guard'; import { RoleGuard } from './services/authentication/guards/role-guard';
import { Roles } from './classes/Authentication/roles'; import { Roles } from './classes/Authentication/roles';
import { ForbiddenComponent } from './components/errors/forbidden/forbidden.component';
import { NotFoundComponent } from './components/errors/not-found/not-found.component';
const routes: Routes = [ const routes: Routes = [
{ {
...@@ -16,6 +18,17 @@ const routes: Routes = [ ...@@ -16,6 +18,17 @@ const routes: Routes = [
component: HomeComponent, component: HomeComponent,
canActivate: [RoleGuard], canActivate: [RoleGuard],
data: { role: Roles.NotRegistered } data: { role: Roles.NotRegistered }
},
{
path: 'errors/forbidden',
component: ForbiddenComponent,
canActivate: [RoleGuard],
data: { role: Roles.NotRegistered }
},
// Everything else
{
path: '**',
component: NotFoundComponent
} }
]; ];
@NgModule({ @NgModule({
......
...@@ -16,6 +16,8 @@ import { FormsModule } from '@angular/forms'; ...@@ -16,6 +16,8 @@ import { FormsModule } from '@angular/forms';
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http';
import { AuthenticationService } from './services/authentication/authentication.service'; import { AuthenticationService } from './services/authentication/authentication.service';
import { AuthenticationInterceptor } from './services/authentication/interceptor/authentication.interceptor'; import { AuthenticationInterceptor } from './services/authentication/interceptor/authentication.interceptor';
import { ForbiddenComponent } from './components/errors/forbidden/forbidden.component';
import { NotFoundComponent } from './components/errors/not-found/not-found.component';
@NgModule({ @NgModule({
...@@ -46,7 +48,9 @@ import { AuthenticationInterceptor } from './services/authentication/interceptor ...@@ -46,7 +48,9 @@ import { AuthenticationInterceptor } from './services/authentication/interceptor
HeaderComponent, HeaderComponent,
FooterComponent, FooterComponent,
HomeComponent, HomeComponent,
LoginFormComponent LoginFormComponent,
ForbiddenComponent,
NotFoundComponent
], ],
// identifies the root component that Angular should // identifies the root component that Angular should
......
<div class="cutom-child text-center" style="padding: 6em;">
<i class="bi bi-exclamation-triangle-fill text-danger" style="font-size: 4em;"></i>
<h1 class="text-danger text-center" style="font-family: Cairo;">خطأ: لا يمكنك الوصول إلى الصفحة التي طلبتها</h1>
</div>
\ No newline at end of file
import { Component } from '@angular/core';
@Component({
selector: 'app-forbidden',
templateUrl: './forbidden.component.html',
styleUrl: './forbidden.component.css'
})
export class ForbiddenComponent {
}
<div class="cutom-child text-center" style="padding: 6em;">
<i class="bi bi-exclamation-triangle-fill text-danger" style="font-size: 4em;"></i>
<h1 class="text-danger text-center" style="font-family: Cairo;">خطأ: الصفحة المطلوبة غير موجودة</h1>
</div>
\ No newline at end of file
import { Component } from '@angular/core';
@Component({
selector: 'app-not-found',
templateUrl: './not-found.component.html',
styleUrl: './not-found.component.css'
})
export class NotFoundComponent {
}
/* #region Custom */
.custom-parent {
display: flex;
flex-direction:column;
min-height:100vh;
}
app-footer {
margin-top: auto;
}
/* #endregion */
/* #region General */ /* #region General */
::ng-deep .custom-child{ ::ng-deep .custom-child{
color: var(--default-color); color: var(--default-color);
......
<app-header class="sticky-top" [userData]="userData"></app-header> <div class="custom-parent">
<app-header class="sticky-top" [userData]="userData"></app-header>
<router-outlet></router-outlet> <router-outlet></router-outlet>
<app-footer></app-footer>
</div>
<app-footer></app-footer>
\ No newline at end of file
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Router } from '@angular/router';
import { AuthenticationService } from '../authentication.service'; import { AuthenticationService } from '../authentication.service';
import { Roles } from '../../../classes/Authentication/roles'; import { Roles } from '../../../classes/Authentication/roles';
import { UserData } from '../../../classes/Authentication/user-data'; import { UserData } from '../../../classes/Authentication/user-data';
...@@ -9,22 +9,34 @@ import { UserData } from '../../../classes/Authentication/user-data'; ...@@ -9,22 +9,34 @@ import { UserData } from '../../../classes/Authentication/user-data';
}) })
export class RoleGuard implements CanActivate { export class RoleGuard implements CanActivate {
constructor(private authenticationService: AuthenticationService) { } constructor(private authenticationService: AuthenticationService, private router: Router) { }
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean {
const userData: UserData | null = this.authenticationService.getUserData(); const userData: UserData | null = this.authenticationService.getUserData();
const requiredRole: string = route.data['role']; const requiredRole: string = route.data['role'];
// No required role
if (!requiredRole) if (!requiredRole)
return true; return true;
// No required role
if (requiredRole === Roles.NotRegistered) if (requiredRole === Roles.NotRegistered)
return true; return true;
// Reauired role, but not registered user
if (!userData) if (!userData)
return false; {
this.router.navigate(["errors/forbidden"]);
return userData.role === requiredRole; return false;
}
if (userData.role !== requiredRole)
{
this.router.navigate(["errors/forbidden"]);
return false;
}
return true;
} }
......
Markdown is supported
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