2025-12-18 13:50:39 +08:00
|
|
|
# Authentication Views
|
|
|
|
|
|
|
|
|
|
This directory contains Vue 3 authentication views for the Sub2API frontend application.
|
|
|
|
|
|
|
|
|
|
## Components
|
|
|
|
|
|
|
|
|
|
### LoginView.vue
|
|
|
|
|
|
|
|
|
|
Login page for existing users to authenticate.
|
|
|
|
|
|
|
|
|
|
**Features:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Username and password inputs with validation
|
|
|
|
|
- Remember me checkbox for persistent sessions
|
|
|
|
|
- Form validation with real-time error display
|
|
|
|
|
- Loading state during authentication
|
|
|
|
|
- Error message display for failed login attempts
|
|
|
|
|
- Redirect to dashboard on successful login
|
|
|
|
|
- Link to registration page for new users
|
|
|
|
|
|
|
|
|
|
**Usage:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
```vue
|
|
|
|
|
<template>
|
|
|
|
|
<LoginView />
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2025-12-25 08:41:36 -08:00
|
|
|
import { LoginView } from '@/views/auth'
|
2025-12-18 13:50:39 +08:00
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Route:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Path: `/login`
|
|
|
|
|
- Name: `Login`
|
|
|
|
|
- Meta: `{ requiresAuth: false }`
|
|
|
|
|
|
|
|
|
|
**Validation Rules:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Username: Required, minimum 3 characters
|
|
|
|
|
- Password: Required, minimum 6 characters
|
|
|
|
|
|
|
|
|
|
**Behavior:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Calls `authStore.login()` with credentials
|
|
|
|
|
- Shows success toast on successful login
|
|
|
|
|
- Shows error toast and inline error message on failure
|
|
|
|
|
- Redirects to `/dashboard` or intended route from query parameter
|
|
|
|
|
- Redirects authenticated users away from login page
|
|
|
|
|
|
|
|
|
|
### RegisterView.vue
|
|
|
|
|
|
|
|
|
|
Registration page for new users to create accounts.
|
|
|
|
|
|
|
|
|
|
**Features:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Username, email, password, and confirm password inputs
|
|
|
|
|
- Comprehensive form validation
|
|
|
|
|
- Password strength requirements (8+ characters, letters + numbers)
|
|
|
|
|
- Email format validation with regex
|
|
|
|
|
- Password match validation
|
|
|
|
|
- Loading state during registration
|
|
|
|
|
- Error message display for failed registration
|
|
|
|
|
- Redirect to dashboard on success
|
|
|
|
|
- Link to login page for existing users
|
|
|
|
|
|
|
|
|
|
**Usage:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
```vue
|
|
|
|
|
<template>
|
|
|
|
|
<RegisterView />
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
2025-12-25 08:41:36 -08:00
|
|
|
import { RegisterView } from '@/views/auth'
|
2025-12-18 13:50:39 +08:00
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
**Route:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Path: `/register`
|
|
|
|
|
- Name: `Register`
|
|
|
|
|
- Meta: `{ requiresAuth: false }`
|
|
|
|
|
|
|
|
|
|
**Validation Rules:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Username:
|
|
|
|
|
- Required
|
|
|
|
|
- 3-50 characters
|
|
|
|
|
- Only letters, numbers, underscores, and hyphens
|
|
|
|
|
- Email:
|
|
|
|
|
- Required
|
|
|
|
|
- Valid email format (RFC 5322 regex)
|
|
|
|
|
- Password:
|
|
|
|
|
- Required
|
|
|
|
|
- Minimum 8 characters
|
|
|
|
|
- Must contain at least one letter and one number
|
|
|
|
|
- Confirm Password:
|
|
|
|
|
- Required
|
|
|
|
|
- Must match password
|
|
|
|
|
|
|
|
|
|
**Behavior:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Calls `authStore.register()` with user data
|
|
|
|
|
- Shows success toast on successful registration
|
|
|
|
|
- Shows error toast and inline error message on failure
|
|
|
|
|
- Redirects to `/dashboard` after successful registration
|
|
|
|
|
- Redirects authenticated users away from register page
|
|
|
|
|
|
|
|
|
|
## Architecture
|
|
|
|
|
|
|
|
|
|
### Component Structure
|
|
|
|
|
|
|
|
|
|
Both views follow a consistent structure:
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
<template>
|
|
|
|
|
<AuthLayout>
|
|
|
|
|
<div class="space-y-6">
|
|
|
|
|
<!-- Title -->
|
|
|
|
|
<!-- Form -->
|
|
|
|
|
<!-- Error Message -->
|
|
|
|
|
<!-- Submit Button -->
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<template #footer>
|
|
|
|
|
<!-- Footer Links -->
|
|
|
|
|
</template>
|
|
|
|
|
</AuthLayout>
|
|
|
|
|
</template>
|
|
|
|
|
|
|
|
|
|
<script setup lang="ts">
|
|
|
|
|
// Imports
|
|
|
|
|
// State
|
|
|
|
|
// Validation
|
|
|
|
|
// Form Handlers
|
|
|
|
|
</script>
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### State Management
|
|
|
|
|
|
|
|
|
|
Both views use:
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- `useAuthStore()` - For authentication actions (login, register)
|
|
|
|
|
- `useAppStore()` - For toast notifications and UI feedback
|
|
|
|
|
- `useRouter()` - For navigation and redirects
|
|
|
|
|
|
|
|
|
|
### Validation Strategy
|
|
|
|
|
|
|
|
|
|
**Client-side Validation:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Real-time validation on form submission
|
|
|
|
|
- Field-level error messages
|
|
|
|
|
- Comprehensive validation rules
|
|
|
|
|
- TypeScript type safety
|
|
|
|
|
|
|
|
|
|
**Server-side Validation:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Backend API validates all inputs
|
|
|
|
|
- Error responses handled gracefully
|
|
|
|
|
- User-friendly error messages displayed
|
|
|
|
|
|
|
|
|
|
### Styling
|
|
|
|
|
|
|
|
|
|
**Design System:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- TailwindCSS utility classes
|
|
|
|
|
- Consistent color scheme (indigo primary)
|
|
|
|
|
- Responsive design
|
|
|
|
|
- Accessible form controls
|
|
|
|
|
- Loading states with spinner animations
|
|
|
|
|
|
|
|
|
|
**Visual Feedback:**
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Red border on invalid fields
|
|
|
|
|
- Error messages below inputs
|
|
|
|
|
- Global error banner for API errors
|
|
|
|
|
- Success toasts on completion
|
|
|
|
|
- Loading spinner on submit button
|
|
|
|
|
|
|
|
|
|
## Dependencies
|
|
|
|
|
|
|
|
|
|
### Components
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- `AuthLayout` - Layout wrapper for auth pages from `@/components/layout`
|
|
|
|
|
|
|
|
|
|
### Stores
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- `authStore` - Authentication state management from `@/stores/auth`
|
|
|
|
|
- `appStore` - Application state and toasts from `@/stores/app`
|
|
|
|
|
|
|
|
|
|
### Libraries
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Vue 3 Composition API
|
|
|
|
|
- Vue Router for navigation
|
|
|
|
|
- Pinia for state management
|
|
|
|
|
- TypeScript for type safety
|
|
|
|
|
|
|
|
|
|
## Usage Examples
|
|
|
|
|
|
|
|
|
|
### Basic Login Flow
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// User enters credentials
|
2025-12-25 08:41:36 -08:00
|
|
|
formData.username = 'john_doe'
|
|
|
|
|
formData.password = 'SecurePass123'
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
// Submit form
|
2025-12-25 08:41:36 -08:00
|
|
|
await handleLogin()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
// On success:
|
|
|
|
|
// - authStore.login() called
|
|
|
|
|
// - Token and user stored
|
|
|
|
|
// - Success toast shown
|
|
|
|
|
// - Redirected to /dashboard
|
|
|
|
|
|
|
|
|
|
// On error:
|
|
|
|
|
// - Error message displayed
|
|
|
|
|
// - Error toast shown
|
|
|
|
|
// - Form remains editable
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Basic Registration Flow
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// User enters registration data
|
2025-12-25 08:41:36 -08:00
|
|
|
formData.username = 'jane_smith'
|
|
|
|
|
formData.email = 'jane@example.com'
|
|
|
|
|
formData.password = 'SecurePass123'
|
|
|
|
|
formData.confirmPassword = 'SecurePass123'
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
// Submit form
|
2025-12-25 08:41:36 -08:00
|
|
|
await handleRegister()
|
2025-12-18 13:50:39 +08:00
|
|
|
|
|
|
|
|
// On success:
|
|
|
|
|
// - authStore.register() called
|
|
|
|
|
// - Token and user stored
|
|
|
|
|
// - Success toast shown
|
|
|
|
|
// - Redirected to /dashboard
|
|
|
|
|
|
|
|
|
|
// On error:
|
|
|
|
|
// - Error message displayed
|
|
|
|
|
// - Error toast shown
|
|
|
|
|
// - Form remains editable
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Error Handling
|
|
|
|
|
|
|
|
|
|
### Client-side Errors
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// Validation errors
|
2025-12-25 08:41:36 -08:00
|
|
|
errors.username = 'Username must be at least 3 characters'
|
|
|
|
|
errors.email = 'Please enter a valid email address'
|
|
|
|
|
errors.password = 'Password must be at least 8 characters with letters and numbers'
|
|
|
|
|
errors.confirmPassword = 'Passwords do not match'
|
2025-12-18 13:50:39 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
### Server-side Errors
|
|
|
|
|
|
|
|
|
|
```typescript
|
|
|
|
|
// API error responses
|
|
|
|
|
{
|
|
|
|
|
response: {
|
|
|
|
|
data: {
|
|
|
|
|
detail: 'Username already exists'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Displayed as:
|
2025-12-25 08:41:36 -08:00
|
|
|
errorMessage.value = 'Username already exists'
|
|
|
|
|
appStore.showError('Username already exists')
|
2025-12-18 13:50:39 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
|
|
## Accessibility
|
|
|
|
|
|
|
|
|
|
- Semantic HTML elements (`<label>`, `<input>`, `<button>`)
|
|
|
|
|
- Proper `for` attributes on labels
|
|
|
|
|
- ARIA attributes for loading states
|
|
|
|
|
- Keyboard navigation support
|
|
|
|
|
- Focus management
|
|
|
|
|
- Error announcements
|
|
|
|
|
- Sufficient color contrast
|
|
|
|
|
|
|
|
|
|
## Testing Considerations
|
|
|
|
|
|
|
|
|
|
### Unit Tests
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Form validation logic
|
|
|
|
|
- Error handling
|
|
|
|
|
- State management
|
|
|
|
|
- Router navigation
|
|
|
|
|
|
|
|
|
|
### Integration Tests
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Full login flow
|
|
|
|
|
- Full registration flow
|
|
|
|
|
- Error scenarios
|
|
|
|
|
- Redirect behavior
|
|
|
|
|
|
|
|
|
|
### E2E Tests
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- Complete user journeys
|
|
|
|
|
- Form interactions
|
|
|
|
|
- API integration
|
|
|
|
|
- Success/error states
|
|
|
|
|
|
|
|
|
|
## Future Enhancements
|
|
|
|
|
|
|
|
|
|
Potential improvements:
|
2025-12-25 08:41:36 -08:00
|
|
|
|
2025-12-18 13:50:39 +08:00
|
|
|
- OAuth/SSO integration (Google, GitHub)
|
|
|
|
|
- Two-factor authentication (2FA)
|
|
|
|
|
- Password strength meter
|
|
|
|
|
- Email verification flow
|
|
|
|
|
- Forgot password functionality
|
|
|
|
|
- Social login buttons
|
|
|
|
|
- CAPTCHA integration
|
|
|
|
|
- Session timeout warnings
|
|
|
|
|
- Password visibility toggle
|
|
|
|
|
- Autofill support enhancement
|
|
|
|
|
|
|
|
|
|
## Security Considerations
|
|
|
|
|
|
|
|
|
|
- Passwords are never logged or displayed
|
|
|
|
|
- HTTPS required in production
|
|
|
|
|
- JWT tokens stored securely in localStorage
|
|
|
|
|
- CORS protection on API
|
|
|
|
|
- XSS protection with Vue's automatic escaping
|
|
|
|
|
- CSRF protection with token-based auth
|
|
|
|
|
- Rate limiting on backend API
|
|
|
|
|
- Input sanitization
|
|
|
|
|
- Secure password requirements
|
|
|
|
|
|
|
|
|
|
## Performance
|
|
|
|
|
|
|
|
|
|
- Lazy-loaded routes
|
|
|
|
|
- Minimal bundle size
|
|
|
|
|
- Fast initial render
|
|
|
|
|
- Optimized re-renders with reactive refs
|
|
|
|
|
- No unnecessary watchers
|
|
|
|
|
- Efficient form validation
|
|
|
|
|
|
|
|
|
|
## Browser Support
|
|
|
|
|
|
|
|
|
|
- Modern browsers (Chrome, Firefox, Safari, Edge)
|
|
|
|
|
- ES2015+ required
|
|
|
|
|
- Flexbox and CSS Grid
|
|
|
|
|
- Tailwind CSS utilities
|
|
|
|
|
- Vue 3 runtime
|
|
|
|
|
|
|
|
|
|
## Related Documentation
|
|
|
|
|
|
|
|
|
|
- [Auth Store Documentation](/src/stores/README.md#auth-store)
|
|
|
|
|
- [AuthLayout Component](/src/components/layout/README.md#authlayout)
|
|
|
|
|
- [Router Configuration](/src/router/index.ts)
|
|
|
|
|
- [API Documentation](/src/api/README.md#authentication)
|
|
|
|
|
- [Type Definitions](/src/types/index.ts)
|