File size: 5,581 Bytes
1f70d93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Authentication API routes - HTTP endpoint handlers

Implements authentication endpoints for user signup, login, logout, and user info.
This module is the HTTP layer that delegates to the auth service layer.

Endpoints:
- POST /api/auth/signup - Register new user
- POST /api/auth/login - Authenticate existing user
- POST /api/auth/logout - Invalidate current session
- GET /api/auth/me - Get current user info
"""

from fastapi import APIRouter, status, Depends, Header, HTTPException
from sqlalchemy.ext.asyncio import AsyncSession

from models.auth import SignupRequest, LoginRequest, AuthResponse, UserResponse
from models.user import User
from services import auth_service, jwt_service
from core.database import get_db
from middleware.auth_middleware import get_current_user

# Create router with prefix and tags
router = APIRouter(prefix="/api/auth", tags=["authentication"])


@router.post("/signup", response_model=AuthResponse, status_code=status.HTTP_201_CREATED, summary="Register new user")
async def signup(data: SignupRequest, db: AsyncSession = Depends(get_db)):
    """
    Register a new user with email and password

    Args:
        data: Signup data (email and password)
        db: Async database session from dependency injection

    Returns:
        AuthResponse: JWT token and user information

    Raises:
        HTTPException 400: Invalid email or password format
        HTTPException 409: Email already exists
        HTTPException 500: Server error

    Business Rules:
        - Email must be valid format (validated by Pydantic EmailStr)
        - Password must be minimum 8 characters
        - Password is hashed with bcrypt before storage
        - JWT token generated and returned immediately
        - Session token stored in database
    """
    # Create user with hashed password
    user = await auth_service.create_user(data.email, data.password, db)

    # Generate JWT token
    access_token = jwt_service.create_access_token(user.id, user.email)

    # Store session token in database
    await jwt_service.create_session_token(user.id, access_token, db)

    # Return auth response
    return AuthResponse(
        access_token=access_token,
        token_type="bearer",
        expires_in=86400,  # 24 hours in seconds
        user=UserResponse(id=user.id, email=user.email)
    )


@router.post("/login", response_model=AuthResponse, summary="Authenticate existing user")
async def login(data: LoginRequest, db: AsyncSession = Depends(get_db)):
    """
    Authenticate user with email and password

    Args:
        data: Login data (email and password)
        db: Async database session from dependency injection

    Returns:
        AuthResponse: JWT token and user information

    Raises:
        HTTPException 401: Invalid credentials
        HTTPException 500: Server error

    Business Rules:
        - Generic error message prevents email enumeration
        - Password verified using bcrypt
        - New JWT token generated on each login
        - Session token stored in database
    """
    # Authenticate user
    user = await auth_service.authenticate_user(data.email, data.password, db)

    if user is None:
        from fastapi import HTTPException
        raise HTTPException(status_code=401, detail="Invalid credentials")

    # Generate JWT token
    access_token = jwt_service.create_access_token(user.id, user.email)

    # Store session token in database
    await jwt_service.create_session_token(user.id, access_token, db)

    # Return auth response
    return AuthResponse(
        access_token=access_token,
        token_type="bearer",
        expires_in=86400,  # 24 hours in seconds
        user=UserResponse(id=user.id, email=user.email)
    )


@router.post("/logout", summary="Invalidate current session")
async def logout(authorization: str = Header(None), db: AsyncSession = Depends(get_db)):
    """
    Logout user by invalidating session token

    Args:
        authorization: Authorization header containing Bearer token
        db: Async database session from dependency injection

    Returns:
        dict: Success message

    Raises:
        HTTPException 401: Missing or invalid Authorization header

    Business Rules:
        - Deletes session token from database
        - Frontend must also clear localStorage
        - Token cannot be used after logout
        - Idempotent (succeeds even if token already deleted)
    """
    if not authorization or not authorization.startswith("Bearer "):
        from fastapi import HTTPException
        raise HTTPException(status_code=401, detail="Missing or invalid Authorization header")

    token = authorization.split(" ")[1]

    # Delete session token from database
    await jwt_service.delete_session_token(token, db)

    return {"message": "Logged out successfully"}




@router.get("/me", response_model=UserResponse, summary="Get current user info")
async def get_current_user_info(current_user: User = Depends(get_current_user)):
    """
    Get information about the currently authenticated user

    Args:
        current_user: Authenticated user from JWT token (via auth middleware)

    Returns:
        UserResponse: User information (id and email)

    Raises:
        HTTPException 401: Missing or invalid authentication token

    Business Rules:
        - Requires valid JWT token in Authorization header
        - Returns basic user information (id and email)
        - Used by frontend to get user details after OAuth login
    """
    return UserResponse(id=current_user.id, email=current_user.email)