Overview
ScreenPulse uses JWT-based authentication with session storage for user management. The authentication system includes login/registration flows, token management, and route guards to protect sensitive pages.Key Components
- AuthService (
src/app/core/services/auth.service.ts) - Manages authentication state and session storage - UserService (
src/app/core/services/user.service.ts) - Handles API calls for login and registration - AuthGuard (
src/app/core/guards/auth.guard.ts) - Protects routes requiring authentication - AuthFormComponent (
src/app/pages/auth/components/auth-form/auth-form.component.ts) - Reusable login/register form
Authentication Flow
User submits credentials
The user enters their credentials in the Password requirements:
AuthFormComponent, which validates the form using Angular’s reactive forms with validation rules:src/app/pages/auth/components/auth-form/auth-form.component.ts
- At least 6 characters
- Contains at least one letter
- Contains at least one number
- Contains at least one special character
API authentication request
The
UserService sends credentials to the backend API:src/app/core/services/user.service.ts
Store session data
Upon successful authentication, the
AuthService stores the JWT token and user information in session storage:src/app/core/services/auth.service.ts
Session storage is used instead of local storage, meaning the session ends when the browser tab is closed.
JWT Token Management
Getting the Auth Token
TheAuthService provides methods to retrieve the stored JWT token:
src/app/core/services/auth.service.ts
Checking Authentication Status
Use observables to react to authentication state changes:src/app/core/services/auth.service.ts
Example: Checking Auth Before Action
src/app/pages/search/page/search.component.ts
Route Protection with AuthGuard
TheAuthGuard protects routes that require authentication:
src/app/core/guards/auth.guard.ts
Applying the Guard to Routes
Logout Flow
Logging out clears all session data and updates observables:src/app/core/services/auth.service.ts
Session Initialization
TheAuthService checks for existing sessions on initialization:
src/app/core/services/auth.service.ts
Form Types
TheAuthFormComponent supports both login and registration:
src/app/pages/auth/components/auth-form/auth-form.component.ts
Error Handling
Invalid Credentials
Invalid Credentials
When login fails due to invalid credentials, the The error is displayed via toast notification in the auth page component.
ErrorInterceptor transforms the HTTP error:User Already Exists
User Already Exists
During registration, if the email is already registered:
Network Connection Issues
Network Connection Issues
If the backend server is unreachable:
Token Expiration
Token Expiration
When a JWT token expires, the
AuthInterceptor automatically logs out the user:src/app/core/interceptors/auth.interceptor.ts
Best Practices
Use Observables
Subscribe to
isLoggedInObservable() instead of directly checking session storage to ensure reactivity across the application.Protect Sensitive Routes
Always apply
AuthGuard to routes that require authentication, such as favorites and user profile pages.Handle Token Expiration
The
AuthInterceptor automatically handles token expiration by logging out users and redirecting to login.Clear Session on Logout
Always use
authService.logOut() to ensure all session data is properly cleared and observables are updated.Related Files
src/app/core/services/auth.service.ts- Core authentication servicesrc/app/core/services/user.service.ts- User API servicesrc/app/core/guards/auth.guard.ts- Route protection guardsrc/app/core/interceptors/auth.interceptor.ts- JWT token interceptorsrc/app/core/interceptors/error.interceptor.ts- Error handling interceptorsrc/app/pages/auth/components/auth-form/auth-form.component.ts- Authentication form componentsrc/app/pages/auth/page/auth-page.component.ts- Authentication page component