Update login endpoint to use JSON request body instead of form data
This commit is contained in:
parent
d1c05cbd6e
commit
e2134243ae
@ -2,7 +2,6 @@ from datetime import timedelta
|
|||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from fastapi import APIRouter, Depends, HTTPException, status
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
from fastapi.security import OAuth2PasswordRequestForm
|
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
from app import crud, schemas
|
from app import crud, schemas
|
||||||
@ -14,15 +13,11 @@ router = APIRouter()
|
|||||||
|
|
||||||
|
|
||||||
@router.post("/login", response_model=schemas.Token)
|
@router.post("/login", response_model=schemas.Token)
|
||||||
def login_access_token(
|
def login_access_token(login_data: schemas.LoginRequest, db: Session = Depends(get_db)) -> Any:
|
||||||
db: Session = Depends(get_db), form_data: OAuth2PasswordRequestForm = Depends()
|
|
||||||
) -> Any:
|
|
||||||
"""
|
"""
|
||||||
OAuth2 compatible token login, get an access token for future requests
|
JSON-based token login, get an access token for future requests
|
||||||
"""
|
"""
|
||||||
user = crud.user.authenticate(
|
user = crud.user.authenticate(db, email=login_data.email, password=login_data.password)
|
||||||
db, email=form_data.username, password=form_data.password
|
|
||||||
)
|
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_401_UNAUTHORIZED,
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
||||||
@ -30,14 +25,10 @@ def login_access_token(
|
|||||||
headers={"WWW-Authenticate": "Bearer"},
|
headers={"WWW-Authenticate": "Bearer"},
|
||||||
)
|
)
|
||||||
elif not crud.user.is_active(user):
|
elif not crud.user.is_active(user):
|
||||||
raise HTTPException(
|
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user")
|
||||||
status_code=status.HTTP_400_BAD_REQUEST, detail="Inactive user"
|
|
||||||
)
|
|
||||||
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||||
return {
|
return {
|
||||||
"access_token": create_access_token(
|
"access_token": create_access_token(user.id, expires_delta=access_token_expires),
|
||||||
user.id, expires_delta=access_token_expires
|
|
||||||
),
|
|
||||||
"token_type": "bearer",
|
"token_type": "bearer",
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -57,13 +48,13 @@ def register_user(
|
|||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="A user with this email already exists",
|
detail="A user with this email already exists",
|
||||||
)
|
)
|
||||||
|
|
||||||
username_exists = crud.user.get_by_username(db, username=user_in.username)
|
username_exists = crud.user.get_by_username(db, username=user_in.username)
|
||||||
if username_exists:
|
if username_exists:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_400_BAD_REQUEST,
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
detail="A user with this username already exists",
|
detail="A user with this username already exists",
|
||||||
)
|
)
|
||||||
|
|
||||||
user = crud.user.create(db, obj_in=user_in)
|
user = crud.user.create(db, obj_in=user_in)
|
||||||
return user
|
return user
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel, EmailStr, Field
|
||||||
|
|
||||||
|
|
||||||
class Token(BaseModel):
|
class Token(BaseModel):
|
||||||
@ -9,4 +9,9 @@ class Token(BaseModel):
|
|||||||
|
|
||||||
|
|
||||||
class TokenPayload(BaseModel):
|
class TokenPayload(BaseModel):
|
||||||
sub: Optional[int] = None
|
sub: Optional[int] = None
|
||||||
|
|
||||||
|
|
||||||
|
class LoginRequest(BaseModel):
|
||||||
|
email: EmailStr
|
||||||
|
password: str = Field(..., min_length=8)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user