feat: Updated endpoint endpoints/login.post.py via AI
This commit is contained in:
parent
6cd75c0f51
commit
4ed31e1efc
@ -1,37 +1,14 @@
|
||||
from fastapi import APIRouter, Depends, HTTPException
|
||||
from pydantic import BaseModel
|
||||
from datetime import timedelta
|
||||
from core.database import get_db
|
||||
from sqlalchemy.orm import Session
|
||||
from core.auth import verify_password, create_access_token
|
||||
from models.user import User
|
||||
from fastapi import APIRouter, HTTPException, status
|
||||
from typing import Dict, Any
|
||||
from helpers.login_helpers import handle_login
|
||||
|
||||
router = APIRouter()
|
||||
|
||||
class UserAuth(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
|
||||
@router.post("/login")
|
||||
async def login(
|
||||
user_data: UserAuth,
|
||||
db: Session = Depends(get_db)
|
||||
):
|
||||
"""User authentication endpoint"""
|
||||
user = db.query(User).filter(User.username == user_data.username).first()
|
||||
|
||||
if not user or not verify_password(user_data.password, user.hashed_password):
|
||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
||||
|
||||
# Generate token with expiration
|
||||
access_token = create_access_token(
|
||||
data={"sub": user.id},
|
||||
expires_delta=timedelta(hours=1)
|
||||
)
|
||||
|
||||
return {
|
||||
"access_token": access_token,
|
||||
"token_type": "bearer",
|
||||
"user_id": user.id,
|
||||
"username": user.username
|
||||
}
|
||||
@router.post("/login", status_code=status.HTTP_200_OK, response_model=Dict[str, Any])
|
||||
async def login_handler(login_data: Dict[str, Any]):
|
||||
"""Handle user login"""
|
||||
try:
|
||||
login_response = handle_login(login_data=login_data)
|
||||
return login_response
|
||||
except ValueError as e:
|
||||
raise HTTPException(status_code=400, detail=str(e))
|
Loading…
x
Reference in New Issue
Block a user