Last active
February 21, 2023 10:30
-
-
Save Toilal/8849bd63d53bd2df2dd4df92d3b12f26 to your computer and use it in GitHub Desktop.
@auth0/angular2-jwt Authorization Service and HttpInterceptor supporting JWT Refresh Token (Angular 4.3+ & 5+)
This file contains hidden or 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
import { NgModule } from '@angular/core'; | |
import { CommonModule } from '@angular/common'; | |
import { JWT_OPTIONS, JwtInterceptor, JwtModule } from '@auth0/angular-jwt'; | |
import { AuthorizationService } from './authorization.service'; | |
import { environment } from '../../environments/environment'; | |
import { HTTP_INTERCEPTORS, HttpClientModule } from '@angular/common/http'; | |
import { RefreshTokenInterceptor } from './refresh-token-interceptor'; | |
function jwtOptionsFactory (authorizationService: AuthorizationService) { | |
return { | |
tokenGetter: () => { | |
return authorizationService.getAccessToken(); | |
}, | |
blacklistedRoutes: [`${environment.apiBaseUrl}/login-check`] | |
}; | |
} | |
@NgModule({ | |
imports: [ | |
CommonModule, | |
HttpClientModule, | |
JwtModule.forRoot({ | |
jwtOptionsProvider: { | |
provide: JWT_OPTIONS, | |
useFactory: jwtOptionsFactory, | |
deps: [AuthorizationService] | |
} | |
}) | |
], | |
providers: [ | |
AuthorizationService, | |
JwtInterceptor, // Providing JwtInterceptor allow to inject JwtInterceptor manually into RefreshTokenInterceptor | |
{ | |
provide: HTTP_INTERCEPTORS, | |
useExisting: JwtInterceptor, | |
multi: true | |
}, | |
{ | |
provide: HTTP_INTERCEPTORS, | |
useClass: RefreshTokenInterceptor, | |
multi: true | |
} | |
], | |
declarations: [] | |
}) | |
export class ApiModule { | |
} |
This file contains hidden or 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
import { HttpErrorResponse, HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; | |
import { Injectable } from '@angular/core'; | |
import { Observable, throwError } from 'rxjs'; | |
import { catchError, mergeMap } from 'rxjs/operators'; | |
import { AuthorizationService } from './authorization.service'; | |
import { JwtInterceptor } from '@auth0/angular-jwt'; | |
@Injectable() | |
export class RefreshTokenInterceptor implements HttpInterceptor { | |
constructor (private authorizationService: AuthorizationService, private jwtInterceptor: JwtInterceptor) { | |
} | |
intercept (req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { | |
if (this.jwtInterceptor.isWhitelistedDomain(req) && !this.jwtInterceptor.isBlacklistedRoute(req)) { | |
return next.handle(req).pipe( | |
catchError((err) => { | |
const errorResponse = err as HttpErrorResponse; | |
if (errorResponse.status === 401 && errorResponse.error.message === 'Expired JWT Token') { | |
return this.authorizationService.refresh().pipe(mergeMap(() => { | |
return this.jwtInterceptor.intercept(req, next); | |
})); | |
} | |
return throwError(err); | |
})); | |
} else { | |
return next.handle(req); | |
} | |
} | |
} |
Thanks for this very neat solution. I have been using it for a while. However lately i have noticed if the browser sits idle for a little while and then some API calls are sent - sometimes they are missing the authorization header (checked it from server logs). I opened an issue with the jwt token repo. @Toilal Would it be possible for you to check that thread out ( auth0/angular2-jwt#763 )?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is not a server side issue. If the token is expired any request until the first token response will request a new token request. We need a refreshSubject or something similar to let other consecutive request know a token is pending some how.