Compare commits

..

2 Commits

Author SHA1 Message Date
3aaa14c986 Cleanup in User code 2024-05-05 23:51:14 +02:00
628d210156 Register swagger 2024-05-05 23:50:50 +02:00
3 changed files with 43 additions and 32 deletions

View File

@ -4,11 +4,12 @@ from flask import request, abort
from flasgger import swag_from
from app.doc.user_swag import login_swagger, logout_swagger, delete_swagger
from app.doc.user_swag import login_swagger, logout_swagger, delete_swagger, register_swagger
from app.services.user_service import UserService
@bp_user.route('/register', methods=['POST'])
@swag_from(register_swagger)
def register():
username = request.json.get('username')
displayname = request.json.get('displayname')
@ -40,13 +41,9 @@ def login():
@jwt_required()
def logout():
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
user_id = get_jwt_identity()
result, status_code = UserService.logout(jti, exp, user_id)
result, status_code = UserService.logout(jwt, user_id)
return result, status_code
@ -62,10 +59,7 @@ def update_username():
result, status_code = UserService.update_username(user_id, new_username)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, user_id)
UserService.logout(jwt, user_id)
return result, status_code
@ -81,10 +75,7 @@ def update_displayname():
result, status_code = UserService.update_username(user_id, new_displayname)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, user_id)
UserService.logout(jwt, user_id)
return result, status_code
@ -100,10 +91,7 @@ def update_email():
result, status_code = UserService.update_email(username, new_mail)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, username)
UserService.logout(jwt, username)
return result, status_code
@ -120,10 +108,7 @@ def update_password():
result, status_code = UserService.update_password(username, new_password)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, username)
UserService.logout(jwt, username)
return result, status_code
@ -136,9 +121,6 @@ def delete_user():
result, status_code = UserService.delete_user(user_id)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp, user_id)
UserService.logout(jwt, user_id)
return result, status_code

View File

@ -1,3 +1,29 @@
register_swagger = {
"methods": ["POST"],
"tags": ["User"],
"description": "Registers a new user in the app. Also sends a notification to the user via the provided email",
"parameters":
[
{
"in": "body",
"name": "body",
"description": "Username, displayname and password of the new user\n- Username can be only lowercase and up to 64 characters\n- Displayname can contain special characters (. _ -) and lower and upper characters\n- Password must be at least 8 characters long, contain both lower and upper characters, numbers and special characters\n- Email has to be in format \"name@domain.tld\" and up to 64 characters long in total",
"required": True,
"schema":
{
"type": "object",
"properties":
{
"username": {"type": "string", "example": "mycoolusername"},
"email": {"type": "string", "example": "mymail@dot.com"},
"displayname": {"type": "string", "example": "MyCoolDisplayName"},
"password": {"type": "string", "example": "My5tr0ngP@55w0rd"}
}
}
}
],
}
login_swagger = {
"methods": ["POST"],
"tags": ["User"],
@ -69,7 +95,7 @@ delete_swagger = {
{
"200":
{
"description": "User successfully deleted out"
"description": "User successfully deleted"
}
}
}

View File

@ -62,7 +62,7 @@ class UserService:
print(f"Error: {e}")
return {"Failed": "Failed to insert into database. Username or email are likely in use already"}, 500
send_mail("register", email)
UserService.__send_email("register", email=email)
return {"Success": "User created successfully"}, 200
@ -98,7 +98,7 @@ class UserService:
token = create_access_token(identity=user_id, expires_delta=expire)
send_mail("login", email)
UserService.__send_email("login", email=email)
return {"token": token}, 200
@ -106,7 +106,7 @@ class UserService:
return {"Failed": f"Failed to login. Error: {e}"}, 500
@staticmethod
def logout(jti, exp, user_id) -> Tuple[Union[dict, str], int]:
def logout(jwt_token, user_id) -> Tuple[Union[dict, str], int]:
"""
Logs out a user by invalidating the provided JWT.
@ -118,6 +118,9 @@ class UserService:
:rtype: Tuple[Union[dict, str], int]
"""
jti = jwt['jti']
exp = jwt['exp']
UserService.__invalidate_token(jti, exp)
UserService.__send_email("logout", id=user_id)
@ -234,7 +237,7 @@ class UserService:
cursor.execute("select email from user where username = %s", (username,))
result = cursor.fetchone()
email = result['email']
send_mail("logout", email)
send_mail(message, email)
except Error as e:
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500
@ -246,7 +249,7 @@ class UserService:
cursor.execute("select email from user where id = %s", (id,))
result = cursor.fetchone()
email = result['email']
send_mail("logout", email)
send_mail(message, email)
except Error as e:
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500