Created
November 22, 2020 10:28
-
-
Save sunilkumarmedium/b570cbf414abac97b41180e7c803a48d to your computer and use it in GitHub Desktop.
auth.guard.ts
This file contains 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 { 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