Created
March 16, 2020 09:30
-
-
Save cuongld2/e770e85a5d5e8d40ac09e3d1b3331f7b to your computer and use it in GitHub Desktop.
authenticate api
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@app.post("/authenticate", response_model=schemas.Token) | |
def authenticate_user(user: schemas.UserAuthenticate, db: Session = Depends(get_db)): | |
db_user = crud.get_user_by_username(db, username=user.username) | |
if db_user is None: | |
raise HTTPException(status_code=400, detail="Username not existed") | |
else: | |
is_password_correct = crud.check_username_password(db, user) | |
if is_password_correct is False: | |
raise HTTPException(status_code=400, detail="Password is not correct") | |
else: | |
from datetime import timedelta | |
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES) | |
from sql_app.app_utils import create_access_token | |
access_token = create_access_token( | |
data={"sub": user.username}, expires_delta=access_token_expires) | |
return {"access_token": access_token, "token_type": "Bearer"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment