75 lines
2.7 KiB
Python
75 lines
2.7 KiB
Python
import json
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy import select
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.core.auth import hash_password, verify_password, create_access_token
|
|
from app.core.deps import get_current_user
|
|
from app.db.database import get_db
|
|
from app.db.models import User, Class_
|
|
from app.schemas.auth import LoginRequest, RegisterRequest, ChangePasswordRequest
|
|
from app.schemas.user import TokenResponse, UserOut
|
|
from app.services.user_service import register_user
|
|
|
|
router = APIRouter(prefix="/api/auth", tags=["auth"])
|
|
|
|
|
|
@router.post("/register")
|
|
async def register(req: RegisterRequest, db: AsyncSession = Depends(get_db)):
|
|
existing = await db.execute(select(User).where(User.email == req.email))
|
|
if existing.scalar_one_or_none():
|
|
raise HTTPException(status_code=400, detail="Email already registered")
|
|
|
|
class_result = await db.execute(select(Class_).where(Class_.id == req.class_id))
|
|
if class_result.scalar_one_or_none() is None:
|
|
raise HTTPException(status_code=400, detail="Class not found")
|
|
|
|
user = await register_user(
|
|
db=db,
|
|
email=req.email,
|
|
password_hash=hash_password(req.password),
|
|
name=req.name,
|
|
class_id=req.class_id,
|
|
student_id=req.student_id,
|
|
)
|
|
return {"message": "Registration submitted. Awaiting admin approval."}
|
|
|
|
|
|
@router.post("/login", response_model=TokenResponse)
|
|
async def login(req: LoginRequest, db: AsyncSession = Depends(get_db)):
|
|
result = await db.execute(select(User).where(User.email == req.email))
|
|
user = result.scalar_one_or_none()
|
|
|
|
if user is None or user.status != "approved":
|
|
raise HTTPException(
|
|
status_code=401, detail="Invalid credentials or account not approved"
|
|
)
|
|
|
|
if not verify_password(req.password, user.password_hash):
|
|
raise HTTPException(status_code=401, detail="Invalid credentials")
|
|
|
|
token = create_access_token({"sub": str(user.id), "role": user.role})
|
|
return TokenResponse(
|
|
token=token,
|
|
user=UserOut.model_validate(user),
|
|
)
|
|
|
|
|
|
@router.get("/me", response_model=UserOut)
|
|
async def get_me(user: User = Depends(get_current_user)):
|
|
return UserOut.model_validate(user)
|
|
|
|
|
|
@router.put("/change-password")
|
|
async def change_password(
|
|
req: ChangePasswordRequest,
|
|
user: User = Depends(get_current_user),
|
|
db: AsyncSession = Depends(get_db),
|
|
):
|
|
if not verify_password(req.old_password, user.password_hash):
|
|
raise HTTPException(status_code=400, detail="Old password is incorrect")
|
|
user.password_hash = hash_password(req.new_password)
|
|
await db.commit()
|
|
return {"message": "Password changed successfully"}
|