feat(1.0.2):修复专栏问题;修复多端登录问题;
parent
2543c93ecf
commit
13f045569e
@ -0,0 +1,41 @@
|
||||
import {inject, Injectable, Injector} from '@angular/core';
|
||||
import {
|
||||
HttpRequest,
|
||||
HttpHandler,
|
||||
HttpEvent,
|
||||
HttpInterceptor,
|
||||
HttpResponse
|
||||
} from '@angular/common/http';
|
||||
import { Observable, tap } from 'rxjs';
|
||||
import { AuthConfigConsts, tokenNotExpired } from "../../mine/auth.jwt";
|
||||
|
||||
@Injectable()
|
||||
export class CheckInterceptor implements HttpInterceptor {
|
||||
constructor() {}
|
||||
//从请求头获取token
|
||||
intercept(req: HttpRequest<unknown>, next: HttpHandler): Observable<HttpEvent<unknown>> {
|
||||
const token = localStorage.getItem('token');
|
||||
let authHeader = req.headers;
|
||||
const authReq = req.clone({headers: authHeader});
|
||||
return next.handle(authReq).pipe(
|
||||
tap((event) => {
|
||||
// 如果是 HTTP 响应
|
||||
if (event instanceof HttpResponse) {
|
||||
// 从响应头中获取 Authorization token
|
||||
const authToken = event.headers.get('Authorization');
|
||||
if (authToken && (!token || token !== authToken.replace(AuthConfigConsts.HEADER_PREFIX_BEARER, ''))) {
|
||||
// 去掉 Bearer 前缀
|
||||
const newToken = authToken.replace(AuthConfigConsts.HEADER_PREFIX_BEARER, '');
|
||||
if(token != newToken) {
|
||||
localStorage.removeItem("token")
|
||||
localStorage.removeItem("giftCount")
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue