-
-
Save argentinaluiz/d5035df95c8b5cb2ec04084688cad91c 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); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment