fix(login): 修复登录后跳转及认证拦截问题
- request.ts: 优化401处理,避免登录接口误判过期 - router/index.ts: 路由守卫增加用户信息获取 - stores/user.ts: fetchUserInfo增强错误处理,login前先清理旧状态 - Login.vue: 使用await router.push,避免重复报错 - user_service.py: bootstrap superuser密码同步
This commit is contained in:
@@ -4,7 +4,7 @@ from fastapi import HTTPException, status
|
||||
|
||||
from app.models.user import User, Role, Dept, UserRole
|
||||
from app.schemas.user import UserCreate, UserUpdate
|
||||
from app.core.security import get_password_hash
|
||||
from app.core.security import get_password_hash, verify_password
|
||||
|
||||
|
||||
def get_user_by_id(db: Session, user_id: int) -> Optional[User]:
|
||||
@@ -105,9 +105,10 @@ def create_initial_data(db: Session):
|
||||
|
||||
db.commit()
|
||||
|
||||
# Create superuser
|
||||
# Create or sync the configured bootstrap superuser.
|
||||
from app.core.config import settings
|
||||
if not get_user_by_username(db, settings.FIRST_SUPERUSER_USERNAME):
|
||||
superuser = get_user_by_username(db, settings.FIRST_SUPERUSER_USERNAME)
|
||||
if not superuser:
|
||||
superuser = User(
|
||||
username=settings.FIRST_SUPERUSER_USERNAME,
|
||||
email=settings.FIRST_SUPERUSER_EMAIL,
|
||||
@@ -121,7 +122,28 @@ def create_initial_data(db: Session):
|
||||
db.commit()
|
||||
db.refresh(superuser)
|
||||
|
||||
superadmin_role = db.query(Role).filter(Role.code == "superadmin").first()
|
||||
if superadmin_role:
|
||||
db.add(UserRole(user_id=superuser.id, role_id=superadmin_role.id))
|
||||
else:
|
||||
changed = False
|
||||
if not verify_password(settings.FIRST_SUPERUSER_PASSWORD, superuser.hashed_password):
|
||||
superuser.hashed_password = get_password_hash(settings.FIRST_SUPERUSER_PASSWORD)
|
||||
changed = True
|
||||
if superuser.email != settings.FIRST_SUPERUSER_EMAIL:
|
||||
superuser.email = settings.FIRST_SUPERUSER_EMAIL
|
||||
changed = True
|
||||
if not superuser.is_active:
|
||||
superuser.is_active = True
|
||||
changed = True
|
||||
if not superuser.is_superuser:
|
||||
superuser.is_superuser = True
|
||||
changed = True
|
||||
if superuser.dept_id is None:
|
||||
superuser.dept_id = 1
|
||||
changed = True
|
||||
if changed:
|
||||
db.commit()
|
||||
db.refresh(superuser)
|
||||
|
||||
superadmin_role = db.query(Role).filter(Role.code == "superadmin").first()
|
||||
if superadmin_role and superadmin_role not in superuser.roles:
|
||||
db.add(UserRole(user_id=superuser.id, role_id=superadmin_role.id))
|
||||
db.commit()
|
||||
|
||||
Reference in New Issue
Block a user