javascript - 在角度 8 的自定义方法中调用拦截
问题描述
在 signInWithToken 方法中,我得到一个需要在请求标头中设置为“授权”的令牌Bearer ${token for here}
。但是问题是在调用signInWithToken方法的时候,发生了重定向,并且intercept()方法来不及调用。如何强制在 signInWithToken () 方法中调用 intercept () 方法?它们都在同一个服务中。
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
if (this.access) {
req = req.clone({
headers: req.headers.set('Authorization', `Bearer ${this.access}`)
});
}
... // standard code for intercept
}
signInWithToken(login: string, password: string): Observable<any> {
return this.http.post('./api/internal/auth/sign-in-with-token', {login, password}).pipe(
map((details: SignInDetails) => {
this.areCookiesValid = true;
this.access = details.token; // in this method, when we received the token, we need to call intercept
return details;
}),
catchError((error) => throwError(error))
);
}
解决方案
推荐阅读
- docker - nginx html文件夹内的docker无法创建挂载
- ruby-on-rails - 升级到 Rails 5.2:如何避免非可选关联的“可选:真”?
- visual-studio - 如何修复'无效的资源目录名称
Xamarin.Android 中的错误? - javascript - (Handsontable) - hot.setCellMeta 不起作用
- r - opts_knit$set(unnames.chunk.label = "figs") 不会更改默认标签
- web-applications - 为什么 java.exe 由于“EnhancedQueueExecutor.java”线程而消耗更多 CPU?
- javascript - 如何检测 URL 是否在固定 URL 之后包含单词?
- c# - 如何正确处理此 dbnull 异常?
- r - 将散点图从 PDF 导入 R
- android - 当我从通知切换回应用程序时,为什么媒体播放器会多次播放媒体?