Cleanup in User code
This commit is contained in:
parent
628d210156
commit
3aaa14c986
@ -4,11 +4,12 @@ from flask import request, abort
|
|||||||
|
|
||||||
from flasgger import swag_from
|
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
|
from app.services.user_service import UserService
|
||||||
|
|
||||||
@bp_user.route('/register', methods=['POST'])
|
@bp_user.route('/register', methods=['POST'])
|
||||||
|
@swag_from(register_swagger)
|
||||||
def register():
|
def register():
|
||||||
username = request.json.get('username')
|
username = request.json.get('username')
|
||||||
displayname = request.json.get('displayname')
|
displayname = request.json.get('displayname')
|
||||||
@ -40,13 +41,9 @@ def login():
|
|||||||
@jwt_required()
|
@jwt_required()
|
||||||
def logout():
|
def logout():
|
||||||
jwt = get_jwt()
|
jwt = get_jwt()
|
||||||
|
|
||||||
jti = jwt['jti']
|
|
||||||
exp = jwt['exp']
|
|
||||||
|
|
||||||
user_id = get_jwt_identity()
|
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
|
return result, status_code
|
||||||
|
|
||||||
@ -62,10 +59,7 @@ def update_username():
|
|||||||
result, status_code = UserService.update_username(user_id, new_username)
|
result, status_code = UserService.update_username(user_id, new_username)
|
||||||
|
|
||||||
jwt = get_jwt()
|
jwt = get_jwt()
|
||||||
|
UserService.logout(jwt, user_id)
|
||||||
jti = jwt['jti']
|
|
||||||
exp = jwt['exp']
|
|
||||||
UserService.logout(jti, exp, user_id)
|
|
||||||
|
|
||||||
return result, status_code
|
return result, status_code
|
||||||
|
|
||||||
@ -81,10 +75,7 @@ def update_displayname():
|
|||||||
result, status_code = UserService.update_username(user_id, new_displayname)
|
result, status_code = UserService.update_username(user_id, new_displayname)
|
||||||
|
|
||||||
jwt = get_jwt()
|
jwt = get_jwt()
|
||||||
|
UserService.logout(jwt, user_id)
|
||||||
jti = jwt['jti']
|
|
||||||
exp = jwt['exp']
|
|
||||||
UserService.logout(jti, exp, user_id)
|
|
||||||
|
|
||||||
return result, status_code
|
return result, status_code
|
||||||
|
|
||||||
@ -100,10 +91,7 @@ def update_email():
|
|||||||
result, status_code = UserService.update_email(username, new_mail)
|
result, status_code = UserService.update_email(username, new_mail)
|
||||||
|
|
||||||
jwt = get_jwt()
|
jwt = get_jwt()
|
||||||
|
UserService.logout(jwt, username)
|
||||||
jti = jwt['jti']
|
|
||||||
exp = jwt['exp']
|
|
||||||
UserService.logout(jti, exp, username)
|
|
||||||
|
|
||||||
return result, status_code
|
return result, status_code
|
||||||
|
|
||||||
@ -120,10 +108,7 @@ def update_password():
|
|||||||
result, status_code = UserService.update_password(username, new_password)
|
result, status_code = UserService.update_password(username, new_password)
|
||||||
|
|
||||||
jwt = get_jwt()
|
jwt = get_jwt()
|
||||||
|
UserService.logout(jwt, username)
|
||||||
jti = jwt['jti']
|
|
||||||
exp = jwt['exp']
|
|
||||||
UserService.logout(jti, exp, username)
|
|
||||||
|
|
||||||
return result, status_code
|
return result, status_code
|
||||||
|
|
||||||
@ -136,9 +121,6 @@ def delete_user():
|
|||||||
result, status_code = UserService.delete_user(user_id)
|
result, status_code = UserService.delete_user(user_id)
|
||||||
|
|
||||||
jwt = get_jwt()
|
jwt = get_jwt()
|
||||||
|
UserService.logout(jwt, user_id)
|
||||||
jti = jwt['jti']
|
|
||||||
exp = jwt['exp']
|
|
||||||
UserService.logout(jti, exp, user_id)
|
|
||||||
|
|
||||||
return result, status_code
|
return result, status_code
|
@ -62,7 +62,7 @@ class UserService:
|
|||||||
print(f"Error: {e}")
|
print(f"Error: {e}")
|
||||||
return {"Failed": "Failed to insert into database. Username or email are likely in use already"}, 500
|
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
|
return {"Success": "User created successfully"}, 200
|
||||||
|
|
||||||
@ -98,7 +98,7 @@ class UserService:
|
|||||||
|
|
||||||
token = create_access_token(identity=user_id, expires_delta=expire)
|
token = create_access_token(identity=user_id, expires_delta=expire)
|
||||||
|
|
||||||
send_mail("login", email)
|
UserService.__send_email("login", email=email)
|
||||||
|
|
||||||
return {"token": token}, 200
|
return {"token": token}, 200
|
||||||
|
|
||||||
@ -106,7 +106,7 @@ class UserService:
|
|||||||
return {"Failed": f"Failed to login. Error: {e}"}, 500
|
return {"Failed": f"Failed to login. Error: {e}"}, 500
|
||||||
|
|
||||||
@staticmethod
|
@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.
|
Logs out a user by invalidating the provided JWT.
|
||||||
|
|
||||||
@ -118,6 +118,9 @@ class UserService:
|
|||||||
:rtype: Tuple[Union[dict, str], int]
|
:rtype: Tuple[Union[dict, str], int]
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
jti = jwt['jti']
|
||||||
|
exp = jwt['exp']
|
||||||
|
|
||||||
UserService.__invalidate_token(jti, exp)
|
UserService.__invalidate_token(jti, exp)
|
||||||
UserService.__send_email("logout", id=user_id)
|
UserService.__send_email("logout", id=user_id)
|
||||||
|
|
||||||
@ -234,7 +237,7 @@ class UserService:
|
|||||||
cursor.execute("select email from user where username = %s", (username,))
|
cursor.execute("select email from user where username = %s", (username,))
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
email = result['email']
|
email = result['email']
|
||||||
send_mail("logout", email)
|
send_mail(message, email)
|
||||||
|
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500
|
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,))
|
cursor.execute("select email from user where id = %s", (id,))
|
||||||
result = cursor.fetchone()
|
result = cursor.fetchone()
|
||||||
email = result['email']
|
email = result['email']
|
||||||
send_mail("logout", email)
|
send_mail(message, email)
|
||||||
|
|
||||||
except Error as e:
|
except Error as e:
|
||||||
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500
|
return {"Failed": f"Failed to fetch some data. Error: {e}"}, 500
|
||||||
|
Loading…
x
Reference in New Issue
Block a user