|
@@ -35,7 +35,20 @@ async def query_user(user_id: str):
|
|
print('[]')
|
|
print('[]')
|
|
return []
|
|
return []
|
|
return result
|
|
return result
|
|
- # return DB['users'].get(user_id)
|
|
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+@manager.user_loader()
|
|
|
|
+async def query_user_username(user_id: str):
|
|
|
|
+ """
|
|
|
|
+ Get a user from the db
|
|
|
|
+ :param user_id: E-Mail of the user
|
|
|
|
+ :return: None or the user object
|
|
|
|
+ """
|
|
|
|
+ result = await User.filter(username=user_id).first()
|
|
|
|
+ if not result:
|
|
|
|
+ print('[]')
|
|
|
|
+ return []
|
|
|
|
+ return result
|
|
|
|
|
|
|
|
|
|
@users.post("/login")
|
|
@users.post("/login")
|
|
@@ -70,14 +83,22 @@ async def logout():
|
|
@users.post("/add")
|
|
@users.post("/add")
|
|
async def add(username: str = Form(default=''), password: str = Form(default=''), email: str = Form(default=''), re_password: str = Form(default='')):
|
|
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 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)
|
|
|
|
- if u:
|
|
|
|
- # send_email()
|
|
|
|
- return {"msg": "已寄送認證信", "code": 200}
|
|
|
|
|
|
+ user_email = await query_user(email)
|
|
|
|
+ user_username = await query_user_username(username)
|
|
|
|
+ if user_email:
|
|
|
|
+ return {"msg":"該信箱已存在","code":403}
|
|
|
|
+ elif user_username:
|
|
|
|
+ return {"msg":"該使用者名稱已存在","code":403}
|
|
else:
|
|
else:
|
|
- return {"msg":"確認密碼錯誤","code":403}
|
|
|
|
|
|
+ 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)
|
|
|
|
+ if u:
|
|
|
|
+ # send_email()
|
|
|
|
+ return {"msg": "已寄送認證信", "code": 200}
|
|
|
|
+ else:
|
|
|
|
+ return {"msg":"確認密碼錯誤","code":403}
|
|
|
|
+
|
|
return {"msg": "create user failed", "code": 403}
|
|
return {"msg": "create user failed", "code": 403}
|
|
|
|
|
|
|
|
|