Update code in endpoints/login.post.py
This commit is contained in:
parent
0687dfe3e5
commit
7f078ca6b1
@ -2,22 +2,27 @@ from fastapi import APIRouter, Depends, HTTPException, status
|
|||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
from core.database import get_db
|
from core.database import get_db
|
||||||
from models.user import User
|
from models.user import User
|
||||||
from schemas.user import UserLogin, TokenResponse
|
from schemas.user import UserLogin, UserResponse
|
||||||
from helpers.auth_helpers import authenticate_user, create_access_token
|
from helpers.auth_helpers import authenticate_user, create_access_token
|
||||||
|
|
||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
@router.post("/login", status_code=200, response_model=TokenResponse)
|
@router.post("/login", status_code=200, response_model=UserResponse)
|
||||||
async def login(
|
async def login(
|
||||||
login_data: UserLogin,
|
login_data: UserLogin,
|
||||||
db: Session = Depends(get_db)
|
db: Session = Depends(get_db)
|
||||||
):
|
):
|
||||||
user = authenticate_user(db, login_data.email, login_data.password)
|
user = authenticate_user(db, login_data.username, login_data.password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
detail="Incorrect email or password",
|
detail="Incorrect username or password",
|
||||||
headers={"WWW-Authenticate": "Bearer"},
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
)
|
)
|
||||||
access_token = create_access_token(data={"sub": user.email})
|
|
||||||
return {"access_token": access_token, "token_type": "bearer"}
|
access_token = create_access_token(data={"sub": user.username})
|
||||||
|
return {
|
||||||
|
"access_token": access_token,
|
||||||
|
"token_type": "bearer",
|
||||||
|
"user": user
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user