36 lines
1.3 KiB
Python
36 lines
1.3 KiB
Python
from datetime import timedelta
|
|
from fastapi import APIRouter, Depends, HTTPException
|
|
from fastapi.security import OAuth2PasswordRequestForm
|
|
from sqlmodel import Session
|
|
|
|
from app.api import deps
|
|
from app.core.config import settings
|
|
from app.core.schemas.token import Token
|
|
from app.core import security
|
|
from app.infrastructure.repositories.user import UserRepository
|
|
|
|
router = APIRouter()
|
|
|
|
@router.post("/login/access-token", response_model=Token)
|
|
def login_access_token(
|
|
db: Session = Depends(deps.get_db), form_data: OAuth2PasswordRequestForm = Depends()
|
|
):
|
|
"""
|
|
OAuth2 compatible token login, get an access token for future requests
|
|
"""
|
|
user_repo = UserRepository(db)
|
|
user = user_repo.get_by_email(email=form_data.username)
|
|
|
|
if not user or not security.verify_password(form_data.password, user.hashed_password):
|
|
raise HTTPException(status_code=400, detail="Incorrect email or password")
|
|
elif not user.is_active:
|
|
raise HTTPException(status_code=400, detail="Inactive user")
|
|
|
|
access_token_expires = timedelta(minutes=settings.ACCESS_TOKEN_EXPIRE_MINUTES)
|
|
return {
|
|
"access_token": security.create_access_token(
|
|
user.email, expires_delta=access_token_expires
|
|
),
|
|
"token_type": "bearer",
|
|
}
|