From e747f8090f78cf97dd6110488c69565a98be0c44 Mon Sep 17 00:00:00 2001 From: Yaseer Munas Date: Thu, 30 Nov 2023 20:12:00 +0530 Subject: [PATCH 1/2] Added signinSilent method To use manual trigger for silent refresh token --- AngularClient/src/app/shared/services/auth.service.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/AngularClient/src/app/shared/services/auth.service.ts b/AngularClient/src/app/shared/services/auth.service.ts index 200e089..f89e20d 100644 --- a/AngularClient/src/app/shared/services/auth.service.ts +++ b/AngularClient/src/app/shared/services/auth.service.ts @@ -32,6 +32,10 @@ export class AuthService { return this._userManager.signinRedirect(); } + public signinSilent(): Promise { + return this.userManager.signinSilent(); + } + public isAuthenticated = (): Promise => { return this._userManager.getUser() .then(user => { From 59fdd854f0e5177f7423c27e435374baf9d4ce2b Mon Sep 17 00:00:00 2001 From: Yaseer Munas Date: Thu, 30 Nov 2023 20:19:46 +0530 Subject: [PATCH 2/2] Update the token when the access token may expire before an API request, potentially resulting in a silent redirect In certain situations, the access token may expire before an API request, potentially resulting in a silent redirect. The challenge arises when ongoing requests lack of ability to update the access token dynamically, leading to failures when interacting with the API. To address this, I've made adjustments to proactively check for token expiration. If expired, a manual silent redirect is triggered to obtain a new token, ensuring subsequent API calls use the updated access token --- .../services/auth-interceptor.service.ts | 37 ++++++++++++++----- 1 file changed, 28 insertions(+), 9 deletions(-) diff --git a/AngularClient/src/app/shared/services/auth-interceptor.service.ts b/AngularClient/src/app/shared/services/auth-interceptor.service.ts index 683662e..2f5a262 100644 --- a/AngularClient/src/app/shared/services/auth-interceptor.service.ts +++ b/AngularClient/src/app/shared/services/auth-interceptor.service.ts @@ -1,7 +1,7 @@ import { AuthService } from './auth.service'; import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent, HttpHeaders } from '@angular/common/http'; -import { Observable, from } from 'rxjs'; +import { Observable, from, lastValueFrom } from 'rxjs'; import { Constants } from '../constants'; @Injectable({ @@ -13,17 +13,36 @@ export class AuthInterceptorService implements HttpInterceptor { intercept(req: HttpRequest, next: HttpHandler): Observable> { if(req.url.startsWith(Constants.apiRoot)){ - return from( - this._authService.getAccessToken() - .then(token => { - const headers = new HttpHeaders().set('Authorization', `Bearer ${token}`); - const authRequest = req.clone({ headers }); - return next.handle(authRequest).toPromise(); - }) - ); + return this.interceptRequestWithAccessToken(req, next); } else { return next.handle(req); } } + + interceptRequestWithAccessToken(request: HttpRequest, next: HttpHandler) { + return from( + this._authService.getAccessToken() + .then(accessToken => { + + if (!accessToken) { + this._authService.signinSilent().then(user => { + return this.updateRequestHeader(request, next, user.access_token); + }); + } else { + return this.updateRequestHeader(request, next, accessToken); + } + }) + ); + } + + updateRequestHeader(request: HttpRequest, next: HttpHandler, accessToken: string) { + const headerss = request.headers.set( + "Authorization", + `Bearer ${accessToken}` + ); + + const authReq = request.clone({ headers: headerss }); + return lastValueFrom(next.handle(authReq)); + } }