Spaces:
Sleeping
Sleeping
File size: 11,429 Bytes
9eafd9f |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 |
# Quickstart: Authentication & API Security
**Feature**: 001-auth-security
**Date**: 2026-01-09
## Overview
This guide provides step-by-step instructions for setting up and testing the authentication and API security feature. Follow these steps to configure Better Auth on the frontend and JWT verification on the backend.
## Prerequisites
- Node.js 18+ and npm installed
- Python 3.11+ installed
- PostgreSQL database (Neon Serverless) accessible
- Git repository cloned
- Existing task CRUD functionality working (from Spec 001-task-crud)
## Setup Instructions
### 1. Environment Configuration
#### Backend Environment Variables
Create or update `backend/.env`:
```bash
# Database
DATABASE_URL=postgresql://user:password@host:5432/database
# Authentication
BETTER_AUTH_SECRET=your-secret-key-min-32-characters-long-and-random
# Application
APP_NAME=Task CRUD API
DEBUG=True
CORS_ORIGINS=http://localhost:3000
```
**Important**: Generate a strong random secret for `BETTER_AUTH_SECRET`:
```bash
# Generate a secure random secret (32+ characters)
python -c "import secrets; print(secrets.token_urlsafe(32))"
```
#### Frontend Environment Variables
Create or update `frontend/.env.local`:
```bash
# API Configuration
NEXT_PUBLIC_API_URL=http://localhost:8000
# Authentication (MUST match backend secret)
BETTER_AUTH_SECRET=your-secret-key-min-32-characters-long-and-random
# Better Auth Database (optional - uses same as backend)
DATABASE_URL=postgresql://user:password@host:5432/database
```
**Critical**: The `BETTER_AUTH_SECRET` must be **identical** in both frontend and backend.
---
### 2. Install Dependencies
#### Backend Dependencies
```bash
cd backend
# Add new dependencies to requirements.txt
echo "PyJWT==2.8.0" >> requirements.txt
echo "passlib[bcrypt]==1.7.4" >> requirements.txt
echo "python-multipart==0.0.6" >> requirements.txt
# Install all dependencies
pip install -r requirements.txt
```
#### Frontend Dependencies
```bash
cd frontend
# Install Better Auth
npm install better-auth @better-auth/react
# Install development dependencies (if not already installed)
npm install --save-dev @types/node @types/react @types/react-dom
```
---
### 3. Database Migration
#### Run Migration to Add Password Field
```bash
cd backend
# Create migration
alembic revision --autogenerate -m "Add password_hash to users"
# Review the generated migration file in alembic/versions/
# Ensure it adds password_hash column to users table
# Apply migration
alembic upgrade head
```
**Expected Migration**:
```python
def upgrade():
op.add_column('users', sa.Column('password_hash', sa.String(255), nullable=False))
def downgrade():
op.drop_column('users', 'password_hash')
```
---
### 4. Backend Implementation
#### Create Security Module
Create `backend/src/core/security.py`:
```python
import jwt
from datetime import datetime, timedelta
from passlib.context import CryptContext
from fastapi import HTTPException, status
from src.core.config import settings
# Password hashing
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
def hash_password(password: str) -> str:
"""Hash a password using bcrypt."""
return pwd_context.hash(password)
def verify_password(plain_password: str, hashed_password: str) -> bool:
"""Verify a password against its hash."""
return pwd_context.verify(plain_password, hashed_password)
def create_jwt_token(user_id: int, email: str) -> str:
"""Create a JWT token for a user."""
payload = {
"sub": str(user_id),
"email": email,
"iat": datetime.utcnow(),
"exp": datetime.utcnow() + timedelta(days=7),
"iss": "better-auth"
}
return jwt.encode(payload, settings.BETTER_AUTH_SECRET, algorithm="HS256")
def verify_jwt_token(token: str) -> dict:
"""Verify and decode a JWT token."""
try:
payload = jwt.decode(
token,
settings.BETTER_AUTH_SECRET,
algorithms=["HS256"]
)
return payload
except jwt.ExpiredSignatureError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Token has expired"
)
except jwt.InvalidTokenError:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token"
)
```
#### Update Dependencies
Modify `backend/src/api/deps.py`:
```python
from fastapi import Depends, HTTPException, status
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
from sqlmodel import Session
from src.core.database import get_session
from src.core.security import verify_jwt_token
security = HTTPBearer()
def get_db() -> Generator[Session, None, None]:
"""Get database session dependency."""
yield from get_session()
def get_current_user(
credentials: HTTPAuthorizationCredentials = Depends(security)
) -> int:
"""
Get current user ID from JWT token.
Extracts and verifies JWT from Authorization header.
"""
token = credentials.credentials
payload = verify_jwt_token(token)
user_id = payload.get("sub")
if not user_id:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Invalid token payload"
)
return int(user_id)
```
#### Update Configuration
Modify `backend/src/core/config.py`:
```python
class Settings(BaseSettings):
# ... existing fields ...
# Authentication
BETTER_AUTH_SECRET: str # Remove Optional, make required
JWT_ALGORITHM: str = "HS256"
JWT_EXPIRATION_DAYS: int = 7
```
---
### 5. Frontend Implementation
#### Configure Better Auth
Create `frontend/src/lib/auth.ts`:
```typescript
import { betterAuth } from "better-auth"
import { jwt } from "better-auth/plugins"
export const auth = betterAuth({
database: {
provider: "postgres",
url: process.env.DATABASE_URL!,
},
emailAndPassword: {
enabled: true,
requireEmailVerification: false,
},
plugins: [
jwt({
secret: process.env.BETTER_AUTH_SECRET!,
expiresIn: "7d",
})
],
secret: process.env.BETTER_AUTH_SECRET!,
})
```
#### Update API Client
Modify `frontend/src/lib/api.ts`:
```typescript
import { auth } from './auth'
async function fetchAPI<T>(
endpoint: string,
options: RequestInit = {}
): Promise<T> {
const session = await auth()
const token = session?.token
const url = `${API_BASE_URL}${endpoint}`
const response = await fetch(url, {
...options,
headers: {
'Content-Type': 'application/json',
...(token && { 'Authorization': `Bearer ${token}` }),
...options.headers,
},
})
if (response.status === 401) {
// Redirect to login
if (typeof window !== 'undefined') {
window.location.href = '/auth/signin'
}
throw new APIError('Authentication required', 401)
}
if (!response.ok) {
const errorData: ErrorResponse = await response.json().catch(() => ({
detail: 'An unexpected error occurred',
}))
throw new APIError(
errorData.detail,
response.status,
errorData.error_code,
errorData.field_errors
)
}
return response.json()
}
```
---
### 6. Testing
#### Backend Tests
```bash
cd backend
# Test authentication endpoints
pytest tests/test_auth.py -v
# Test JWT protection on task endpoints
pytest tests/test_tasks.py -v
# Run all tests
pytest -v
```
#### Manual Testing with curl
**Sign Up**:
```bash
curl -X POST http://localhost:8000/api/auth/signup \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "SecurePass123!",
"name": "Test User"
}'
```
**Sign In**:
```bash
curl -X POST http://localhost:8000/api/auth/signin \
-H "Content-Type: application/json" \
-d '{
"email": "test@example.com",
"password": "SecurePass123!"
}'
```
**Access Protected Endpoint**:
```bash
# Save token from signin response
TOKEN="your-jwt-token-here"
curl -X GET http://localhost:8000/api/tasks \
-H "Authorization: Bearer $TOKEN"
```
**Test Unauthorized Access**:
```bash
# Should return 401
curl -X GET http://localhost:8000/api/tasks
```
---
### 7. Running the Application
#### Start Backend
```bash
cd backend
uvicorn src.main:app --reload --port 8000
```
#### Start Frontend
```bash
cd frontend
npm run dev
```
#### Access Application
- Frontend: http://localhost:3000
- Backend API: http://localhost:8000
- API Docs: http://localhost:8000/docs
---
## Verification Checklist
### Backend Verification
- [ ] `BETTER_AUTH_SECRET` is set in backend/.env
- [ ] PyJWT, passlib, python-multipart installed
- [ ] Database migration applied (password_hash column exists)
- [ ] `src/core/security.py` created with JWT functions
- [ ] `src/api/deps.py` updated with JWT verification
- [ ] Backend starts without errors: `uvicorn src.main:app --reload`
- [ ] API docs accessible at http://localhost:8000/docs
### Frontend Verification
- [ ] `BETTER_AUTH_SECRET` matches backend (identical value)
- [ ] better-auth and @better-auth/react installed
- [ ] `src/lib/auth.ts` created with Better Auth config
- [ ] `src/lib/api.ts` updated to include JWT in headers
- [ ] Frontend starts without errors: `npm run dev`
- [ ] Can access http://localhost:3000
### Integration Verification
- [ ] User can sign up with email/password
- [ ] User can sign in and receive JWT token
- [ ] Authenticated requests to /api/tasks succeed
- [ ] Unauthenticated requests to /api/tasks return 401
- [ ] User can only see their own tasks
- [ ] Token expires after 7 days (test with modified exp claim)
---
## Troubleshooting
### "Invalid token" errors
**Cause**: BETTER_AUTH_SECRET mismatch between frontend and backend
**Solution**: Verify both .env files have identical BETTER_AUTH_SECRET values
### "Token has expired" immediately
**Cause**: System clock skew or incorrect exp claim
**Solution**: Check system time, verify token exp claim is 7 days in future
### "Not authenticated" on all requests
**Cause**: Token not being included in Authorization header
**Solution**: Check frontend api.ts includes `Authorization: Bearer ${token}` header
### Database connection errors
**Cause**: DATABASE_URL incorrect or database not accessible
**Solution**: Verify DATABASE_URL format and database is running
### Import errors for better-auth
**Cause**: Package not installed or wrong version
**Solution**: Run `npm install better-auth @better-auth/react` in frontend directory
---
## Next Steps
After completing this setup:
1. Run `/sp.tasks` to generate implementation tasks
2. Implement authentication endpoints (signup, signin)
3. Implement JWT verification middleware
4. Update task endpoints to require authentication
5. Create frontend auth pages (signin, signup)
6. Test end-to-end authentication flow
7. Deploy to production with HTTPS enabled
---
## Security Reminders
- ✅ Never commit .env files to git
- ✅ Use HTTPS in production
- ✅ Rotate BETTER_AUTH_SECRET periodically
- ✅ Use strong passwords (min 8 chars, complexity requirements)
- ✅ Monitor for suspicious authentication attempts
- ✅ Keep dependencies updated for security patches
---
## Reference Documentation
- Better Auth: https://better-auth.com/docs
- PyJWT: https://pyjwt.readthedocs.io/
- FastAPI Security: https://fastapi.tiangolo.com/tutorial/security/
- JWT.io: https://jwt.io/ (for debugging tokens)
|