from app.api import bp_user from flask_jwt_extended import jwt_required, get_jwt_identity, get_jwt from flask import request, abort from flasgger import swag_from 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') 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 @bp_user.route('/login', methods=['POST']) @swag_from(login_swagger) 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 @bp_user.route('/logout', methods=['DELETE']) @swag_from(logout_swagger) @jwt_required() def logout(): jwt = get_jwt() user_id = get_jwt_identity() result, status_code = UserService.logout(jwt, user_id) return result, status_code @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: return abort(400) result, status_code = UserService.update_username(user_id, new_username) jwt = get_jwt() UserService.logout(jwt, user_id) 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) jwt = get_jwt() UserService.logout(jwt, user_id) return result, status_code @bp_user.route('/update/email', methods=['PUT']) @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() UserService.logout(jwt, username) return result, status_code @bp_user.route('/update/password', methods=['PUT']) @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() UserService.logout(jwt, username) return result, status_code @bp_user.route('/delete', methods=['DELETE']) @swag_from(delete_swagger) @jwt_required() def delete_user(): user_id = get_jwt_identity() result, status_code = UserService.delete_user(user_id) jwt = get_jwt() UserService.logout(jwt, user_id) return result, status_code