fix: improve sign-in error messages for both auth flows

- Check user_response?.detail (FastAPI standard) before user_response?.error
- Distinguish null response (network/server error) from bad credentials
- Remove silent console.error-only path; user now always sees a message
- Fix misleading 'auth_ae_obj__username_password' label in user_id+key flow
- Clarify 'no person record' message to suggest contacting administrator
- Simplify success log messages (remove dead commented-out code)
This commit is contained in:
Scott Idem
2026-03-06 22:42:15 -05:00
parent 79457103de
commit 0dab64a8d6

View File

@@ -477,16 +477,18 @@
user_obj = user_response; // Store the user object for later use user_obj = user_response; // Store the user object for later use
user_id = user_obj.user_id; // Use the user_id for further API calls user_id = user_obj.user_id; // Use the user_id for further API calls
// person_id = user_obj.person_id_random; // person_id = user_obj.person_id_random;
} else if (!user_response) {
alert('Sign in failed: No response from the server. Check your connection and try again.');
} else { } else {
alert('Failed to authenticate: ' + (user_response?.error || 'Unknown error')); // API returns 'detail' for validation errors (FastAPI standard), 'error' for app-level errors
const reason = user_response?.detail || user_response?.error || 'Invalid User ID or Auth Key.';
alert('Sign in failed: ' + reason);
} }
}) })
.then((response) => { .then((response) => {
if (!user_id) { if (!user_id) {
// If we didn't get a user_id, return early // Auth failed in the previous .then() — user has already been alerted
console.error( console.error('Auth (user_id+key): user_id not set after authentication attempt.');
'No user_id obtained from auth_ae_obj__username_password'
);
return; return;
} }
@@ -537,27 +539,17 @@
trigger = true; // Set trigger to true to indicate we can now sign in trigger = true; // Set trigger to true to indicate we can now sign in
} else { } else {
alert( alert(
'Failed to load person information. No person record found for this user.' 'Sign in failed: No person record is linked to this user account. Please contact your administrator.'
); );
} }
}) })
.then(() => { .then(() => {
// Once all promises are resolved, we can check if we have both user_id and person_id // Once all promises are resolved, we can check if we have both user_id and person_id
if (user_id && person_id) { if (user_id && person_id) {
// Set the session information console.log(`Successfully authenticated and loaded user and person records: user_id: ${user_id}, person_id: ${person_id}`);
// $ae_loc.person_id = person_id; // Set the person_id in the ae_loc store
// $ae_loc.person = person_obj; // Store the full person object for reference
// $ae_loc.user_id = user_id; // Set the user_id in the ae_loc store
// $ae_loc.user = user_obj; // Store the full user object for reference
console.log(`Successfully authenticated and loaded user and person records:
user_id: ${user_id},
person_id: ${person_id}
`);
// window.location.reload(); // Reload to get the new session
} else { } else {
console.error( console.error(
'Failed to authenticate and load data: missing user_id or person_id' 'Auth (user_id+key): finished but missing user_id or person_id — sign_in() will not be called.'
); );
} }
}); });
@@ -584,16 +576,18 @@
user_obj = user_response; // Store the user object for later use user_obj = user_response; // Store the user object for later use
user_id = user_obj.user_id; // Use the user_id for further API calls user_id = user_obj.user_id; // Use the user_id for further API calls
// person_id = user_obj.person_id_random; // person_id = user_obj.person_id_random;
} else if (!user_response) {
alert('Sign in failed: No response from the server. Check your connection and try again.');
} else { } else {
alert('Failed to authenticate: ' + (user_response?.error || 'Unknown error')); // API returns 'detail' for validation errors (FastAPI standard), 'error' for app-level errors
const reason = user_response?.detail || user_response?.error || 'Invalid username or password.';
alert('Sign in failed: ' + reason);
} }
}) })
.then((response) => { .then((response) => {
if (!user_id) { if (!user_id) {
// If we didn't get a user_id, return early // Auth failed in the previous .then() — user has already been alerted
console.error( console.error('Auth (username+password): user_id not set after authentication attempt.');
'No user_id obtained from auth_ae_obj__username_password'
);
return; return;
} }
@@ -644,27 +638,17 @@
trigger = true; // Set trigger to true to indicate we can now sign in trigger = true; // Set trigger to true to indicate we can now sign in
} else { } else {
alert( alert(
'Failed to load person information. No person record found for this user.' 'Sign in failed: No person record is linked to this user account. Please contact your administrator.'
); );
} }
}) })
.then(() => { .then(() => {
// Once all promises are resolved, we can check if we have both user_id and person_id // Once all promises are resolved, we can check if we have both user_id and person_id
if (user_id && person_id) { if (user_id && person_id) {
// Set the session information console.log(`Successfully authenticated and loaded user and person records: user_id: ${user_id}, person_id: ${person_id}`);
// $ae_loc.person_id = person_id; // Set the person_id in the ae_loc store
// $ae_loc.person = person_obj; // Store the full person object for reference
// $ae_loc.user_id = user_id; // Set the user_id in the ae_loc store
// $ae_loc.user = user_obj; // Store the full user object for reference
console.log(`Successfully authenticated and loaded user and person records:
user_id: ${user_id},
person_id: ${person_id}
`);
// window.location.reload(); // Reload to get the new session
} else { } else {
console.error( console.error(
'Failed to authenticate and load data: missing user_id or person_id' 'Auth (username+password): finished but missing user_id or person_id — sign_in() will not be called.'
); );
} }
}); });