Skip to content

Instantly share code, notes, and snippets.

@sunilkumarmedium
Created November 22, 2020 10:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sunilkumarmedium/b570cbf414abac97b41180e7c803a48d to your computer and use it in GitHub Desktop.
Save sunilkumarmedium/b570cbf414abac97b41180e7c803a48d to your computer and use it in GitHub Desktop.
auth.guard.ts
import { Injectable } from '@angular/core';
import { Router, CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot } from '@angular/router';
import { Observable } from 'rxjs';
import { AuthenticationService } from '../_services/authentication/authentication.service';
@Injectable({
providedIn: 'root'
})
export class AuthGuard implements CanActivate {
constructor(
private router: Router,
private authenticationService: AuthenticationService
) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean> | Promise<boolean> | boolean {
const user = this.authenticationService.userValue;
if (user) {
return true;
}
this.router.navigate(['/account/login'], {queryParams: { returnUrl: state.url }});
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment