feat: implement auto-setup for Lite edition, enhancing user experience by automating user and tenant creation on first launch
This commit is contained in:
@@ -190,6 +190,21 @@ export async function register(data: RegisterRequest): Promise<RegisterResponse>
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Lite 版自动初始化(创建默认用户/租户 + 签发令牌)
|
||||
*/
|
||||
export async function autoSetup(): Promise<LoginResponse> {
|
||||
try {
|
||||
const response = await post('/api/v1/auth/auto-setup', {})
|
||||
return response as unknown as LoginResponse
|
||||
} catch (error: any) {
|
||||
return {
|
||||
success: false,
|
||||
message: error.message || 'Auto-setup unavailable'
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前用户信息
|
||||
*/
|
||||
|
||||
@@ -768,6 +768,11 @@ const onDragHandleMouseDown = (e: MouseEvent) => {
|
||||
transition: width 0.25s ease, min-width 0.25s ease;
|
||||
position: relative;
|
||||
|
||||
// macOS Wails 桌面:红绿灯位于 HiddenInset 标题栏区域,需让出顶部空间
|
||||
html.wails-desktop & {
|
||||
padding-top: 30px;
|
||||
}
|
||||
|
||||
&--collapsed {
|
||||
min-width: 60px;
|
||||
width: 60px;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
import { createRouter, createWebHistory } from 'vue-router'
|
||||
import { listKnowledgeBases } from '@/api/knowledge-base'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { validateToken } from '@/api/auth'
|
||||
import { validateToken, autoSetup } from '@/api/auth'
|
||||
|
||||
const router = createRouter({
|
||||
history: createWebHistory(import.meta.env.BASE_URL),
|
||||
@@ -105,6 +105,36 @@ const router = createRouter({
|
||||
],
|
||||
});
|
||||
|
||||
// 持久化 auto-setup / login 返回的认证信息到 store
|
||||
function persistLoginResponse(authStore: ReturnType<typeof useAuthStore>, response: any) {
|
||||
if (response.user && response.tenant && response.token) {
|
||||
authStore.setUser({
|
||||
id: response.user.id || '',
|
||||
username: response.user.username || '',
|
||||
email: response.user.email || '',
|
||||
avatar: response.user.avatar,
|
||||
tenant_id: String(response.tenant.id) || '',
|
||||
can_access_all_tenants: response.user.can_access_all_tenants || false,
|
||||
created_at: response.user.created_at || new Date().toISOString(),
|
||||
updated_at: response.user.updated_at || new Date().toISOString()
|
||||
})
|
||||
authStore.setToken(response.token)
|
||||
if (response.refresh_token) {
|
||||
authStore.setRefreshToken(response.refresh_token)
|
||||
}
|
||||
authStore.setTenant({
|
||||
id: String(response.tenant.id) || '',
|
||||
name: response.tenant.name || '',
|
||||
api_key: response.tenant.api_key || '',
|
||||
owner_id: response.user.id || '',
|
||||
created_at: response.tenant.created_at || new Date().toISOString(),
|
||||
updated_at: response.tenant.updated_at || new Date().toISOString()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
let autoSetupAttempted = false
|
||||
|
||||
// 路由守卫:检查认证状态和系统初始化状态
|
||||
router.beforeEach(async (to, from, next) => {
|
||||
const authStore = useAuthStore()
|
||||
@@ -123,26 +153,23 @@ router.beforeEach(async (to, from, next) => {
|
||||
// 检查用户认证状态
|
||||
if (to.meta.requiresAuth !== false) {
|
||||
if (!authStore.isLoggedIn) {
|
||||
// 未登录,跳转到登录页面
|
||||
// Lite 版:尝试自动初始化(仅一次),成功则跳过登录页
|
||||
if (!autoSetupAttempted) {
|
||||
autoSetupAttempted = true
|
||||
try {
|
||||
const response = await autoSetup()
|
||||
if (response.success) {
|
||||
persistLoginResponse(authStore, response)
|
||||
next(to.fullPath)
|
||||
return
|
||||
}
|
||||
} catch {
|
||||
// auto-setup 不可用(非 lite 版本),走正常登录流程
|
||||
}
|
||||
}
|
||||
next('/login')
|
||||
return
|
||||
}
|
||||
|
||||
// 验证Token有效性
|
||||
// try {
|
||||
// const { valid } = await validateToken()
|
||||
// if (!valid) {
|
||||
// // Token无效,清空认证信息并跳转到登录页面
|
||||
// authStore.logout()
|
||||
// next('/login')
|
||||
// return
|
||||
// }
|
||||
// } catch (error) {
|
||||
// console.error('Token验证失败:', error)
|
||||
// authStore.logout()
|
||||
// next('/login')
|
||||
// return
|
||||
// }
|
||||
}
|
||||
|
||||
next()
|
||||
|
||||
@@ -377,7 +377,7 @@ import { Autoplay, EffectFade, Pagination } from 'swiper/modules'
|
||||
import 'swiper/css'
|
||||
import 'swiper/css/effect-fade'
|
||||
import 'swiper/css/pagination'
|
||||
import { login, register, getOIDCAuthorizationURL, getOIDCConfig } from '@/api/auth'
|
||||
import { login, register, getOIDCAuthorizationURL, getOIDCConfig, autoSetup } from '@/api/auth'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
|
||||
@@ -668,13 +668,23 @@ const handleRegister = async () => {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if already logged in
|
||||
onMounted(() => {
|
||||
// Check if already logged in; for lite edition, attempt transparent auto-setup
|
||||
onMounted(async () => {
|
||||
if (authStore.isLoggedIn) {
|
||||
router.replace('/platform/knowledge-bases')
|
||||
return
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await autoSetup()
|
||||
if (response.success) {
|
||||
await persistLoginResponse(response)
|
||||
return
|
||||
}
|
||||
} catch {
|
||||
// auto-setup not available (standard edition) — show normal login form
|
||||
}
|
||||
|
||||
loadOIDCConfig()
|
||||
})
|
||||
</script>
|
||||
|
||||
Reference in New Issue
Block a user