fix oidc auth login
This commit is contained in:
+17
-15
@@ -42,7 +42,7 @@ const clearOIDCCallbackState = (path = '/') => {
|
|||||||
|
|
||||||
const syncOIDCUserContext = async () => {
|
const syncOIDCUserContext = async () => {
|
||||||
const currentUserResponse = await getCurrentUser()
|
const currentUserResponse = await getCurrentUser()
|
||||||
if (!currentUserResponse.success || !currentUserResponse.data?.user || !currentUserResponse.data?.tenant) {
|
if (!currentUserResponse.success || !currentUserResponse.data?.user) {
|
||||||
throw new Error(currentUserResponse.message || 'Failed to get user information')
|
throw new Error(currentUserResponse.message || 'Failed to get user information')
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,24 +52,26 @@ const syncOIDCUserContext = async () => {
|
|||||||
username: user.username || '',
|
username: user.username || '',
|
||||||
email: user.email || '',
|
email: user.email || '',
|
||||||
avatar: user.avatar,
|
avatar: user.avatar,
|
||||||
tenant_id: String(user.tenant_id || tenant.id || ''),
|
tenant_id: String(user.tenant_id || tenant?.id || ''),
|
||||||
can_access_all_tenants: user.can_access_all_tenants || false,
|
can_access_all_tenants: user.can_access_all_tenants || false,
|
||||||
created_at: user.created_at || new Date().toISOString(),
|
created_at: user.created_at || new Date().toISOString(),
|
||||||
updated_at: user.updated_at || new Date().toISOString()
|
updated_at: user.updated_at || new Date().toISOString()
|
||||||
})
|
})
|
||||||
authStore.setTenant({
|
if (tenant) {
|
||||||
id: String(tenant.id) || '',
|
authStore.setTenant({
|
||||||
name: tenant.name || '',
|
id: String(tenant.id) || '',
|
||||||
api_key: tenant.api_key || '',
|
name: tenant.name || '',
|
||||||
owner_id: tenant.owner_id || user.id || '',
|
api_key: tenant.api_key || '',
|
||||||
description: tenant.description,
|
owner_id: tenant.owner_id || user.id || '',
|
||||||
status: tenant.status,
|
description: tenant.description,
|
||||||
business: tenant.business,
|
status: tenant.status,
|
||||||
storage_quota: tenant.storage_quota,
|
business: tenant.business,
|
||||||
storage_used: tenant.storage_used,
|
storage_quota: tenant.storage_quota,
|
||||||
created_at: tenant.created_at || new Date().toISOString(),
|
storage_used: tenant.storage_used,
|
||||||
updated_at: tenant.updated_at || new Date().toISOString()
|
created_at: tenant.created_at || new Date().toISOString(),
|
||||||
})
|
updated_at: tenant.updated_at || new Date().toISOString()
|
||||||
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const persistOIDCLoginResponse = async (response: any) => {
|
const persistOIDCLoginResponse = async (response: any) => {
|
||||||
|
|||||||
@@ -208,10 +208,10 @@ export async function autoSetup(): Promise<LoginResponse> {
|
|||||||
/**
|
/**
|
||||||
* 获取当前用户信息
|
* 获取当前用户信息
|
||||||
*/
|
*/
|
||||||
export async function getCurrentUser(): Promise<{ success: boolean; data?: { user: UserInfo; tenant: TenantInfo }; message?: string }> {
|
export async function getCurrentUser(): Promise<{ success: boolean; data?: { user: UserInfo; tenant?: TenantInfo | null }; message?: string }> {
|
||||||
try {
|
try {
|
||||||
const response = await get('/api/v1/auth/me')
|
const response = await get('/api/v1/auth/me')
|
||||||
return response as unknown as { success: boolean; data?: { user: UserInfo; tenant: TenantInfo }; message?: string }
|
return response as unknown as { success: boolean; data?: { user: UserInfo; tenant?: TenantInfo | null }; message?: string }
|
||||||
} catch (error: any) {
|
} catch (error: any) {
|
||||||
return {
|
return {
|
||||||
success: false,
|
success: false,
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { createRouter, createWebHistory } from 'vue-router'
|
import { createRouter, createWebHistory } from 'vue-router'
|
||||||
import type { RouteLocationNormalized } from 'vue-router'
|
import type { RouteLocationNormalized } from 'vue-router'
|
||||||
import { useAuthStore } from '@/stores/auth'
|
import { useAuthStore } from '@/stores/auth'
|
||||||
import { autoSetup } from '@/api/auth'
|
import { autoSetup, getCurrentUser } from '@/api/auth'
|
||||||
|
|
||||||
/** Lite /桌面 WebView 硬刷新时可能只打开 `/`,用 session 记住上次页面以便恢复 */
|
/** Lite /桌面 WebView 硬刷新时可能只打开 `/`,用 session 记住上次页面以便恢复 */
|
||||||
const LITE_LAST_PATH_KEY = 'weknora_lite_last_path'
|
const LITE_LAST_PATH_KEY = 'weknora_lite_last_path'
|
||||||
@@ -32,6 +32,12 @@ function isSafeLiteRestoreTarget(path: string) {
|
|||||||
return path.startsWith('/platform/') && !path.startsWith('/platform/organizations')
|
return path.startsWith('/platform/') && !path.startsWith('/platform/organizations')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function hasPendingOIDCCallback() {
|
||||||
|
if (typeof window === 'undefined') return false
|
||||||
|
const hash = window.location.hash || ''
|
||||||
|
return hash.includes('oidc_result=') || hash.includes('oidc_error=')
|
||||||
|
}
|
||||||
|
|
||||||
const router = createRouter({
|
const router = createRouter({
|
||||||
history: createWebHistory(import.meta.env.BASE_URL),
|
history: createWebHistory(import.meta.env.BASE_URL),
|
||||||
routes: [
|
routes: [
|
||||||
@@ -169,6 +175,60 @@ function persistLoginResponse(authStore: ReturnType<typeof useAuthStore>, respon
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function hydrateSessionFromToken(authStore: ReturnType<typeof useAuthStore>) {
|
||||||
|
const token = localStorage.getItem('weknora_token')
|
||||||
|
if (!token) return false
|
||||||
|
|
||||||
|
if (!authStore.token) {
|
||||||
|
authStore.setToken(token)
|
||||||
|
}
|
||||||
|
|
||||||
|
const storedRefreshToken = localStorage.getItem('weknora_refresh_token')
|
||||||
|
if (storedRefreshToken && !authStore.refreshToken) {
|
||||||
|
authStore.setRefreshToken(storedRefreshToken)
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await getCurrentUser()
|
||||||
|
const user = response.data?.user
|
||||||
|
if (!response.success || !user) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
authStore.setUser({
|
||||||
|
id: user.id || '',
|
||||||
|
username: user.username || '',
|
||||||
|
email: user.email || '',
|
||||||
|
avatar: user.avatar,
|
||||||
|
tenant_id: String(user.tenant_id || response.data?.tenant?.id || ''),
|
||||||
|
can_access_all_tenants: user.can_access_all_tenants || false,
|
||||||
|
created_at: user.created_at || new Date().toISOString(),
|
||||||
|
updated_at: user.updated_at || new Date().toISOString(),
|
||||||
|
})
|
||||||
|
|
||||||
|
const tenant = response.data?.tenant
|
||||||
|
if (tenant) {
|
||||||
|
authStore.setTenant({
|
||||||
|
id: String(tenant.id) || '',
|
||||||
|
name: tenant.name || '',
|
||||||
|
api_key: tenant.api_key || '',
|
||||||
|
owner_id: tenant.owner_id || user.id || '',
|
||||||
|
description: tenant.description,
|
||||||
|
status: tenant.status,
|
||||||
|
business: tenant.business,
|
||||||
|
storage_quota: tenant.storage_quota,
|
||||||
|
storage_used: tenant.storage_used,
|
||||||
|
created_at: tenant.created_at || new Date().toISOString(),
|
||||||
|
updated_at: tenant.updated_at || new Date().toISOString(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
} catch {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let autoSetupAttempted = false
|
let autoSetupAttempted = false
|
||||||
let liteDeepLinkRestoreDone = false
|
let liteDeepLinkRestoreDone = false
|
||||||
|
|
||||||
@@ -176,6 +236,13 @@ let liteDeepLinkRestoreDone = false
|
|||||||
router.beforeEach(async (to, from, next) => {
|
router.beforeEach(async (to, from, next) => {
|
||||||
const authStore = useAuthStore()
|
const authStore = useAuthStore()
|
||||||
|
|
||||||
|
// OIDC 回跳登录结果依赖 App.vue 在挂载后消费 URL hash。
|
||||||
|
// 如果这里先按“未登录”拦截到 /login,会导致回调结果没有机会落盘。
|
||||||
|
if (hasPendingOIDCCallback()) {
|
||||||
|
next()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Lite:硬刷新后若落在默认首页,恢复本次会话中最后访问的 /platform 子路径
|
// Lite:硬刷新后若落在默认首页,恢复本次会话中最后访问的 /platform 子路径
|
||||||
if (!liteDeepLinkRestoreDone) {
|
if (!liteDeepLinkRestoreDone) {
|
||||||
liteDeepLinkRestoreDone = true
|
liteDeepLinkRestoreDone = true
|
||||||
@@ -204,6 +271,12 @@ router.beforeEach(async (to, from, next) => {
|
|||||||
// 检查用户认证状态
|
// 检查用户认证状态
|
||||||
if (to.meta.requiresAuth !== false) {
|
if (to.meta.requiresAuth !== false) {
|
||||||
if (!authStore.isLoggedIn) {
|
if (!authStore.isLoggedIn) {
|
||||||
|
const restored = await hydrateSessionFromToken(authStore)
|
||||||
|
if (restored) {
|
||||||
|
next(to.fullPath)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if (!autoSetupAttempted && shouldTryAutoSetup()) {
|
if (!autoSetupAttempted && shouldTryAutoSetup()) {
|
||||||
autoSetupAttempted = true
|
autoSetupAttempted = true
|
||||||
try {
|
try {
|
||||||
|
|||||||
Reference in New Issue
Block a user