UNPKG

ohayolibs

Version:

Ohayo is a set of essential modules for ohayojp.

32 lines (29 loc) 947 B
import { HttpRequest } from '@angular/common/http'; import { Injectable } from '@angular/core'; import { OhayoAuthConfig } from '@ohayo/util'; import { BaseInterceptor } from '../base.interceptor'; import { CheckJwt } from '../helper'; import { DA_SERVICE_TOKEN } from '../interface'; import { JWTTokenModel } from './jwt.model'; /** * JWT 拦截器 * * ``` * // app.module.ts * { provide: HTTP_INTERCEPTORS, useClass: JWTInterceptor, multi: true} * ``` */ @Injectable() export class JWTInterceptor extends BaseInterceptor { isAuth(options: OhayoAuthConfig): boolean { this.model = this.injector.get(DA_SERVICE_TOKEN).get<JWTTokenModel>(JWTTokenModel); return CheckJwt(this.model as JWTTokenModel, options.token_exp_offset!); } setReq(req: HttpRequest<any>, _options: OhayoAuthConfig): HttpRequest<any> { return req.clone({ setHeaders: { Authorization: `Bearer ${this.model.token}`, }, }); } }