在重新授权并使用新会话后重试该函数



我有一个接收数据的函数

getAll(params) {
return this.http.get(url).pipe(map(data => {
return data
})
})

在this.http.get中使用会话id进行授权。如果我的会话死亡,我如何使用post请求调用relogin函数并重试我的getAll()

我想你指的是刷新令牌方法。试试这个链接https://jasonwatmore.com/post/2020/05/22/angular-9-jwt-authentication-with-refresh-tokens但你应该使用OAuth2或类似的隐式流。

@Injectable()
export class ErrorInterceptor implements HttpInterceptor {
constructor(private authenticationService: AuthenticationService) { }
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).pipe(catchError(err => {
if ([401, 403].includes(err.status) && this.authenticationService.userValue) {
// auto logout if 401 or 403 response returned from api
// this.authenticationService.logout();
// Or call your get token and try it again
}
const error = (err && err.error && err.error.message) || err.statusText;
console.error(err);
return throwError(error);
}))
}
}

最新更新