auth
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
import { HttpInterceptorFn } from '@angular/common/http';
|
||||
import { inject } from '@angular/core';
|
||||
import { from, switchMap } from 'rxjs';
|
||||
import { KeycloakService } from './keycloak.service';
|
||||
import { API_BASE_URL } from '../issues/issues-api.service';
|
||||
|
||||
export const authInterceptor: HttpInterceptorFn = (req, next) => {
|
||||
if (!req.url.startsWith(API_BASE_URL)) {
|
||||
return next(req);
|
||||
}
|
||||
const keycloak = inject(KeycloakService);
|
||||
return from(keycloak.getToken()).pipe(
|
||||
switchMap((token) => {
|
||||
if (!token) return next(req);
|
||||
return next(req.clone({ setHeaders: { Authorization: `Bearer ${token}` } }));
|
||||
}),
|
||||
);
|
||||
};
|
||||
@@ -38,4 +38,13 @@ export class KeycloakService {
|
||||
isLoggedIn(): boolean {
|
||||
return this.keycloak.authenticated ?? false;
|
||||
}
|
||||
|
||||
async getToken(): Promise<string | undefined> {
|
||||
try {
|
||||
await this.keycloak.updateToken(30);
|
||||
return this.keycloak.token;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user