Skip to content

Commit 245198d

Browse files
committed
Add guard routing
1 parent 4588312 commit 245198d

File tree

6 files changed

+31
-4
lines changed

6 files changed

+31
-4
lines changed

src/app/topics/02-advanced-component/advanced-component.module.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { RouterModule, Routes } from '@angular/router';
44
import { SharedComponentsModule } from 'src/app/shared/components/shared-components.module';
55
import { AdvancedComponent } from './advanced.component';
66
import { CustomTwoWayDataBindingComponent } from './custom-two-way-data-binding/custom-two-way-data-binding.component';
7+
import { FormsModule } from '@angular/forms';
78

89
const routes: Routes = [
910
{
@@ -17,6 +18,7 @@ const routes: Routes = [
1718
imports: [
1819
RouterModule.forChild(routes),
1920
CommonModule,
21+
FormsModule,
2022
SharedComponentsModule
2123
]
2224
})

src/app/topics/02-advanced-component/advanced.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,6 @@ export class AdvancedComponent {
1515
{title: 'Sanitization of HTML content', url: ''},
1616
];
1717

18-
1918
public message = "Hello from two way data binding";
19+
2020
}

src/app/topics/08-advanced-routing/advanced-routing.component.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ <h1 class="mb-2 text-1xl font-bold text-gray-800 md:text-3xl">
66
<div class="grid grid-cols-1 md:grid-cols-5 gap-6">
77
<div class="col-span-2"><app-topic-card [topicCards]="topicCards"></app-topic-card></div>
88
<div class="demo-content col-span-3 bg-slate-100 p-4 shadow-lg">
9-
9+
<!-- Child routing content to be injected -->
10+
<router-outlet></router-outlet>
1011
</div>
1112
</div>
1213

src/app/topics/08-advanced-routing/advanced-routing.module.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,23 @@ import { RouterModule, Routes } from '@angular/router';
44
import { SharedComponentsModule } from 'src/app/shared/components/shared-components.module';
55
import { AdvancedRoutingComponent } from './advanced-routing.component';
66
import { GuardComponent } from './components/guard/guard.component';
7+
import { PermissionsService, UserToken, canActivateUser } from './services/routing.service';
78

89
const routes: Routes = [
910
{
1011
path: '',
1112
component: AdvancedRoutingComponent,
1213
children: [{
1314
path: 'with-guard',
14-
component: GuardComponent
15+
component: GuardComponent,
16+
canActivate: [canActivateUser]
1517
}]
1618
}
1719
];
1820

1921
@NgModule({
2022
declarations: [AdvancedRoutingComponent],
23+
providers: [PermissionsService, UserToken],
2124
imports: [
2225
RouterModule.forChild(routes),
2326
CommonModule,

src/app/topics/08-advanced-routing/components/guard/guard.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<h2 class="mb-2 text-1xl font-bold text-gray-800 md:text-3xl">
1+
<h2 class="mb-2 text-1xl font-bold text-gray-800 md:text-2xl">
22
Component with guard
33
</h2>
44
<p class="pb-8">Congratulation! You are authorised!</p>
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { Injectable, inject } from "@angular/core";
2+
import { ActivatedRouteSnapshot, CanActivateFn, RouterStateSnapshot } from "@angular/router";
3+
4+
@Injectable()
5+
export class UserToken {
6+
}
7+
8+
@Injectable()
9+
export class PermissionsService {
10+
canActivate(currentUser: UserToken, userId: string): boolean {
11+
return true;
12+
}
13+
canMatch(currentUser: UserToken): boolean {
14+
return true;
15+
}
16+
}
17+
18+
19+
export const canActivateUser: CanActivateFn = (route: ActivatedRouteSnapshot, state: RouterStateSnapshot) => {
20+
return inject(PermissionsService).canActivate(inject(UserToken), 'fakeUserId');
21+
};

0 commit comments

Comments
 (0)