Skip to content

Instantly share code, notes, and snippets.

@cuongld2
Created March 16, 2020 09:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cuongld2/e770e85a5d5e8d40ac09e3d1b3331f7b to your computer and use it in GitHub Desktop.
Save cuongld2/e770e85a5d5e8d40ac09e3d1b3331f7b to your computer and use it in GitHub Desktop.
authenticate api
@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