fix: stabilize admin social redirect

This commit is contained in:
237899745
2026-05-28 00:46:24 +08:00
parent 15823ee8a4
commit edc843951b
2 changed files with 45 additions and 4 deletions

View File

@@ -341,6 +341,21 @@ def admin_auth_social_callback():
) )
@api_social_bp.route("/yuyx/api/admin-auth/social/session", methods=["GET"])
def admin_auth_social_session():
admin_id = int(session.get("admin_id") or 0)
if not admin_id:
return jsonify({"authenticated": False}), 401
admin = database.get_admin_by_id(admin_id)
if not admin:
session.pop("admin_id", None)
session.pop("admin_username", None)
session.pop("admin_reauth_until", None)
session.modified = True
return jsonify({"authenticated": False}), 401
return jsonify({"authenticated": True, "username": admin.get("username") or ""})
@api_social_bp.route("/yuyx/api/admin-auth/social/qr", methods=["GET"]) @api_social_bp.route("/yuyx/api/admin-auth/social/qr", methods=["GET"])
def admin_auth_social_qr(): def admin_auth_social_qr():
value = str(request.args.get("data") or "").strip() value = str(request.args.get("data") or "").strip()

View File

@@ -683,6 +683,25 @@
} }
} }
async function waitForAdminSession(maxAttempts = 8) {
for (let attempt = 0; attempt < maxAttempts; attempt += 1) {
try {
const response = await fetch('/yuyx/api/admin-auth/social/session', {
method: 'GET',
credentials: 'same-origin',
cache: 'no-store'
});
if (response.ok) {
return true;
}
} catch (error) {
// retry below
}
await new Promise((resolve) => setTimeout(resolve, 180 + attempt * 120));
}
return false;
}
async function handleSocialCallback() { async function handleSocialCallback() {
const params = new URLSearchParams(window.location.search || ''); const params = new URLSearchParams(window.location.search || '');
const provider = String(params.get('provider') || params.get('type') || '').trim(); const provider = String(params.get('provider') || params.get('type') || '').trim();
@@ -703,17 +722,24 @@
return; return;
} }
showSuccess('登录成功,正在跳转...'); showSuccess('登录成功,正在跳转...');
window.setTimeout(() => { await waitForAdminSession();
window.location.replace(data.redirect || '/yuyx/admin'); window.location.replace(data.redirect || '/yuyx/admin');
}, 500);
} catch (error) { } catch (error) {
showError('快捷登录失败'); showError('快捷登录失败');
} }
} }
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
loadSocialConfig(); const params = new URLSearchParams(window.location.search || '');
const hasSocialCallback = Boolean(
String(params.get('code') || '').trim()
&& String(params.get('provider') || params.get('type') || '').trim()
);
if (hasSocialCallback) {
handleSocialCallback(); handleSocialCallback();
return;
}
loadSocialConfig();
}); });
</script> </script>
</body> </body>