Last active
March 18, 2020 11:14
-
-
Save medesko/5b2dbd6cc34d928c3af59e49af595492 to your computer and use it in GitHub Desktop.
Angular 2 - PERFORMANCE
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router'; | |
import { ApiService } from '../services/api.service'; | |
@Injectable() | |
export class AuthGuard implements CanActivate { | |
constructor(private router:Router, private _apiService:ApiService) {} | |
canActivate(route: ActivatedRouteSnapshot, state: RouterStateSnapshot){ | |
console.log('AuthGuard#canActivate called'); | |
return this._apiService.loginByToken().then((logged) => { | |
if (logged === false){ | |
localStorage.removeItem('authToken'); | |
this.router.navigate(['users', 'login']); | |
return false; | |
} | |
return true; | |
}).catch((error)=>{ | |
localStorage.removeItem('authToken'); | |
this.router.navigate(['users', 'login']); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment