Skip to content

Commit

Permalink
fix: add updateTokenIfNeeded (#275)
Browse files Browse the repository at this point in the history
  • Loading branch information
anninowak authored Jun 12, 2024
1 parent 5a531a2 commit 03e8d1b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 10 deletions.
3 changes: 3 additions & 0 deletions libs/angular-auth/src/lib/auth-service-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export class AuthServiceWrapper {
getHeaderValues(): Record<string, string> {
return this.authService?.getHeaderValues() ?? {}
}
updateTokenIfNeeded(): Promise<boolean> {
return this.authService?.updateTokenIfNeeded() ?? Promise.reject()
}

async initializeAuthService(): Promise<void> {
const serviceTypeConfig = this.configService.getProperty(CONFIG_KEY.AUTH_SERVICE) ?? 'keycloak'
Expand Down
2 changes: 2 additions & 0 deletions libs/angular-auth/src/lib/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface AuthService {
getHeaderValues(): Record<string, string>

logout(): void

updateTokenIfNeeded(): Promise<boolean>
}

export enum Injectables {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,14 @@ export class KeycloakAuthService implements AuthService {
this.keycloakService.logout()
}

async updateTokenIfNeeded(): Promise<boolean> {
if (!(await this.keycloakService.isLoggedIn())) {
return this.keycloakService.login().then(() => false)
} else {
return this.keycloakService.updateToken()
}
}

getAuthProviderName(): string {
return 'keycloak-auth'
}
Expand Down
25 changes: 15 additions & 10 deletions libs/angular-auth/src/lib/token.interceptor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'
import { Injectable } from '@angular/core'
import { Observable } from 'rxjs'
import { Observable, from, mergeMap } from 'rxjs'
import { AuthServiceWrapper } from './auth-service-wrapper'

const WHITELIST = ['assets']
Expand All @@ -14,14 +14,19 @@ export class TokenInterceptor implements HttpInterceptor {
if (skip) {
return next.handle(request)
}
const headerValues = this.authService.getHeaderValues()
let headers = request.headers
for (const header in headerValues) {
headers = headers.set(header, headerValues[header])
}
const authenticatedReq: HttpRequest<unknown> = request.clone({
headers: headers,
})
return next.handle(authenticatedReq)

return from(this.authService.updateTokenIfNeeded()).pipe(
mergeMap(() => {
const headerValues = this.authService.getHeaderValues()
let headers = request.headers
for (const header in headerValues) {
headers = headers.set(header, headerValues[header])
}
const authenticatedReq: HttpRequest<unknown> = request.clone({
headers: headers,
})
return next.handle(authenticatedReq)
})
)
}
}

0 comments on commit 03e8d1b

Please sign in to comment.