Skip to content

Instantly share code, notes, and snippets.

@abhishekluv
Created February 10, 2020 12:08
Show Gist options
  • Save abhishekluv/2b8760d4b0d08bb51a73305a6b51c455 to your computer and use it in GitHub Desktop.
Save abhishekluv/2b8760d4b0d08bb51a73305a6b51c455 to your computer and use it in GitHub Desktop.
Angular Source Code Demo 2
import { Injectable } from '@angular/core';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { CanActivate } from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(private router: Router) {
}
canActivate(next: ActivatedRouteSnapshot,
state: RouterStateSnapshot): boolean {
if (localStorage.getItem('token') != null) {
return true;
} else {
this.router.navigateByUrl('/login');
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment