Let register return access token without extra login
This commit is contained in:
@@ -16,13 +16,13 @@ ALGORITHM = "HS256"
|
|||||||
ACCESS_TOKEN_EXPIRE_MINUTES = 44640 # One month
|
ACCESS_TOKEN_EXPIRE_MINUTES = 44640 # One month
|
||||||
|
|
||||||
|
|
||||||
def authenticate_user(db: Session, user: UserCreate):
|
def authenticate_user(db: Session, username: str, password: str):
|
||||||
"""Checks whether the provided credentials match with an existing User"""
|
"""Checks whether the provided credentials match with an existing User"""
|
||||||
db_user = get_user_by_username(db=db, username=user.username)
|
db_user = get_user_by_username(db, username)
|
||||||
if not db_user:
|
if not db_user:
|
||||||
return False
|
return False
|
||||||
hashed_password = db_user.hashed_password
|
hashed_password = db_user.hashed_password
|
||||||
if not hashed_password or not pwd_context.verify(user.password, hashed_password):
|
if not hashed_password or not pwd_context.verify(password, hashed_password):
|
||||||
return False
|
return False
|
||||||
return db_user
|
return db_user
|
||||||
|
|
||||||
@@ -36,11 +36,11 @@ def register(db: Session, username: str, password: str):
|
|||||||
db.add(db_user)
|
db.add(db_user)
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(db_user)
|
db.refresh(db_user)
|
||||||
return db_user
|
return login(db, username, password)
|
||||||
|
|
||||||
|
|
||||||
def login(db: Session, user: UserCreate):
|
def login(db: Session, username: str, password: str):
|
||||||
user = authenticate_user(db, user)
|
user = authenticate_user(db, username, password)
|
||||||
if not user:
|
if not user:
|
||||||
raise HTTPException(status_code=401, detail="Invalid username or password")
|
raise HTTPException(status_code=401, detail="Invalid username or password")
|
||||||
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
access_token_expires = timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES)
|
||||||
|
|||||||
@@ -66,14 +66,14 @@ async def patch_current_user(
|
|||||||
crud_users.patch_user(db, current_user_name, user)
|
crud_users.patch_user(db, current_user_name, user)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/register", response_model=users.User)
|
@app.post("/register")
|
||||||
async def register(user: users.UserCreate, db: Session = Depends(get_db)):
|
async def register(user: users.UserCreate, db: Session = Depends(get_db)):
|
||||||
return crud_authentication.register(db, user.username, user.password)
|
return crud_authentication.register(db, user.username, user.password)
|
||||||
|
|
||||||
|
|
||||||
@app.post("/login")
|
@app.post("/login")
|
||||||
async def login(user: users.UserCreate, db: Session = Depends(get_db)):
|
async def login(user: users.UserCreate, db: Session = Depends(get_db)):
|
||||||
return crud_authentication.login(db, user)
|
return crud_authentication.login(db, user.username, user.password)
|
||||||
|
|
||||||
|
|
||||||
@app.get("/highscores", response_model=List[users.UserHighScore])
|
@app.get("/highscores", response_model=List[users.UserHighScore])
|
||||||
|
|||||||
Reference in New Issue
Block a user