From 80f6c629d41f8638a8fe886e428c61867a7bf01a Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Mon, 6 Jun 2022 18:16:06 +0300 Subject: [PATCH] sql_app: create user now stores new api token Signed-off-by: HeshamTB --- sql_app/main.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/sql_app/main.py b/sql_app/main.py index 545aebb..a980a63 100644 --- a/sql_app/main.py +++ b/sql_app/main.py @@ -64,13 +64,22 @@ def get_current_iot_device(current_device: schemas.IotBluetoothMac = Depends(), def create_user(user: schemas.UserCreate, db: Session = Depends(get_db)): db_user = crud.get_user_by_email(db, email=user.email) if db_user: - raise HTTPException(status_code=400, detail="Email already registered") + raise HTTPException(status_code=400, detail="Email/Username already registered") db_user = crud.get_user_by_username(db, username=user.username) if db_user: - raise HTTPException(status_code=400, detail="Username already registerd") - - return crud.create_user(db=db, user=user) + raise HTTPException(status_code=400, detail="Email/Username already registered") + + db_user = crud.create_user(db=db, user=user) + if not db_user: + raise HTTPException(status_code=500, detail="Failed to create user") + + access_token = auth_helper.create_access_token( + data={"sub": db_user.username}, expires_delta=timedelta(minutes=15) + ) + crud.set_user_last_token(db, db_user.username, access_token) + + return db_user @app.get("/users/me/", response_model=schemas.User, tags=['Users']) def get_user_details(current_user: schemas.User = Depends(get_current_active_user)):