We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
最近一个项目有这样的需求,在发送请求时,如果没有用户信息,则请求后端拿到用户信息,继续发送该请求。这样的需求,想了下,适合在NG的拦截器这块去做,那么如果实现呢。
auth-service.ts
@Injectable() export class AuthService { constructor(private http: HttpClient) { } getToken(): string { return localStorage.getItem('token'); } removeToken(): void { localStorage.removeItem('token'); } logic() { return this.http.get('mock-data/user.json').map((res: any) => { localStorage.setItem('token', res.token); return res; }); } }
token-interceptor.ts
@Injectable() export class TokenInterceptor implements HttpInterceptor { constructor(public authService: AuthService) { } intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { if (request.url === 'mock-data/user.json') { return next.handle(request); } if (this.authService.getToken()) { request = request.clone({ setHeaders: { Authorization: `Bearer ${this.authService.getToken()}` } }); return next.handle(request); } return this.authService.logic().flatMap(() => { return next.handle(request).do((event) => { return event; } ); }); } }
注意,这里一开始有个URL的判断,因为我们下面的情况,需要多请求一次登录,而登录本身的请求,也会走拦截器,所以这里需要加上判断,去掉登录这个请求,否则,登录请求进来,还是会再请求一次登录无限走下去,内存溢出。
https://stackoverflow.com/questions/45345354/how-use-async-service-into-angular-httpclient-interceptor
The text was updated successfully, but these errors were encountered:
#66 use async in Interceptor
52edd9e
No branches or pull requests
实现
auth-service.ts
token-interceptor.ts
防止死锁
注意,这里一开始有个URL的判断,因为我们下面的情况,需要多请求一次登录,而登录本身的请求,也会走拦截器,所以这里需要加上判断,去掉登录这个请求,否则,登录请求进来,还是会再请求一次登录无限走下去,内存溢出。
相关资料
https://stackoverflow.com/questions/45345354/how-use-async-service-into-angular-httpclient-interceptor
The text was updated successfully, but these errors were encountered: