fix: resolve 500 error in password update API by aligning schema and fixing hash logic

This commit is contained in:
DXC
2026-03-23 13:32:53 +08:00
parent 2242aca6fe
commit 61ec906cfb

View File

@ -1,6 +1,6 @@
# app/api/v1/auth.py
from flask import Blueprint, request, jsonify, current_app
from flask_jwt_extended import jwt_required, get_jwt
from flask_jwt_extended import jwt_required, get_jwt_identity
from app.services.auth_service import AuthService
from app.utils.decorators import permission_required, audit_log
@ -255,16 +255,17 @@ def get_my_permissions():
@jwt_required()
def change_my_password():
"""
改造】自我修改密码接口
重构】自我修改密码接口
- 无需管理员权限,无需旧密码
- 只要 JWT Token 有效(已证明当前登录身份),即可直接修改新密码
- 字段脱敏:不暴露系统角色
- 使用 get_jwt_identity() 获取用户 ID与项目其他接口保持一致
"""
try:
from app.models.system import SysUser
claims = get_jwt()
user_id = claims.get('sub')
# 【关键修复】使用 get_jwt_identity() 而非 claims.get('sub')
# 与项目其他接口outbound.py / scrap.py 等)保持一致,避免 JWT sub 字段取不到导致 500
user_id = get_jwt_identity()
data = request.get_json()
if not data:
@ -305,17 +306,16 @@ def change_my_password():
@jwt_required()
def get_my_profile():
"""
新增】获取当前登录用户的个人资料(自我查看)
重构】获取当前登录用户的个人资料(自我查看)
- 只返回姓名/账号和所属部门
- 严格脱敏:不暴露系统角色字段
- 使用 get_jwt_identity() 获取用户 ID
"""
try:
from app.models.system import SysUser
claims = get_jwt()
user_id = claims.get('sub')
display_name = claims.get('display_name', '')
account_id = claims.get('username', '')
# 【关键修复】统一使用 get_jwt_identity()
user_id = get_jwt_identity()
# 超级管理员user_id=0
if user_id == 0:
@ -338,8 +338,8 @@ def get_my_profile():
'msg': '获取成功',
'data': {
'id': user.id,
'username': account_id,
'display_name': user.username.split('/')[0] if user.username else display_name,
'username': user.username.split('/')[1] if '/' in user.username else user.username,
'display_name': user.username.split('/')[0] if '/' in user.username else user.username,
'department': user.department or '-',
# 【关键】严格脱敏:不暴露 role 字段
}