Skip to content

Commit 043eed9

Browse files
author
Your Name
committed
Angular Router Course
1 parent 71298f0 commit 043eed9

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/app/courses/courses-routing.module.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const routes: Routes = [
1919
path:":courseUrl",
2020
component: CourseComponent,
2121
canActivate: [AuthGuard],
22+
canActivateChild: [AuthGuard],
2223
children: [
2324
{
2425
path: "",

src/app/services/auth.guard.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import {Injectable} from '@angular/core';
22
import {AuthStore} from './auth.store';
3-
import {ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
3+
import {ActivatedRouteSnapshot, CanActivate, CanActivateChild, Router, RouterStateSnapshot, UrlTree} from '@angular/router';
44
import {Observable} from 'rxjs';
55
import {map} from 'rxjs/operators';
66

77

88
@Injectable()
9-
export class AuthGuard implements CanActivate {
9+
export class AuthGuard implements CanActivate, CanActivateChild {
1010

1111
constructor(private auth:AuthStore,
1212
private router:Router) {
@@ -18,12 +18,25 @@ export class AuthGuard implements CanActivate {
1818
state: RouterStateSnapshot):
1919
Observable<boolean | UrlTree> {
2020

21+
return this.checkIfAuthenticated();
22+
23+
}
24+
25+
26+
canActivateChild(childRoute: ActivatedRouteSnapshot,
27+
state: RouterStateSnapshot):
28+
Observable<boolean | UrlTree> {
29+
30+
return this.checkIfAuthenticated();
31+
}
32+
33+
34+
private checkIfAuthenticated() {
2135
return this.auth.isLoggedIn$
2236
.pipe(
2337
map(loggedIn =>
2438
loggedIn? true: this.router.parseUrl('/login') )
2539
);
26-
2740
}
2841

2942
}

0 commit comments

Comments
 (0)