fix: preserve wechat bind resume state

This commit is contained in:
IanShaw027
2026-04-20 22:52:56 +08:00
parent 4d10ba4297
commit 7fdede579a
2 changed files with 60 additions and 5 deletions

View File

@@ -388,6 +388,15 @@ function resolveWeChatOAuthMode(): 'open' | 'mp' {
return /MicroMessenger/i.test(navigator.userAgent) ? 'mp' : 'open'
}
function normalizeWeChatOAuthMode(value: unknown): 'open' | 'mp' | null {
return value === 'open' || value === 'mp' ? value : null
}
function resolveRequestedWeChatOAuthMode(): 'open' | 'mp' {
const queryMode = normalizeWeChatOAuthMode(route.query.mode)
return queryMode || resolveWeChatOAuthMode()
}
function resolveRedirectTarget(): string {
return sanitizeRedirectPath(
(route.query.redirect as string | undefined) || redirectTo.value || '/dashboard'
@@ -398,7 +407,7 @@ function resolveWeChatStartURL(intent: 'bind_current_user' | 'adopt_existing_use
const apiBase = (import.meta.env.VITE_API_BASE_URL as string | undefined) || '/api/v1'
const normalized = apiBase.replace(/\/$/, '')
const params = new URLSearchParams({
mode: resolveWeChatOAuthMode(),
mode: resolveRequestedWeChatOAuthMode(),
redirect: resolveRedirectTarget(),
intent,
})
@@ -415,6 +424,7 @@ function buildExistingAccountResumePath(): string {
const params = new URLSearchParams({
wechat_bind_existing: '1',
redirect: resolveRedirectTarget(),
mode: resolveRequestedWeChatOAuthMode(),
})
const email = existingAccountEmail.value.trim()
@@ -727,9 +737,21 @@ onMounted(async () => {
existingAccountEmail.value = route.query.email
}
if (route.query.wechat_bind_existing === '1' && getAuthToken()) {
prepareOAuthBindAccessTokenCookie()
window.location.href = resolveWeChatStartURL('bind_current_user')
if (route.query.wechat_bind_existing === '1') {
if (getAuthToken()) {
prepareOAuthBindAccessTokenCookie()
window.location.href = resolveWeChatStartURL('bind_current_user')
return
}
const params = new URLSearchParams({
redirect: buildExistingAccountResumePath(),
})
const email = existingAccountEmail.value.trim()
if (email) {
params.set('email', email)
}
await router.replace(`/login?${params.toString()}`)
return
}