-
-
Notifications
You must be signed in to change notification settings - Fork 130
/
jwt.interceptor.ts
31 lines (26 loc) · 1.07 KB
/
jwt.interceptor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { Injectable } from '@angular/core';
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Store } from '@ngxs/store';
import { environment } from '@env/environment';
import { OAuthService } from '@xmlking/angular-oauth2-oidc-all';
const allowedUrls = [environment.API_BASE_URL, environment.DOCS_BASE_URL];
@Injectable()
export class JwtInterceptor implements HttpInterceptor {
constructor(private store: Store, private oauthService: OAuthService) {}
intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const access_token = this.oauthService.getAccessToken();
// const token = this.store.selectSnapshot<string>((state: AuthState) => state.auth.profile.token);
const url = req.url.toLowerCase();
const found = !!allowedUrls.find(u => url.startsWith(u));
if (!found) {
return next.handle(req);
}
req = req.clone({
setHeaders: {
Authorization: `Bearer ${access_token}`,
},
});
return next.handle(req);
}
}