Auth
This document provides a detailed overview of the Intecode Common Auth Module, which is responsible for handling user authentication and authorization.
Functionality
The Auth module ensures secure access to the system by managing user login, registration, password recovery, and session handling. It includes role-based access controls and integrates with OAuth providers.
Auth Provider
The AuthProvider
component and AuthContext
provide authentication management and user context for the application. It handles storing and managing authentication tokens, user information, and team-related updates.
Key Features:
1. State Management:
token
&refreshToken
: Stored in localStorage using theuseLocalStorage
hook. These hold the access token and refresh token for authenticated sessions.currentUser
: Holds the currently authenticated user's data.
2. Functions:
fetchUser
:Fetches the current user data from the API using
userService.getUser()
.Updates user roles and checks if the user has a team. If no team exists, it navigates to the team creation page.
setAuth
: Stores the access token and refresh token in localStorage.logout
:Logs the user out by deactivating their membership (if applicable) and clears the authentication tokens.
Redirects to the sign-in page.
changeTeam
:Switches the active team by calling the
authService.changeTeam
API and updating tokens.Re-fetches the user data after changing the team.
3. Context Usage:
AuthContext.Provider
: Provides the authentication-related values and functions (token
,setAuth
,logout
, etc.) to the component tree.useAuth
: Hook to access theAuthContext
values and functions within any component.
Usage Example:
To access the authentication context:
This structure allows centralized control of authentication, token management, and user/team updates throughout the app.
Components
Components Necessary for Optimal Functioning of the Auth Module
Auth Guard
The AuthGuard
component is a route protection mechanism that checks if a user is authenticated before granting access to specific routes.
Key Features:
Authentication Check:
It verifies if the user is on an "auth" route (e.g.,
/auth/sign-in
) by checking the URL path.If the user is already authenticated (i.e., has valid tokens stored), they are redirected to the dashboard route.
Route Protection:
If the user is accessing a non-auth route and doesn't have valid tokens (
TOKEN
andREFRESH_TOKEN
in localStorage), they are redirected to the sign-in page (auth/sign-in
).
GetUser Decorator
This decorator retrieves user data parsed by JwtAuthenticationGuard
Auth Flow Schema
Last updated