Last change
on this file since 8b13eb2 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
|
Rev | Line | |
---|
[1f4846d] | 1 | import { Injectable } from '@angular/core';
|
---|
| 2 | import {
|
---|
| 3 | CanActivate,
|
---|
| 4 | ActivatedRouteSnapshot,
|
---|
| 5 | RouterStateSnapshot,
|
---|
| 6 | UrlTree,
|
---|
| 7 | Router,
|
---|
| 8 | } from '@angular/router';
|
---|
| 9 | import { Observable } from 'rxjs';
|
---|
| 10 | import { AuthService } from '../services/auth.service';
|
---|
| 11 | import { map } from 'rxjs/operators';
|
---|
| 12 |
|
---|
| 13 | @Injectable({
|
---|
| 14 | providedIn: 'root',
|
---|
| 15 | })
|
---|
| 16 | export 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.