
- Setup project structure and basic FastAPI application - Define database models for users, profiles, matches, and messages - Set up database connection and create Alembic migrations - Implement user authentication and registration endpoints - Create API endpoints for profile management, matches, and messaging - Add filtering and search functionality for tech profiles - Setup environment variable configuration - Create README with project information and setup instructions
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
from datetime import timedelta
|
|
from typing import Any
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.api.deps import authenticate_user, get_current_user
|
|
from app.core.config import settings
|
|
from app.core.security import create_access_token
|
|
from app.db.session import get_db
|
|
from app.schemas.token import Token
|
|
from app.schemas.user import User, UserCreate
|
|
from app.crud import user
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post("/login", response_model=Token)
|
|
def login_access_token(
|
|
db: Session = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
|
|
) -> Any:
|
|
"""
|
|
OAuth2 compatible token login, get an access token for future requests
|
|
"""
|
|
user_obj = authenticate_user(db, form_data.username, form_data.password)
|
|
if not user_obj:
|
|
raise HTTPException(status_code=400, detail="Incorrect username or password")
|
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
return {
|
|
"access_token": create_access_token(
|
|
user_obj.id, expires_delta=access_token_expires
|
|
),
|
|
"token_type": "bearer",
|
|
}
|
|
|
|
|
|
@router.post("/register", response_model=User)
|
|
def register_user(
|
|
*,
|
|
db: Session = Depends(get_db),
|
|
user_in: UserCreate,
|
|
) -> Any:
|
|
"""
|
|
Register a new user
|
|
"""
|
|
# Check if user already exists
|
|
if user.get_by_email(db, email=user_in.email):
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="A user with this email already exists",
|
|
)
|
|
if user.get_by_username(db, username=user_in.username):
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="A user with this username already exists",
|
|
)
|
|
|
|
# Create new user
|
|
new_user = user.create(db, obj_in=user_in)
|
|
return new_user
|
|
|
|
|
|
@router.post("/test-token", response_model=User)
|
|
def test_token(current_user: User = Depends(get_current_user)) -> Any:
|
|
"""
|
|
Test access token
|
|
"""
|
|
return current_user |