From f29cf1eba570792177b1135832f13b8a60a44f76 Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Tue, 5 Sep 2017 11:40:51 +0100 Subject: [PATCH] Made guards less likely to give infinite loop --- src/app/_guards/customer.guard.ts | 13 +++++++------ src/app/_guards/org.guard.ts | 13 +++++++------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/app/_guards/customer.guard.ts b/src/app/_guards/customer.guard.ts index 82512a3..93687c1 100644 --- a/src/app/_guards/customer.guard.ts +++ b/src/app/_guards/customer.guard.ts @@ -7,15 +7,16 @@ export class CustomerGuard implements CanActivate { constructor(private router: Router) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - if (localStorage.getItem('usertype') == 'customer') { - console.log('Customer logged in') + if (localStorage.getItem('usertype') === 'customer') { + console.log('Customer logged in'); // customer logged in so return true return true; + } else if (localStorage.getItem('usertype') === 'organisation') { + console.log('not an customer'); + this.router.navigate(['/dashboard']); + return false; } - - // customer not logged in so redirect to org dashboard - console.log('not an customer') - this.router.navigate(['/dashboard']); + this.router.navigate(['/login']); return false; } } diff --git a/src/app/_guards/org.guard.ts b/src/app/_guards/org.guard.ts index b83b748..0205b47 100644 --- a/src/app/_guards/org.guard.ts +++ b/src/app/_guards/org.guard.ts @@ -7,15 +7,16 @@ export class OrgGuard implements CanActivate { constructor(private router: Router) { } canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot) { - if (localStorage.getItem('usertype') == 'organisation') { - console.log('Organisation logged in') + if (localStorage.getItem('usertype') === 'organisation') { + console.log('Organisation logged in'); // org logged in so return true return true; + } else if (localStorage.getItem('usertype') === 'customer') { + console.log('not an organisation'); + this.router.navigate(['/dashboard-customer']); + return false; } - - // org not logged in so redirect to customer dashboard - console.log('not an organisation') - this.router.navigate(['/dashboard-customer']); + this.router.navigate(['/login']); return false; } }