export class YourInterceptor implements HttpInterceptor {
constructor() {}
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
return next.handle(request).do((event: HttpEvent<any>) => {
if (event instanceof HttpResponse) {
// do stuff with response if you want
}
}, (err: any) => {
if (err instanceof HttpErrorResponse) {
if (err.status === 401) {
// redirect to the login route
// or show a modal
}
}
});
}
}
2 回答
您需要检查
HTTP
个请求,例如,如果会话(令牌)过期并且用户尝试从客户端尝试某些HTTP调用,则服务器必须返回一些相关的状态代码,让's say it' 401 .因此,在这种情况下,您将检查服务器是否正在响应状态代码
401
然后显示弹出窗口并重定向到log in
屏幕 .您可以使用角度 http-interceptor 至 intercept 所有请求 . 当您的令牌或会话到期时,http响应将为 401(unauthorized) . 基于此,您可以将用户重定向到登录路由 . 请参阅HttpInterceptor的文档 .
像这样的东西 .
希望这可以帮助 .