Skip to content

Instantly share code, notes, and snippets.

@tiagolpadua
Created March 18, 2021 22:25
Show Gist options
  • Save tiagolpadua/183c60f803616d2107f6631501e3332a to your computer and use it in GitHub Desktop.
Save tiagolpadua/183c60f803616d2107f6631501e3332a to your computer and use it in GitHub Desktop.
cmail\src\app\guards\auth2.guard.ts
import { Injectable } from '@angular/core';
import { CanActivate, Router } from '@angular/router';
import { catchError, map } from 'rxjs/operators';
import { LoginService } from '../services/login.service';
@Injectable({
providedIn: 'root'
})
export class Auth2Guard implements CanActivate {
constructor(private roteador: Router, private loginService: LoginService) { }
canActivate() {
return this.loginService.validaToken()
.pipe(
map(response => response.ok),
catchError(() => {
localStorage.removeItem('cmail-token');
this.roteador.navigate(['login']);
return [false];
})
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment