|
| 1 | +from datetime import timedelta |
| 2 | +from typing import Any |
| 3 | + |
| 4 | +from fastapi import APIRouter, Depends, HTTPException, status |
| 5 | +from fastapi.security import OAuth2PasswordRequestForm |
| 6 | +from sqlmodel import Session |
| 7 | + |
| 8 | +from app.core import security |
| 9 | +from app.core.config import settings |
| 10 | +from app.db import get_db |
| 11 | +from app.models import User |
| 12 | +from app.schemas.auth import Token, UserLogin, UserRegister, UserOut, PasswordResetRequest, PasswordResetConfirm |
| 13 | +from app.crud import create_user, get_user_by_email, update_user |
| 14 | + |
| 15 | +router = APIRouter() |
| 16 | + |
| 17 | +@router.post("/signup", response_model=UserOut, status_code=status.HTTP_201_CREATED) |
| 18 | +def signup( |
| 19 | + *, |
| 20 | + db: Session = Depends(get_db), |
| 21 | + user_in: UserRegister, |
| 22 | +) -> Any: |
| 23 | + """ |
| 24 | + Create new user with email and password. |
| 25 | + """ |
| 26 | + # Check if user with this email already exists |
| 27 | + db_user = get_user_by_email(db, email=user_in.email) |
| 28 | + if db_user: |
| 29 | + raise HTTPException( |
| 30 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 31 | + detail="The user with this email already exists in the system.", |
| 32 | + ) |
| 33 | + |
| 34 | + # Create new user |
| 35 | + user = create_user(db, user_in) |
| 36 | + |
| 37 | + # TODO: Send verification email |
| 38 | + |
| 39 | + return user |
| 40 | + |
| 41 | +@router.post("/login", response_model=Token) |
| 42 | +def login( |
| 43 | + db: Session = Depends(get_db), |
| 44 | + form_data: OAuth2PasswordRequestForm = Depends(), |
| 45 | +) -> Any: |
| 46 | + """ |
| 47 | + OAuth2 compatible token login, get an access token for future requests. |
| 48 | + """ |
| 49 | + # Authenticate user |
| 50 | + user = security.authenticate( |
| 51 | + db, email=form_data.username, password=form_data.password |
| 52 | + ) |
| 53 | + if not user: |
| 54 | + raise HTTPException( |
| 55 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 56 | + detail="Incorrect email or password", |
| 57 | + ) |
| 58 | + |
| 59 | + if not user.is_active: |
| 60 | + raise HTTPException( |
| 61 | + status_code=status.HTTP_400_BAD_REQUEST, |
| 62 | + detail="Inactive user", |
| 63 | + ) |
| 64 | + |
| 65 | + # Generate tokens |
| 66 | + tokens = security.generate_token_response(str(user.id)) |
| 67 | + |
| 68 | + # Store refresh token in database |
| 69 | + # TODO: Implement refresh token storage |
| 70 | + |
| 71 | + return tokens |
| 72 | + |
| 73 | +@router.post("/refresh", response_model=Token) |
| 74 | +def refresh_token( |
| 75 | + refresh_token: str, |
| 76 | + db: Session = Depends(get_db), |
| 77 | +) -> Any: |
| 78 | + """ |
| 79 | + Refresh access token using a valid refresh token. |
| 80 | + """ |
| 81 | + # Verify refresh token |
| 82 | + try: |
| 83 | + token_data = security.verify_refresh_token(refresh_token) |
| 84 | + except HTTPException as e: |
| 85 | + raise e |
| 86 | + |
| 87 | + # Get user |
| 88 | + user = get_user(db, user_id=token_data["sub"]) |
| 89 | + if not user: |
| 90 | + raise HTTPException( |
| 91 | + status_code=status.HTTP_404_NOT_FOUND, |
| 92 | + detail="User not found", |
| 93 | + ) |
| 94 | + |
| 95 | + # Generate new tokens |
| 96 | + tokens = security.generate_token_response(str(user.id)) |
| 97 | + |
| 98 | + # TODO: Update refresh token in database |
| 99 | + |
| 100 | + return tokens |
| 101 | + |
| 102 | +@router.post("/forgot-password", status_code=status.HTTP_202_ACCEPTED) |
| 103 | +def forgot_password( |
| 104 | + password_reset: PasswordResetRequest, |
| 105 | + db: Session = Depends(get_db), |
| 106 | +) -> Any: |
| 107 | + """ |
| 108 | + Request password reset. |
| 109 | + """ |
| 110 | + user = get_user_by_email(db, email=password_reset.email) |
| 111 | + if not user: |
| 112 | + # Don't reveal that the user doesn't exist |
| 113 | + return {"message": "If your email is registered, you will receive a password reset link."} |
| 114 | + |
| 115 | + # TODO: Generate password reset token and send email |
| 116 | + |
| 117 | + return {"message": "If your email is registered, you will receive a password reset link."} |
| 118 | + |
| 119 | +@router.post("/reset-password", status_code=status.HTTP_200_OK) |
| 120 | +def reset_password( |
| 121 | + reset_data: PasswordResetConfirm, |
| 122 | + db: Session = Depends(get_db), |
| 123 | +) -> Any: |
| 124 | + """ |
| 125 | + Reset password using a valid token. |
| 126 | + """ |
| 127 | + # TODO: Verify reset token |
| 128 | + # TODO: Update user password |
| 129 | + |
| 130 | + return {"message": "Password updated successfully"} |
| 131 | + |
| 132 | +@router.get("/me", response_model=UserOut) |
| 133 | +def read_users_me( |
| 134 | + current_user: User = Depends(security.get_current_user), |
| 135 | +) -> Any: |
| 136 | + """ |
| 137 | + Get current user. |
| 138 | + """ |
| 139 | + return current_user |
0 commit comments