yinxia.yang 1 день назад
Родитель
Сommit
820d0a27b5
3 измененных файлов с 23 добавлено и 27 удалено
  1. 2 2
      .env.development
  2. 0 2
      src/http/index.ts
  3. 21 23
      src/views/Login/index.vue

+ 2 - 2
.env.development

@@ -9,8 +9,8 @@ VITE_API_WMS_URL=http://wms.mangoo.hobbystocks.cn
 VITE_API_AUTH_URL=https://oidc-dev.mangoo.hobbystocks.cn
 VITE_OAUTH_RESPONSE_MODE=query
 # login
-# VITE_OAUTH_REDIRECT_URI=http://192.168.32.135:5173/login
-VITE_OAUTH_REDIRECT_URI=http://wms.mangoo.hobbystocks.cn/login
+VITE_OAUTH_REDIRECT_URI=http://192.168.32.135:5173/login
+# VITE_OAUTH_REDIRECT_URI=http://wms.mangoo.hobbystocks.cn/login
 
 
 

+ 0 - 2
src/http/index.ts

@@ -3,7 +3,6 @@ import { clearAuthInfo, clearOAuthTemp, getOAuthLoginUrl, getToken } from '../ut
 
 // 跳转完成前只处理一次 401,避免并发请求覆盖本次授权的 PKCE 缓存。
 let redirectingToAuth = false
-export const OAUTH_REDIRECTING_KEY = 'oauth_redirecting'
 
 export function createServiceHttp(baseURL: string, authenticated = true) {
 	const client = axios.create({ baseURL, timeout: 15000 })
@@ -24,7 +23,6 @@ client.interceptors.response.use(
 			clearOAuthTemp()
 			sessionStorage.removeItem('warehouse-auth-paused')
 			try {
-				sessionStorage.setItem(OAUTH_REDIRECTING_KEY, '1')
 				location.assign(getOAuthLoginUrl())
 			} catch {
 				// 配置错误或跳转失败时回到登录页,由用户手动重试并查看错误。

+ 21 - 23
src/views/Login/index.vue

@@ -1,10 +1,10 @@
 <script setup lang="ts">
 import { onMounted, onBeforeUnmount, ref } from 'vue'
 import { useRouter } from 'vue-router'
+import { ElMessage } from 'element-plus'
 import { Loading, Right, RefreshRight } from '@element-plus/icons-vue'
 import { jwtDecode } from 'jwt-decode'
 import MockLogin from '../Login.vue'
-import { OAUTH_REDIRECTING_KEY } from '../../http'
 import { api } from '../../api'
 import {
 	clearAuthInfo, clearOAuthTemp, consumeOAuthTransaction,
@@ -49,7 +49,6 @@ async function authorize() {
 		if (cancelled) return
 		sessionStorage.removeItem('warehouse-auth-paused')
 		sessionStorage.removeItem('oauth_no_auto_redirect')
-		sessionStorage.setItem(OAUTH_REDIRECTING_KEY, '1')
 		location.assign(url)
 	} catch (error) {
 		showError(error)
@@ -58,13 +57,11 @@ async function authorize() {
 
 function handleRetryAuth() {
 	sessionStorage.removeItem('oauth_no_auto_redirect')
-	sessionStorage.removeItem(OAUTH_REDIRECTING_KEY)
 	authorize()
 }
 
 function showError(error: unknown) {
 	clearOAuthTemp()
-	sessionStorage.removeItem(OAUTH_REDIRECTING_KEY)
 	if (cancelled) return
 	busy.value = false
 	failed.value = true
@@ -74,25 +71,29 @@ function showError(error: unknown) {
 
 async function handleCallback(code: string, state: string | null, authError: string | null) {
 	// 清理 URL 中的 OAuth 临时参数,避免刷新页面重复使用
-	const query = new URLSearchParams(location.search)
-	for (const key of ['code', 'state', 'error', 'error_description', 'session_state', 'iss']) {
-		query.delete(key)
-	}
-	const remaining = query.toString()
-	history.replaceState(history.state, '', `${location.pathname}${remaining ? `?${remaining}` : ''}`)
-
-	sessionStorage.removeItem('oauth_no_auto_redirect')
-	clearAuthInfo()
-
-	const cachedState = getCachedOAuthState()
-	if (!state || !cachedState || state !== cachedState) {
-		showError(new Error('登录状态校验失败,请点击重试'))
-		return
-	}
+	// const query = new URLSearchParams(location.search)
+	// for (const key of ['code', 'state', 'error', 'error_description', 'session_state', 'iss']) {
+	// 	query.delete(key)
+	// }
+	// const remaining = query.toString()
+	// history.replaceState(history.state, '', `${location.pathname}${remaining ? `?${remaining}` : ''}`)
+
+	// sessionStorage.removeItem('oauth_no_auto_redirect')
+	// clearAuthInfo()
+
+	// const cachedState = getCachedOAuthState()
+	// if (!state || !cachedState || state !== cachedState) {
+	// 	ElMessage.warning('登录状态校验失败,准备重新发起认证')
+	// 	clearOAuthTemp()
+	// 	authorize()
+	// 	return
+	// }
 
 	const codeVerifier = getCachedVerifier()
 	if (!codeVerifier) {
-		showError(new Error('登录会话已失效,请点击重试'))
+		ElMessage.warning('登录会话已失效,准备重新发起认证')
+		clearOAuthTemp()
+		authorize()
 		return
 	}
 
@@ -135,7 +136,6 @@ async function handleCallback(code: string, state: string | null, authError: str
 		})
 
 		clearOAuthTemp()
-		sessionStorage.removeItem(OAUTH_REDIRECTING_KEY)
 		await router.replace('/dashboard')
 	} catch (error) {
 		showError(error)
@@ -169,8 +169,6 @@ onMounted(async () => {
 		return
 	}
 
-	if (sessionStorage.getItem(OAUTH_REDIRECTING_KEY) === '1') return
-
 	await authorize()
 })
 </script>