fix: token 刷新时缺少 company_name 导致多租户过滤返回空数据
This commit is contained in:
@ -162,22 +162,25 @@ class AuthService:
|
|||||||
if not user_id:
|
if not user_id:
|
||||||
raise ValueError("无效的 refresh_token")
|
raise ValueError("无效的 refresh_token")
|
||||||
|
|
||||||
# 重新查询数据库获取用户的 display_name,避免刷新后丢失
|
# 重新查询数据库获取用户的 display_name + company_name,避免刷新后丢失
|
||||||
from app.models.system import SysUser
|
from app.models.system import SysUser
|
||||||
user = SysUser.query.get(user_id)
|
user = SysUser.query.get(user_id)
|
||||||
if user:
|
if user:
|
||||||
user_info = user.to_dict()
|
user_info = user.to_dict()
|
||||||
display_name = user_info.get('display_name', username)
|
display_name = user_info.get('display_name', username)
|
||||||
|
user_company = user.department or ''
|
||||||
else:
|
else:
|
||||||
display_name = username
|
display_name = username
|
||||||
|
user_company = ''
|
||||||
|
|
||||||
# 生成新的 access_token
|
# 生成新的 access_token(必须包含 company_name,否则多租户过滤失效)
|
||||||
new_access_token = create_access_token(
|
new_access_token = create_access_token(
|
||||||
identity=user_id,
|
identity=user_id,
|
||||||
additional_claims={
|
additional_claims={
|
||||||
'role': role,
|
'role': role,
|
||||||
'username': username,
|
'username': username,
|
||||||
'display_name': display_name
|
'display_name': display_name,
|
||||||
|
'company_name': user_company
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@ -16,28 +16,11 @@ const isLoginPage = computed(() => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// 页面加载时刷新权限
|
// 页面加载时刷新权限
|
||||||
onMounted(async () => {
|
onMounted(() => {
|
||||||
if (userStore.token) {
|
if (userStore.token) {
|
||||||
// ★ 如果 access_token 可能已过期,先主动用 refresh_token 换新 token
|
|
||||||
// 避免后续 API 调用触发 401 → 刷新 → 重试的竞态导致权限空白
|
|
||||||
try {
|
|
||||||
const refreshTokenValue = localStorage.getItem('refresh_token')
|
|
||||||
if (refreshTokenValue) {
|
|
||||||
const res = await fetch('/api/v1/auth/refresh', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: { 'Content-Type': 'application/json' },
|
|
||||||
body: JSON.stringify({ refresh_token: refreshTokenValue })
|
|
||||||
})
|
|
||||||
if (res.ok) {
|
|
||||||
const data = await res.json()
|
|
||||||
if (data.access_token) {
|
|
||||||
userStore.setToken(data.access_token)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (e) { /* 静默,失败时走正常 401 刷新流程 */ }
|
|
||||||
userStore.refreshUserPermissions()
|
userStore.refreshUserPermissions()
|
||||||
}
|
}
|
||||||
|
// 当 Vue 根组件挂载完毕,确保 Dify 图标一定会被加载
|
||||||
if (typeof (window as any).initDifyChatbot === 'function') {
|
if (typeof (window as any).initDifyChatbot === 'function') {
|
||||||
(window as any).initDifyChatbot()
|
(window as any).initDifyChatbot()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user