angular-refresh-token
angular-refresh-token copied to clipboard
Three ways to refresh token with Angular Http Interceptor
Angular refresh token interceptors :repeat_one:
Three ways to refresh token with Angular Http Interceptor
- Brute force solution with
tokenRefreshed$BehaviorSubject as a semaphore - Using
caughtparameter incatchErrorrxjs operator to retry request failed request - Using
retryWhenoperator
Features
- ✅ Refresh token only once for multiple requests
- ✅ Log out user if refreshToken failed
- ✅ Log out if user gets an error after first refreshing
- ✅ Queue all requests while token is being refreshed(except
BruteForceInterceptor) - ✅ Tests for all cases above
Usage
Add HttpInterceptor in providers section of your AppModule:
@NgModule({
imports: [
HttpClientModule
],
providers: [
AuthService,
{
provide: HTTP_INTERCEPTORS,
useClass: RetryWhenInterceptor,
multi: true
},
],
})
export class AuthModule { }