4.0 KiB
4.0 KiB
Auth Service Integration
The blog editor is integrated with the existing auth service located at G:\LivingAi\GITTEA_RPO\auth.
How It Works
Backend Integration
The blog editor backend validates JWT tokens by calling the auth service's /auth/validate-token endpoint:
- Client sends request with
Authorization: Bearer <token>header - Blog editor backend middleware (
middleware/auth.js) extracts the token - Middleware calls
POST /auth/validate-tokenon the auth service - Auth service validates the token and returns user info
- Blog editor backend sets
req.userand continues processing
Frontend Integration
The frontend uses the auth service directly for authentication:
-
Login Flow:
- User enters phone number
- Frontend calls
POST /auth/request-otpon auth service - User enters OTP
- Frontend calls
POST /auth/verify-otpon auth service - Auth service returns
access_tokenandrefresh_token - Frontend stores tokens in localStorage
-
API Requests:
- Frontend includes
Authorization: Bearer <access_token>header - Blog editor backend validates token via auth service
- If token expires, frontend automatically refreshes using
refresh_token
- Frontend includes
Configuration
Backend (.env)
AUTH_SERVICE_URL=http://localhost:3000
Frontend (.env)
VITE_AUTH_API_URL=http://localhost:3000
Token Storage
access_token- Stored in localStorage, used for API requestsrefresh_token- Stored in localStorage, used to refresh access tokenuser- User object stored in localStorage
Authentication Flow
┌─────────┐ ┌──────────────┐ ┌─────────────┐
│ Client │────────▶│ Auth Service │────────▶│ Blog Editor │
│ │ │ │ │ Backend │
└─────────┘ └──────────────┘ └─────────────┘
│ │ │
│ 1. Request OTP │ │
│◀─────────────────────│ │
│ │ │
│ 2. Verify OTP │ │
│─────────────────────▶│ │
│ 3. Get Tokens │ │
│◀─────────────────────│ │
│ │ │
│ 4. API Request │ │
│──────────────────────────────────────────────▶│
│ │ 5. Validate Token │
│ │◀───────────────────────│
│ │ 6. User Info │
│ │───────────────────────▶│
│ 7. Response │ │
│◀──────────────────────────────────────────────│
Benefits
- Single Source of Truth: All authentication handled by one service
- Consistent Security: Same JWT validation across all services
- Token Rotation: Auth service handles token refresh and rotation
- User Management: Centralized user management in auth service
- Guest Support: Auth service supports guest users
Notes
- The blog editor backend does NOT handle user registration/login
- All authentication is delegated to the auth service
- The blog editor only validates tokens, not creates them
- Phone/OTP authentication is used (not email/password)