fix: tolerate flat (non-nested) success response shape from /admin/login
The live backend returns success:true + token but doesn't nest user fields under a data object the way the spec described, causing 'Cannot read properties of undefined (reading email)'. Falls back to reading fields off data.data, data.user, or the top-level response.
This commit is contained in:
@@ -53,11 +53,15 @@ const Login = () => {
|
||||
|
||||
if (res.data.success) {
|
||||
OpenToast('Login Successful', 'success', 1000);
|
||||
const { token, data } = res.data;
|
||||
// The live backend's success response doesn't nest user fields under
|
||||
// `data` the way the original spec described — fall back to reading
|
||||
// them off the top-level response if `data` isn't there.
|
||||
const token = res.data.token;
|
||||
const data = res.data.data || res.data.user || res.data;
|
||||
dispatch(setLoginUser(data));
|
||||
localStorage.setItem('authname', data.email || data.firstname);
|
||||
localStorage.setItem('userid', data.userid);
|
||||
localStorage.setItem('roleid', data.roleid);
|
||||
localStorage.setItem('authname', data.email || data.firstname || username);
|
||||
localStorage.setItem('userid', data.userid ?? data.id ?? '');
|
||||
localStorage.setItem('roleid', data.roleid ?? data.role ?? '');
|
||||
localStorage.setItem('token', token);
|
||||
axios.defaults.headers.common.Authorization = `Bearer ${token}`;
|
||||
markSessionStart();
|
||||
|
||||
Reference in New Issue
Block a user