feat: Updated endpoint endpoints/login.post.py via AI
This commit is contained in:
parent
7fab4e21cc
commit
27885bdcd7
@ -1,37 +1,16 @@
|
|||||||
from fastapi import APIRouter, Depends, HTTPException
|
from fastapi import APIRouter, HTTPException, status
|
||||||
from pydantic import BaseModel
|
from typing import Dict, Any
|
||||||
from datetime import timedelta
|
from helpers.generic_helpers import login_user
|
||||||
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
|
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
class UserAuth(BaseModel):
|
@router.post("/login", status_code=status.HTTP_200_OK, response_model=Dict[str, Any])
|
||||||
username: str
|
|
||||||
password: str
|
|
||||||
|
|
||||||
@router.post("/login")
|
|
||||||
async def login(
|
async def login(
|
||||||
user_data: UserAuth,
|
user_data: Dict[str, Any]
|
||||||
db: Session = Depends(get_db)
|
|
||||||
):
|
):
|
||||||
"""User authentication endpoint"""
|
"""Login user"""
|
||||||
user = db.query(User).filter(User.username == user_data.username).first()
|
try:
|
||||||
|
login_response = login_user(user_data=user_data)
|
||||||
if not user or not verify_password(user_data.password, user.hashed_password):
|
return login_response
|
||||||
raise HTTPException(status_code=400, detail="Invalid credentials")
|
except ValueError as e:
|
||||||
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
# 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
|
|
||||||
}
|
|
Loading…
x
Reference in New Issue
Block a user