⚙️ Step 8: Core Module – Low Level Design (LLD)
This document describes the core module that provides app-wide singleton services, route guards, HTTP interceptors, and global utilities.
📦 Module Structure
src/
├── app/
│ └── core/
│ ├── services/
│ │ ├── auth.service.ts
│ │ ├── token.service.ts
│ │ ├── logger.service.ts
│ │ └── error-handler.service.ts
│ ├── guards/
│ │ └── auth.guard.ts
│ ├── interceptors/
│ │ ├── auth.interceptor.ts
│ │ └── error.interceptor.ts
│ ├── core.module.ts
🧱 Service Breakdown
🔐 AuthService
- Handles authentication logic: login, logout, token refresh
- Stores JWT tokens securely via TokenService
- Provides current user info and authentication status
🔑 TokenService
- Manages storing and retrieving JWT tokens from
localStorageorsessionStorage - Provides token expiration checks and refresh triggers
📝 LoggerService
- Centralized logging for debugging and error tracking
- Supports different log levels (info, warn, error)
- Could integrate with external logging services
🚨 ErrorHandlerService
- Global error handler capturing uncaught exceptions
- Displays user-friendly error messages or redirects
- Reports errors to external monitoring (optional)
🚦 Guards
AuthGuard
- Protects routes that require authentication
- Redirects unauthenticated users to login page
@Injectable({ providedIn: "root" })
export class AuthGuard implements CanActivate {
constructor(private authService: AuthService, private router: Router) {}
canActivate(): boolean {
if (this.authService.isLoggedIn()) {
return true;
}
this.router.navigate(["/login"]);
return false;
}
}
🛡️ Interceptors
AuthInterceptor
- Attaches JWT token to outgoing HTTP requests' Authorization header
ErrorInterceptor
- Intercepts HTTP errors globally
- Handles common status codes (401, 403, 500) with proper user notifications or logout
📐 Module Definition
@NgModule({
providers: [
AuthService,
TokenService,
LoggerService,
ErrorHandlerService,
AuthGuard,
{
provide: HTTP_INTERCEPTORS,
useClass: AuthInterceptor,
multi: true,
},
{
provide: HTTP_INTERCEPTORS,
useClass: ErrorInterceptor,
multi: true,
},
],
})
export class CoreModule {}
✅ Responsibilities Summary
| Part | Responsibility |
|---|---|
| AuthService | Authentication management |
| TokenService | JWT token storage and refresh |
| LoggerService | Application logging |
| ErrorHandlerService | Global error handling |
| AuthGuard | Route protection |
| AuthInterceptor | JWT token injection in HTTP requests |
| ErrorInterceptor | Global HTTP error handling |