
- User authentication with JWT tokens (register/login) - Tutor session management with CRUD operations - Message tracking for tutor conversations - SQLite database with Alembic migrations - CORS configuration for frontend integration - Health check and service info endpoints - Proper project structure with models, schemas, and API routes
95 lines
2.8 KiB
Python
95 lines
2.8 KiB
Python
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
|
from sqlalchemy.orm import Session
|
|
from jose import JWTError, jwt
|
|
from app.db.session import get_db
|
|
from app.models.user import User
|
|
from app.schemas.user import UserCreate, UserLogin, User as UserSchema
|
|
from app.core.security import (
|
|
verify_password,
|
|
get_password_hash,
|
|
create_access_token,
|
|
SECRET_KEY,
|
|
ALGORITHM
|
|
)
|
|
|
|
router = APIRouter()
|
|
security = HTTPBearer()
|
|
|
|
|
|
def get_user_by_email(db: Session, email: str):
|
|
return db.query(User).filter(User.email == email).first()
|
|
|
|
|
|
def create_user(db: Session, user: UserCreate):
|
|
hashed_password = get_password_hash(user.password)
|
|
db_user = User(
|
|
email=user.email,
|
|
hashed_password=hashed_password,
|
|
full_name=user.full_name,
|
|
)
|
|
db.add(db_user)
|
|
db.commit()
|
|
db.refresh(db_user)
|
|
return db_user
|
|
|
|
|
|
def authenticate_user(db: Session, email: str, password: str):
|
|
user = get_user_by_email(db, email)
|
|
if not user:
|
|
return False
|
|
if not verify_password(password, user.hashed_password):
|
|
return False
|
|
return user
|
|
|
|
|
|
def get_current_user(
|
|
credentials: HTTPAuthorizationCredentials = Depends(security),
|
|
db: Session = Depends(get_db)
|
|
):
|
|
credentials_exception = HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Could not validate credentials",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
try:
|
|
payload = jwt.decode(credentials.credentials, SECRET_KEY, algorithms=[ALGORITHM])
|
|
user_id: int = payload.get("sub")
|
|
if user_id is None:
|
|
raise credentials_exception
|
|
except JWTError:
|
|
raise credentials_exception
|
|
|
|
user = db.query(User).filter(User.id == user_id).first()
|
|
if user is None:
|
|
raise credentials_exception
|
|
return user
|
|
|
|
|
|
@router.post("/register", response_model=UserSchema)
|
|
def register(user: UserCreate, db: Session = Depends(get_db)):
|
|
db_user = get_user_by_email(db, email=user.email)
|
|
if db_user:
|
|
raise HTTPException(
|
|
status_code=400,
|
|
detail="Email already registered"
|
|
)
|
|
return create_user(db=db, user=user)
|
|
|
|
|
|
@router.post("/login")
|
|
def login(user_credentials: UserLogin, db: Session = Depends(get_db)):
|
|
user = authenticate_user(db, user_credentials.email, user_credentials.password)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Incorrect email or password",
|
|
headers={"WWW-Authenticate": "Bearer"},
|
|
)
|
|
access_token = create_access_token(subject=user.id)
|
|
return {"access_token": access_token, "token_type": "bearer"}
|
|
|
|
|
|
@router.get("/me", response_model=UserSchema)
|
|
def read_users_me(current_user: User = Depends(get_current_user)):
|
|
return current_user |