swag-shop/app/api/routes/user_routes.py

134 lines
3.0 KiB
Python
Raw Normal View History

2024-03-05 16:01:26 +01:00
from app.api import bp_user
from flask_jwt_extended import jwt_required, get_jwt_identity, get_jwt
from flask import request, abort
2024-03-05 16:01:26 +01:00
from app.services.user_service import UserService
@bp_user.route('/register', methods=['POST'])
def register():
username = request.json.get('username')
displayname = request.json.get('displayname')
email = request.json.get('email')
password = request.json.get('password')
if username is None or email is None or password is None or displayname is None:
return abort(400)
result, status_code = UserService.register(username, displayname, email, password)
return result, status_code
2024-03-05 16:01:26 +01:00
@bp_user.route('/login', methods=['POST'])
def login():
username = request.json.get('username')
password = request.json.get('password')
if username is None or password is None:
return abort(400)
result, status_code = UserService.login(username, password)
return result, status_code
2024-03-05 16:01:26 +01:00
@bp_user.route('/logout', methods=['DELETE'])
@jwt_required()
def logout():
jwt = get_jwt()
2024-03-05 16:01:26 +01:00
jti = jwt['jti']
exp = jwt['exp']
result, status_code = UserService.logout(jti, exp)
2024-03-05 16:01:26 +01:00
return result, status_code
2024-03-05 16:01:26 +01:00
@bp_user.route('/update/username', methods=['PUT'])
@jwt_required()
def update_username():
user_id = get_jwt_identity()
new_username = request.json.get('new_username')
if new_username is None:
2024-03-05 16:01:26 +01:00
return abort(400)
result, status_code = UserService.update_username(user_id, new_username)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp)
return result, status_code
@bp_user.route('/update/displayname', methods=['PUT'])
@jwt_required()
def update_displayname():
user_id = get_jwt_identity()
new_displayname = request.json.get('new_displayname')
if new_displayname is None:
return abort(400)
result, status_code = UserService.update_username(user_id, new_displayname)
2024-03-05 16:01:26 +01:00
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp)
2024-03-05 16:01:26 +01:00
return result, status_code
@bp_user.route('/update/email', methods=['PUT'])
2024-03-05 16:01:26 +01:00
@jwt_required()
def update_email():
username = get_jwt_identity()
new_mail = request.json.get('new_email')
if new_mail is None:
return abort(400)
result, status_code = UserService.update_email(username, new_mail)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp)
2024-03-05 16:01:26 +01:00
return result, status_code
2024-03-05 16:01:26 +01:00
@bp_user.route('/update/password', methods=['PUT'])
2024-03-05 16:01:26 +01:00
@jwt_required()
def update_password():
username = get_jwt_identity()
new_password = request.json.get('new_password')
if new_password is None:
return abort(400)
result, status_code = UserService.update_password(username, new_password)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp)
return result, status_code
@bp_user.route('/delete', methods=['DELETE'])
@jwt_required()
def delete_user():
user_id = get_jwt_identity()
result, status_code = UserService.delete_user(user_id)
jwt = get_jwt()
jti = jwt['jti']
exp = jwt['exp']
UserService.logout(jti, exp)
return result, status_code