-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix: Refresh Token in Custom Interceptor (#191)
- Loading branch information
Showing
3 changed files
with
38 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,26 @@ | ||
import { HttpInterceptorFn } from '@angular/common/http'; | ||
import { inject } from '@angular/core'; | ||
import { SecurityStore } from './security-store.service'; | ||
import { from, switchMap } from 'rxjs'; | ||
|
||
export const securityInterceptor: HttpInterceptorFn = (req, next) => { | ||
const keycloakService = inject(SecurityStore); | ||
|
||
const bearer = keycloakService.user()?.bearer; | ||
// update access token to prevent 401s | ||
return from(keycloakService.updateToken()).pipe( | ||
switchMap(() => { | ||
// add bearer token to request | ||
const bearer = keycloakService.user()?.bearer; | ||
|
||
if (!bearer) { | ||
return next(req); | ||
} | ||
if (!bearer) { | ||
return next(req); | ||
} | ||
|
||
return next( | ||
req.clone({ | ||
headers: req.headers.set('Authorization', `Bearer ${bearer}`) | ||
return next( | ||
req.clone({ | ||
headers: req.headers.set('Authorization', `Bearer ${bearer}`) | ||
}) | ||
); | ||
}) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters