mirror of
http://88.130.71.182:3000/BlitTech/badoHair_be.git
synced 2026-06-13 08:34:29 +00:00
61 lines
1.7 KiB
Python
61 lines
1.7 KiB
Python
from fastapi import APIRouter, Depends
|
|
import asyncpg
|
|
|
|
from app.core.responses import ok
|
|
from app.dependencies import get_db, get_current_user
|
|
from app.models.auth import RegisterRequest, LoginRequest, RefreshRequest, ForgotPasswordRequest, UpdateProfileRequest
|
|
from app.services import auth_service
|
|
|
|
router = APIRouter(prefix="/auth", tags=["Auth"])
|
|
|
|
|
|
@router.post("/register", status_code=201)
|
|
async def register(body: RegisterRequest, db: asyncpg.Connection = Depends(get_db)):
|
|
data = await auth_service.register(body, db)
|
|
return ok(data)
|
|
|
|
|
|
@router.post("/login")
|
|
async def login(body: LoginRequest):
|
|
data = await auth_service.login(body)
|
|
return ok(data)
|
|
|
|
|
|
@router.post("/refresh")
|
|
async def refresh(body: RefreshRequest):
|
|
data = await auth_service.refresh(body)
|
|
return ok(data)
|
|
|
|
|
|
@router.post("/forgot-password")
|
|
async def forgot_password(body: ForgotPasswordRequest):
|
|
await auth_service.forgot_password(body.email)
|
|
return ok({"message": "If that email exists, a reset link has been sent."})
|
|
|
|
|
|
@router.get("/me")
|
|
async def me(user: dict = Depends(get_current_user)):
|
|
return ok({
|
|
"id": str(user["id"]),
|
|
"email": user["email"],
|
|
"full_name": user["full_name"],
|
|
"phone": user["phone"],
|
|
"role": user["role"],
|
|
})
|
|
|
|
|
|
@router.patch("/me")
|
|
async def update_me(
|
|
body: UpdateProfileRequest,
|
|
user: dict = Depends(get_current_user),
|
|
db: asyncpg.Connection = Depends(get_db),
|
|
):
|
|
updated = await auth_service.update_profile(str(user["id"]), db, body.full_name, body.phone)
|
|
return ok({
|
|
"id": str(updated["id"]),
|
|
"email": updated["email"],
|
|
"full_name": updated["full_name"],
|
|
"phone": updated["phone"],
|
|
"role": updated["role"],
|
|
})
|