-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.guard.ts
30 lines (29 loc) · 1.07 KB
/
login.guard.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
Protects the login page
Gets the current value of UserService.isLoggedIn
If it's false (user is not already logged in), the user is allowed to visit the login page
If it's true (user is already logged in), the user is redirected to the dashboard
*/
import { Injectable } from '@angular/core';
import { CanActivate, ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree, Router } from '@angular/router';
import { Observable } from 'rxjs';
import { UserService } from '../services/user.service';
import { map } from 'rxjs/operators';
@Injectable({
providedIn: 'root'
})
export class LoginGuard implements CanActivate {
constructor(private userService: UserService, private router: Router) {}
canActivate(
route: ActivatedRouteSnapshot,
state: RouterStateSnapshot): Observable<boolean | UrlTree> | Promise<boolean | UrlTree> | boolean | UrlTree {
return this.userService.getLoggedIn().pipe(
map(isLoggedIn => {
if (!isLoggedIn) {
return true;
}
return this.router.parseUrl('/dashboard');
})
);
}
}