Skip to content

Instantly share code, notes, and snippets.

View SaeedAdam's full-sized avatar
🏠
Working from home

Saeed Adam SaeedAdam

🏠
Working from home
View GitHub Profile
@SaeedAdam
SaeedAdam / auth.interceptor.ts
Created May 17, 2025 13:06
An Angular HTTP interceptor that handles authentication and token refresh. It automatically attaches a Bearer token to outgoing requests, refreshes the token if it's about to expire, and retries the request after successful refresh. If the token refresh fails or the user is unauthorized, it redirects to the login page.
import { inject } from '@angular/core';
import {
HttpInterceptorFn,
HttpErrorResponse,
HttpRequest,
} from '@angular/common/http';
import { Router } from '@angular/router';
import { catchError, switchMap, throwError } from 'rxjs';
import { AuthService } from './auth.service';
import { isTokenExpiringSoon } from './utils/token.utils';