From 4d84d937879207842f5dbad0259340b63c271211 Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 8 Mar 2019 12:17:17 -0700 Subject: [PATCH 1/6] CanLoad Implemented --- docs/routing.md | 20 ++++++++++++++++++- .../src/lib/angular-token.service.ts | 14 ++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/docs/routing.md b/docs/routing.md index 7a68a9c6..152305a7 100644 --- a/docs/routing.md +++ b/docs/routing.md @@ -32,4 +32,22 @@ this.tokenService.signIn({ }, error => console.log(error) ); -``` \ No newline at end of file +``` + +Angular-Token implements the `CanLoad` interface, so it can directly be used as a route guard on lazy loading modules. +If the `signInRedirect` option is set the user will be redirected on a failed (=false) CanLoad using `Router.navigate()`. +It currently does not distinguish between user types. + +#### Example: +```javascript +const routerConfig: Routes = [ + { + path: '', + component: PublicComponent + }, { + path: 'restricted', + loadChildren: './restrictedmodule_path/restricted.module#RestrictedModule', + canLoad: [AngularTokenService] + } +]; +``` diff --git a/projects/angular-token/src/lib/angular-token.service.ts b/projects/angular-token/src/lib/angular-token.service.ts index c230efce..fb84b7a3 100755 --- a/projects/angular-token/src/lib/angular-token.service.ts +++ b/projects/angular-token/src/lib/angular-token.service.ts @@ -1,5 +1,5 @@ import { Injectable, Optional, Inject, PLATFORM_ID } from '@angular/core'; -import { ActivatedRoute, Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; +import { ActivatedRoute, Router, CanLoad, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http'; import { isPlatformServer } from '@angular/common'; @@ -145,6 +145,18 @@ export class AngularTokenService implements CanActivate { } } + canLoad(route: Route, segments: UrlSegment[]): boolean { + if (this.userSignedIn()) { + return true; + } else { + // Redirect user to sign in if signInRedirect is set + if (this.router && this.options.signInRedirect) { + this.router.navigate([this.options.signInRedirect]); + } + return false; + } + } + canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { if (this.userSignedIn()) { return true; From 6c391e86d64d2cbcfca1c94bd3dc5c0e554395fe Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 8 Mar 2019 12:22:51 -0700 Subject: [PATCH 2/6] CanLoad implemented declaration added to Class --- projects/angular-token/src/lib/angular-token.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/angular-token/src/lib/angular-token.service.ts b/projects/angular-token/src/lib/angular-token.service.ts index fb84b7a3..53a503da 100755 --- a/projects/angular-token/src/lib/angular-token.service.ts +++ b/projects/angular-token/src/lib/angular-token.service.ts @@ -25,7 +25,7 @@ import { @Injectable({ providedIn: 'root', }) -export class AngularTokenService implements CanActivate { +export class AngularTokenService implements CanActivate CanLoad { get currentUserType(): string { if (this.userType.value != null) { From 2dd50b353d37ca48d32690b3279108a58179c79d Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 8 Mar 2019 12:44:11 -0700 Subject: [PATCH 3/6] Build fix update --- projects/angular-token/src/lib/angular-token.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/angular-token/src/lib/angular-token.service.ts b/projects/angular-token/src/lib/angular-token.service.ts index 53a503da..b39e72a1 100755 --- a/projects/angular-token/src/lib/angular-token.service.ts +++ b/projects/angular-token/src/lib/angular-token.service.ts @@ -25,7 +25,7 @@ import { @Injectable({ providedIn: 'root', }) -export class AngularTokenService implements CanActivate CanLoad { +export class AngularTokenService implements CanActivate, CanLoad { get currentUserType(): string { if (this.userType.value != null) { From f956800c88f07687e9d840bdf3def98121d12ff7 Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 8 Mar 2019 12:53:52 -0700 Subject: [PATCH 4/6] Route and UrlSegment declaration added --- projects/angular-token/src/lib/angular-token.service.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/projects/angular-token/src/lib/angular-token.service.ts b/projects/angular-token/src/lib/angular-token.service.ts index b39e72a1..2efbe2db 100755 --- a/projects/angular-token/src/lib/angular-token.service.ts +++ b/projects/angular-token/src/lib/angular-token.service.ts @@ -1,5 +1,5 @@ import { Injectable, Optional, Inject, PLATFORM_ID } from '@angular/core'; -import { ActivatedRoute, Router, CanLoad, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; +import { ActivatedRoute, Router, CanLoad, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Route, UrlSegment } from '@angular/router'; import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http'; import { isPlatformServer } from '@angular/common'; From fb3f39109de97b64dab18b2f075eb66937a221ed Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 8 Mar 2019 13:04:27 -0700 Subject: [PATCH 5/6] Breaking line in less than 140 characters --- projects/angular-token/src/lib/angular-token.service.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/projects/angular-token/src/lib/angular-token.service.ts b/projects/angular-token/src/lib/angular-token.service.ts index 2efbe2db..d987df04 100755 --- a/projects/angular-token/src/lib/angular-token.service.ts +++ b/projects/angular-token/src/lib/angular-token.service.ts @@ -1,5 +1,6 @@ import { Injectable, Optional, Inject, PLATFORM_ID } from '@angular/core'; -import { ActivatedRoute, Router, CanLoad, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, Route, UrlSegment } from '@angular/router'; +import { ActivatedRoute, Router, CanLoad, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, + Route, UrlSegment } from '@angular/router'; import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http'; import { isPlatformServer } from '@angular/common'; From 61eae67c56f407978f7811fa3e8e3d778a668734 Mon Sep 17 00:00:00 2001 From: Juan Date: Fri, 8 Mar 2019 13:13:52 -0700 Subject: [PATCH 6/6] Removing Trail whitespace --- projects/angular-token/src/lib/angular-token.service.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/projects/angular-token/src/lib/angular-token.service.ts b/projects/angular-token/src/lib/angular-token.service.ts index d987df04..70332824 100755 --- a/projects/angular-token/src/lib/angular-token.service.ts +++ b/projects/angular-token/src/lib/angular-token.service.ts @@ -1,6 +1,6 @@ import { Injectable, Optional, Inject, PLATFORM_ID } from '@angular/core'; -import { ActivatedRoute, Router, CanLoad, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, - Route, UrlSegment } from '@angular/router'; +import { ActivatedRoute, Router, CanLoad, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, +Route, UrlSegment } from '@angular/router'; import { HttpClient, HttpResponse, HttpErrorResponse } from '@angular/common/http'; import { isPlatformServer } from '@angular/common';