|
@@ -40,15 +40,13 @@ async def query_user(user_id: str):
|
|
|
|
|
|
@users.post("/login")
|
|
|
async def login(data: OAuth2PasswordRequestForm = Depends()):
|
|
|
- username = data.username
|
|
|
+ email = data.email
|
|
|
password = data.password
|
|
|
- password_bytes = password.encode('utf-8') # 輸入的密碼
|
|
|
- print(password_bytes)
|
|
|
- user = await query_user(username)
|
|
|
- stored_hashed_password_bytes = user.password.encode('utf-8')
|
|
|
+
|
|
|
+ user = await query_user(email)
|
|
|
print(user)
|
|
|
access_token = manager.create_access_token(
|
|
|
- data={'sub': username}
|
|
|
+ data={'sub': email}
|
|
|
)
|
|
|
if not user:
|
|
|
# you can return any response or error of your choice
|
|
@@ -56,9 +54,8 @@ async def login(data: OAuth2PasswordRequestForm = Depends()):
|
|
|
|
|
|
# elif password != user.password:
|
|
|
# raise InvalidCredentialsException
|
|
|
-
|
|
|
else:
|
|
|
- if bcrypt.checkpw(password_bytes, stored_hashed_password_bytes):
|
|
|
+ if bcrypt.checkpw(user.password.encode('utf-8'), password):
|
|
|
return {'access_token': access_token}
|
|
|
else:
|
|
|
return {"message": "Invalid username or password"}
|
|
@@ -73,8 +70,8 @@ async def logout():
|
|
|
async def add(username: str = Form(default=''), password: str = Form(default=''), email: str = Form(default=''), re_password: str = Form(default='')):
|
|
|
if username and password and email:
|
|
|
if password == re_password:
|
|
|
- hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
|
|
|
- u = await User.create(username=username, password=hashed_password, email=email,point='1000')
|
|
|
+ hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
|
|
|
+ u = await User.create(username=username, password=hashed_password, email=email)
|
|
|
if u:
|
|
|
# send_email()
|
|
|
return {"msg": "已寄送認證信", "code": 200}
|