This commit is contained in:
2026-07-30 21:23:06 +08:00
parent 4e24d381c2
commit 4cef53e842
23 changed files with 493 additions and 54 deletions
+16 -4
View File
@@ -2,6 +2,7 @@ import { computed, ref } from 'vue'
import { defineStore } from 'pinia'
import type { CurrentUser } from '@chongchong/shared'
import { loginMerchant, logoutMerchant } from '@/api/auth'
import { ApiError } from '@/api/http'
import { getMerchantPermissionResources, getMerchantPersonalProfile, getTenantProfile, updateMerchantPersonalProfile, type MerchantPermissionResource } from '@/api/merchantIam'
import { clearSessionTokens, getAccessToken, getRefreshToken, persistTokenPair } from '@/api/session'
import { TENANT_PERMISSION_GROUPS } from '@/stores/tenantAdministration'
@@ -74,6 +75,10 @@ function currentUser(payload: MerchantJwtPayload, tenantName?: string): CurrentU
}
}
function isExpiredSessionError(error: unknown) {
return error instanceof ApiError && (error.status === 401 || error.code === 'A0401')
}
export const useAuthStore = defineStore('client-auth', () => {
const token = ref(getAccessToken())
const payload = ref<MerchantJwtPayload | null>(null)
@@ -139,8 +144,12 @@ export const useAuthStore = defineStore('client-auth', () => {
try {
await loadSessionContext()
} catch (error) {
clearSession()
throw error
if (isExpiredSessionError(error)) {
clearSession()
throw error
}
sessionRestored.value = true
return
}
sessionRestored.value = true
}
@@ -159,8 +168,11 @@ export const useAuthStore = defineStore('client-auth', () => {
await loadSessionContext()
sessionRestored.value = true
} catch (error) {
clearSession()
throw error
if (isExpiredSessionError(error)) {
clearSession()
throw error
}
sessionRestored.value = true
}
}