Skip to content

Three ways to refresh token with Angular Http Interceptor

Notifications You must be signed in to change notification settings

kingkarki/angular-refresh-token

 
 

Repository files navigation

Angular refresh token interceptors 🔂

Three ways to refresh token with Angular Http Interceptor

  • Brute force solution with tokenRefreshed$ BehaviorSubject as a semaphore
  • Using caught parameter in catchError rxjs operator to retry request failed request
  • Using retryWhen operator

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 { }

About

Three ways to refresh token with Angular Http Interceptor

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • HTML 53.4%
  • TypeScript 42.5%
  • JavaScript 3.9%
  • CSS 0.2%