Authentication - gaulatti/autostrada-frontend GitHub Wiki
Overview
Autostrada uses AWS Cognito for authentication, providing a secure and scalable solution for user management. This document details the authentication flow implementation in the frontend application.
Authentication Components
Core Authentication Services
-
AWS Amplify Configuration
- Located in
app/root.tsx
- Configures Cognito user pool settings
- Sets up OAuth flows with appropriate redirect URIs
- Located in
-
Authentication Hooks
useAuthStatus
: Provides the current authentication stateuseLogout
: Handles user logout and session clearinguseSession
: Manages the user session and token refresh
-
Authentication Pages
- Login page: User sign-in interface
- Logout page: Session termination confirmation
- OAuth callback handler: Processes authentication responses
Authentication Flow
1. Initial Application Load
When the application first loads:
- The
root.tsx
component initializes AWS Amplify with the Cognito configuration. - The application checks for an existing authenticated session.
- If a valid session exists, the user is directed to the main application.
- If no session exists, the user is redirected to the login page.
2. Login Process
The login flow consists of:
- User navigates to the login page.
- User enters credentials or selects to sign in with an OAuth provider.
- AWS Cognito handles the authentication and returns tokens.
- The application stores the tokens and redirects to the callback URL.
- The callback handler processes the authentication response and updates the Redux store.
- Once authenticated, the user is redirected to the main application.
sequenceDiagram
participant User
participant Frontend
participant Cognito
User->>Frontend: Access application
Frontend->>Frontend: Check for existing session
alt No session
Frontend->>User: Redirect to login page
User->>Frontend: Enter credentials
Frontend->>Cognito: Authentication request
Cognito->>Cognito: Validate credentials
Cognito->>Frontend: Return authentication tokens
Frontend->>Frontend: Store tokens
Frontend->>User: Redirect to main application
else Session exists
Frontend->>User: Show main application
end
3. Session Management
After successful authentication:
- The application stores authentication tokens in secure storage.
- Redux sagas monitor the session state.
- The
useAuthStatus
hook provides authentication state to components. - Token refresh is handled automatically in the background.
- Session timeouts redirect users back to the login page.
4. Logout Process
When a user logs out:
- User clicks the logout button.
- The application calls the
useLogout
hook. - AWS Amplify clears the authentication tokens.
- Redux store authentication state is cleared.
- User is redirected to the logout confirmation page.
- After confirmation, the user is redirected back to the login page.
Token Handling
Token Types
- ID Token: Contains user identity information
- Access Token: Used for API authorization
- Refresh Token: Used to obtain new tokens when others expire
Security Considerations
- Tokens are stored securely and never exposed in the URL
- All tokens have appropriate expiration times
- Refresh tokens are used to maintain the session without requiring frequent logins
- HTTPS is enforced for all authentication operations
Protected Routes
Autostrada implements protected routes to secure application content:
- Route configuration checks the auth state before rendering protected components.
- Unauthorized access attempts are redirected to the login page.
- Role-based access control further restricts access based on user permissions.
Integration with Backend API
The authentication flow is integrated with the backend API:
- The authenticated user's access token is included in API requests.
- The backend validates tokens for API authorization.
- Invalid or expired tokens trigger a session refresh or logout.
OAuth Configuration
The OAuth configuration includes:
const config: ResourcesConfig = {
Auth: {
Cognito: {
userPoolId,
userPoolClientId,
loginWith: {
oauth: {
domain: userPoolDomain,
scopes: ['email', 'openid', 'profile', 'aws.cognito.signin.user.admin'],
redirectSignIn: ['http://localhost:5173', fqdn],
redirectSignOut: ['http://localhost:5173/logout', `${fqdn}/logout`],
responseType: 'code',
},
},
},
},
};
Troubleshooting
Common authentication issues and solutions:
- Session timeouts: Usually due to token expiration - refresh the token or re-login
- Redirect issues: Check OAuth configuration for proper redirect URIs
- API authorization failures: Verify token inclusion in API requests
- Cross-origin problems: Ensure CORS is properly configured
Implementation Recommendations
When working with the authentication system:
- Always use the provided hooks for authentication operations
- Don't store sensitive user information in local storage or Redux without encryption
- Keep OAuth configuration in sync between frontend, backend, and AWS Cognito
- Test authentication flows in both development and production environments