UNPKG

ohayolibs

Version:

Ohayo is a set of essential modules for ohayojp.

59 lines (53 loc) 1.72 kB
import { Inject, Injectable, Injector } from '@angular/core'; import { ActivatedRouteSnapshot, CanActivate, CanActivateChild, CanLoad, Route, RouterStateSnapshot, UrlSegment } from '@angular/router'; import { OhayoAuthConfig } from '@ohayo/util'; import { CheckJwt, ToLogin } from '../helper'; import { DA_SERVICE_TOKEN, ITokenService } from '../interface'; import { JWTTokenModel } from './jwt.model'; /** * JWT 路由守卫, [ACL Document](https://ohayojp.com/auth/guard). * * ```ts * data: { * path: 'home', * canActivate: [ JWTGuard ] * }, * { * path: 'my', * canActivateChild: [JWTGuard], * children: [ * { path: 'profile', component: MockComponent } * ], * }, * ``` */ @Injectable({ providedIn: 'root' }) export class JWTGuard implements CanActivate, CanActivateChild, CanLoad { private url: string | undefined; private get cog(): OhayoAuthConfig { return this.srv.options; } constructor(@Inject(DA_SERVICE_TOKEN) private srv: ITokenService, private injector: Injector) { } private process(): boolean { const res = CheckJwt(this.srv.get<JWTTokenModel>(JWTTokenModel), this.cog.token_exp_offset!); if (!res) { ToLogin(this.cog, this.injector, this.url); } return res; } // lazy loading canLoad(route: Route, _segments: UrlSegment[]): boolean { this.url = route.path; return this.process(); } // all children route canActivateChild(_childRoute: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { this.url = state.url; return this.process(); } // route canActivate(_route: ActivatedRouteSnapshot, state: RouterStateSnapshot): boolean { this.url = state.url; return this.process(); } }