fix: guard profile auth and logout
This commit is contained in:
@@ -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
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user