-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.guard.ts
35 lines (32 loc) · 1.09 KB
/
auth.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
31
32
33
34
35
import { Injectable } from '@angular/core';
import type {
ActivatedRouteSnapshot,
CanActivate,
RouterStateSnapshot,
} from '@angular/router';
import { Router } from '@angular/router';
import { AuthModalService } from '@core/auth-modal/auth-modal.service';
import { AuthenticatedService } from '@core/cache-client/authenticated.service';
@Injectable({ providedIn: 'root' })
export class AuthGuard implements CanActivate {
constructor(
private readonly authenticatedService: AuthenticatedService,
private readonly authModalService: AuthModalService,
private readonly router: Router
) {}
async canActivate(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) {
const authenticated =
this.authenticatedService.isAuthenticated ||
(await this.authModalService.show('login'));
return authenticated ? true : this.router.parseUrl('/');
}
async canActivateChild(
next: ActivatedRouteSnapshot,
state: RouterStateSnapshot
) {
return this.canActivate(next, state);
}
}