source: Farmatiko/ClientApp/src/app/shared/guards/auth.guard.ts@ 68454c6

Last change on this file since 68454c6 was 1f4846d, checked in by Mile Jankuloski <mile.jankuloski@…>, 4 years ago

Jwt token auth interceptors, services and guards

  • Property mode set to 100644
File size: 927 bytes
Line 
1import { Injectable } from '@angular/core';
2import {
3 CanActivate,
4 ActivatedRouteSnapshot,
5 RouterStateSnapshot,
6 UrlTree,
7 Router,
8} from '@angular/router';
9import { Observable } from 'rxjs';
10import { AuthService } from '../services/auth.service';
11import { map } from 'rxjs/operators';
12
13@Injectable({
14 providedIn: 'root',
15})
16export class AuthGuard implements CanActivate {
17 constructor(private router: Router, private authService: AuthService) {}
18
19 canActivate(
20 next: ActivatedRouteSnapshot,
21 state: RouterStateSnapshot
22 ):
23 | Observable<boolean | UrlTree>
24 | Promise<boolean | UrlTree>
25 | boolean
26 | UrlTree {
27 return this.authService.user$.pipe(
28 map((user) => {
29 if (user) {
30 return true;
31 } else {
32 this.router.navigate(['login'], {
33 queryParams: { returnUrl: state.url },
34 });
35 return false;
36 }
37 })
38 );
39 }
40}
Note: See TracBrowser for help on using the repository browser.