diff --git a/app/api/routes/user_routes.py b/app/api/routes/user_routes.py index 34a012f..5c15587 100644 --- a/app/api/routes/user_routes.py +++ b/app/api/routes/user_routes.py @@ -101,4 +101,14 @@ def update_password(): @bp_user.route('/delete', methods=['DELETE']) @jwt_required() def delete_user(): - return abort(501) \ No newline at end of file + 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 \ No newline at end of file diff --git a/app/services/user_service.py b/app/services/user_service.py index cfb7144..e4a3b66 100644 --- a/app/services/user_service.py +++ b/app/services/user_service.py @@ -50,15 +50,7 @@ class UserService: hashed_password = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt()) try: - db_cursor.execute("select max(user_id) as max_id from user") - last_id = db_cursor.fetchone()['max_id'] - - if last_id < 23000: - return {"Failed": "Error occurred when fetching last user id"} - - new_id = last_id + 1 - - db_cursor.execute("insert into user (username, email, password, user_id, role_id) values (%s, %s, %s, %s, 1)", (username, email, hashed_password, new_id)) + db_cursor.execute("insert into user (username, email, password, role_id) values (%s, %s, %s, 1)", (username, email, hashed_password)) db_connection.commit() except Error as e: print(f"Error: {e}") @@ -79,12 +71,11 @@ class UserService: :rtype: Tuple[Union[dict, str], int] """ - db_cursor.execute("select user_id, password, last_change from user where username = %s", (username,)) + db_cursor.execute("select user_id, password from user where username = %s", (username,)) result = db_cursor.fetchone() user_id = result['user_id'] password_hash = result['password'] - last_change = result['last_change'] if user_id is None: return {"Failed": "Username not found"}, 400 @@ -94,7 +85,7 @@ class UserService: expire = datetime.timedelta(hours=1) - token = create_access_token(identity=user_id, expires_delta=expire,additional_claims={"lm": last_change}) + token = create_access_token(identity=user_id, expires_delta=expire) return {"token": token}, 200 @@ -114,6 +105,16 @@ class UserService: return {"Success": "Successfully logged out"}, 200 + @staticmethod + def delete_user(user_id: str) -> Tuple[Union[dict, str], int]: + try: + db_cursor.execute("delete from user where user_id = %s", (user_id,)) + db_connection.commit() + except Error as e: + return {"Failed": f"Failed to delete user. {e}"}, 500 + + return {"Success": "User successfully deleted"}, 200 + @staticmethod def update_email(user_id: str, new_email: str) -> Tuple[Union[dict, str], int]: """