fix: stabilize admin social redirect
This commit is contained in:
@@ -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"])
|
||||
def admin_auth_social_qr():
|
||||
value = str(request.args.get("data") or "").strip()
|
||||
|
||||
@@ -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() {
|
||||
const params = new URLSearchParams(window.location.search || '');
|
||||
const provider = String(params.get('provider') || params.get('type') || '').trim();
|
||||
@@ -703,17 +722,24 @@
|
||||
return;
|
||||
}
|
||||
showSuccess('登录成功,正在跳转...');
|
||||
window.setTimeout(() => {
|
||||
await waitForAdminSession();
|
||||
window.location.replace(data.redirect || '/yuyx/admin');
|
||||
}, 500);
|
||||
} catch (error) {
|
||||
showError('快捷登录失败');
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
return;
|
||||
}
|
||||
loadSocialConfig();
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
|
||||
Reference in New Issue
Block a user