24 lines
717 B
Python
24 lines
717 B
Python
# Entity: User
|
|
|
|
```python
|
|
from fastapi import APIRouter, Depends, HTTPException, status
|
|
from sqlalchemy.orm import Session
|
|
from core.database import get_db
|
|
from core.models.user import User
|
|
from core.schemas.user import UserLogin, UserResponse
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/login", status_code=200, response_model=UserResponse)
|
|
async def login(
|
|
login_data: UserLogin,
|
|
db: Session = Depends(get_db)
|
|
):
|
|
user = db.query(User).filter(User.email == login_data.email).first()
|
|
if not user or not user.verify_password(login_data.password):
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Incorrect email or password"
|
|
)
|
|
return user
|
|
``` |