fix: guard profile auth and logout

This commit is contained in:
rain
2026-07-11 09:32:06 +08:00
parent 8b896f4ba3
commit f60cc67749
5 changed files with 82 additions and 4 deletions
+22 -2
View File
@@ -55,9 +55,23 @@
});
}
function performLogout(targetUrl) {
var api = window.GenealogyApi && window.GenealogyApi.defaultClient;
var redirect = function () {
window.location.href = targetUrl || 'index.html';
};
if (!api || !api.logout) {
redirect();
return;
}
api.logout().catch(function () {}).then(redirect);
}
function bindLogout() {
$(document).on('click', '[data-logout-open]', function (event) {
var targetUrl = $(this).attr('data-logout-url') || 'login.html';
var targetUrl = $(this).attr('data-logout-url') || 'index.html';
event.preventDefault();
if (!layerApi) {
@@ -70,11 +84,17 @@
content: '确认退出当前账号吗?退出后需要重新登录才能继续管理家谱。',
confirmText: '确认退出',
onConfirm: function () {
window.location.href = targetUrl;
performLogout(targetUrl);
}
});
});
$(document).on('click', '[data-logout-confirm]', function (event) {
event.preventDefault();
closeLegacyLogout();
performLogout($(this).attr('href') || 'index.html');
});
$(document).on('click', '[data-logout-close]', function () {
closeLegacyLogout();
});
+27
View File
@@ -92,6 +92,28 @@
return root.GenealogyApi && root.GenealogyApi.defaultClient;
}
function shouldRedirectToHome(api, error) {
var status = error && (error.status || error.code);
return !api || !api.getToken || !api.getToken() || Number(status) === 401;
}
function redirectToHome() {
if (!root.location) return;
if (typeof root.location.replace === 'function') {
root.location.replace('index.html');
return;
}
root.location.href = 'index.html';
}
function redirectUnauthorized(api, error) {
if (!shouldRedirectToHome(api, error)) return false;
if (api && api.clearToken) api.clearToken();
redirectToHome();
return true;
}
function query(selector, rootNode) {
if (!documentRef) return null;
return (rootNode || documentRef).querySelector(selector);
@@ -167,12 +189,14 @@
var profile;
if (!api || !documentRef || !documentRef.querySelector('[data-profile-page]')) return;
if (redirectUnauthorized(api)) return;
try {
profile = await api.currentProfile();
renderProfile(buildProfileView(profile));
fillProfileForm(profile);
} catch (error) {
if (redirectUnauthorized(api, error)) return;
showMessage(error.message || '个人资料加载失败');
}
}
@@ -181,6 +205,7 @@
var api = getApi();
if (!api || !form) return;
if (redirectUnauthorized(api)) return;
try {
setSubmitting(form, true);
@@ -190,6 +215,7 @@
showMessage('个人资料已保存');
await loadProfile();
} catch (error) {
if (redirectUnauthorized(api, error)) return;
setStatus(form, '保存失败');
showMessage(error.message || '个人资料保存失败');
} finally {
@@ -218,6 +244,7 @@
getAvatarText: getAvatarText,
buildProfileView: buildProfileView,
buildProfileUpdateBody: buildProfileUpdateBody,
shouldRedirectToHome: shouldRedirectToHome,
init: init
};
});