Kaynağa Gözat

server users.py

conrad 1 yıl önce
ebeveyn
işleme
980adbdc1a

BIN
app/api/__pycache__/classes.cpython-38.pyc


BIN
app/api/__pycache__/users.cpython-38.pyc


+ 7 - 6
app/api/users.py

@@ -30,7 +30,7 @@ async def query_user(user_id: str):
     :param user_id: E-Mail of the user
     :return: None or the user object
     """
-    result = await User.filter(username=user_id).first()
+    result = await User.filter(email=user_id).first()
     if not result:
         print('[]')
         return []
@@ -40,7 +40,7 @@ async def query_user(user_id: str):
 
 @users.post("/login")
 async def login(data: OAuth2PasswordRequestForm = Depends()):
-    email = data.email
+    email = data.username
     password = data.password
 
     user = await query_user(email)
@@ -50,13 +50,14 @@ async def login(data: OAuth2PasswordRequestForm = Depends()):
     )
     if not user:
         # you can return any response or error of your choice
-        raise InvalidCredentialsException
+        return {"message":"查無此人"}
 
     # elif password != user.password:
     #     raise InvalidCredentialsException
     else:
-        if bcrypt.checkpw(user.password.encode('utf-8'), password):
-            return {'access_token': access_token}
+        stored_hashed_password = user.password.encode('utf-8')
+        if bcrypt.checkpw(password.encode('utf-8'),stored_hashed_password):
+            return {'access_token': access_token,'username':user.username,'email':user.email,'points':user.points}
         else:
             return {"message": "Invalid username or password"}
 
@@ -70,7 +71,7 @@ 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())
+            hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()).decode('utf-8')
             u = await User.create(username=username, password=hashed_password, email=email)
             if u:
                 # send_email()

BIN
app/models/__pycache__/models.cpython-38.pyc