returned users data on login

This commit is contained in:
2ManyProjects 2025-04-26 12:02:39 -05:00
parent b6447d4d21
commit b67da76dc6
5 changed files with 29 additions and 9 deletions

View file

@ -202,6 +202,9 @@ module.exports = (pool, query) => {
message: 'Login successful', message: 'Login successful',
userId: userId, userId: userId,
isAdmin: userInfo.rows[0].is_admin, isAdmin: userInfo.rows[0].is_admin,
firstname: userInfo.rows[0].first_name,
lastname: userInfo.rows[0].last_name,
email: userInfo.rows[0].email,
apiKey: apiKey apiKey: apiKey
}); });
} catch (error) { } catch (error) {

View file

@ -2,6 +2,7 @@ import { createSlice } from '@reduxjs/toolkit';
const initialState = { const initialState = {
user: JSON.parse(localStorage.getItem('user')) || null, user: JSON.parse(localStorage.getItem('user')) || null,
userData: JSON.parse(localStorage.getItem('userData')) || null,
apiKey: localStorage.getItem('apiKey') || null, apiKey: localStorage.getItem('apiKey') || null,
isAdmin: localStorage.getItem('isAdmin') === 'true', isAdmin: localStorage.getItem('isAdmin') === 'true',
isAuthenticated: !!localStorage.getItem('apiKey'), isAuthenticated: !!localStorage.getItem('apiKey'),
@ -21,11 +22,24 @@ export const authSlice = createSlice({
state.loading = false; state.loading = false;
state.isAuthenticated = true; state.isAuthenticated = true;
state.user = action.payload.user; state.user = action.payload.user;
state.userData = {
id: action.payload.user,
apiKey: action.payload.apiKey,
email: action.payload.email,
firstname: action.payload.firstname,
lastname: action.payload.lastname,
isAdmin: action.payload.isAdmin
};
state.apiKey = action.payload.apiKey; state.apiKey = action.payload.apiKey;
state.isAdmin = action.payload.isAdmin; state.isAdmin = action.payload.isAdmin;
localStorage.setItem('apiKey', action.payload.apiKey); localStorage.setItem('apiKey', action.payload.apiKey);
localStorage.setItem('isAdmin', action.payload.isAdmin); localStorage.setItem('isAdmin', action.payload.isAdmin);
localStorage.setItem('user', JSON.stringify(action.payload.user)); localStorage.setItem('user', JSON.stringify(action.payload.user));
localStorage.setItem('userData', JSON.stringify({
id: action.payload.user,
apiKey: action.payload.apiKey,
isAdmin: action.payload.isAdmin
}));
}, },
loginFailed: (state, action) => { loginFailed: (state, action) => {
state.loading = false; state.loading = false;
@ -34,11 +48,13 @@ export const authSlice = createSlice({
logout: (state) => { logout: (state) => {
state.isAuthenticated = false; state.isAuthenticated = false;
state.user = null; state.user = null;
state.userData = null;
state.apiKey = null; state.apiKey = null;
state.isAdmin = false; state.isAdmin = false;
localStorage.removeItem('apiKey'); localStorage.removeItem('apiKey');
localStorage.removeItem('isAdmin'); localStorage.removeItem('isAdmin');
localStorage.removeItem('user'); localStorage.removeItem('user');
localStorage.removeItem('userData');
}, },
clearError: (state) => { clearError: (state) => {
state.error = null; state.error = null;

View file

@ -143,7 +143,7 @@ export const useVerifyCode = () => {
return useMutation({ return useMutation({
mutationFn: (verifyData) => authService.verifyCode(verifyData), mutationFn: (verifyData) => authService.verifyCode(verifyData),
onSuccess: (data) => { onSuccess: (data) => {
login(data.userId, data.apiKey, data.isAdmin); login(data.userId, data.apiKey, data.isAdmin, data.email, data?.firstname, data?.lastname);
notification.showNotification('Login successful', 'success'); notification.showNotification('Login successful', 'success');
}, },
onError: (error) => { onError: (error) => {

View file

@ -60,6 +60,7 @@ export const useAuth = () => {
const isAuthenticated = useAppSelector((state) => state.auth.isAuthenticated); const isAuthenticated = useAppSelector((state) => state.auth.isAuthenticated);
const isAdmin = useAppSelector((state) => state.auth.isAdmin); const isAdmin = useAppSelector((state) => state.auth.isAdmin);
const user = useAppSelector((state) => state.auth.user); const user = useAppSelector((state) => state.auth.user);
const userData = useAppSelector((state) => state.auth.userData);
const apiKey = useAppSelector((state) => state.auth.apiKey); const apiKey = useAppSelector((state) => state.auth.apiKey);
const loading = useAppSelector((state) => state.auth.loading); const loading = useAppSelector((state) => state.auth.loading);
const error = useAppSelector((state) => state.auth.error); const error = useAppSelector((state) => state.auth.error);
@ -69,14 +70,14 @@ export const useAuth = () => {
isAuthenticated, isAuthenticated,
isAdmin, isAdmin,
user, user,
userData,
apiKey, apiKey,
loading, loading,
error, error,
login: (user, apiKey, isAdmin) => login: (user, apiKey, isAdmin, email, firstname = "", lastname = "") =>
dispatch({ dispatch({
type: 'auth/loginSuccess', type: 'auth/loginSuccess',
payload: { user, apiKey, isAdmin } payload: { user, apiKey, isAdmin, email, firstname, lastname}}),
}),
logout: () => dispatch({ type: 'auth/logout' }), logout: () => dispatch({ type: 'auth/logout' }),
clearError: () => dispatch({ type: 'auth/clearError' }), clearError: () => dispatch({ type: 'auth/clearError' }),
}; };

View file

@ -39,7 +39,7 @@ import { format } from 'date-fns';
import EmailDialog from '@components/EmailDialog'; import EmailDialog from '@components/EmailDialog';
import { useAuth } from '@hooks/reduxHooks'; import { useAuth } from '@hooks/reduxHooks';
const AdminCustomersPage = () => { const AdminCustomersPage = () => {
const { user, isAuthenticated } = useAuth(); const { user, userData, isAuthenticated } = useAuth();
const [page, setPage] = useState(0); const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10); const [rowsPerPage, setRowsPerPage] = useState(10);
const [search, setSearch] = useState(''); const [search, setSearch] = useState('');
@ -329,12 +329,12 @@ const AdminCustomersPage = () => {
<Switch <Switch
checked={formData.is_disabled} checked={formData.is_disabled}
onChange={handleFormChange} onChange={handleFormChange}
disabled={user === currentUser.id} disabled={userData?.id === currentUser.id}
name="is_disabled" name="is_disabled"
color="error" color="error"
/> />
} }
label={`${formData.is_disabled ? "Account is disabled" : "Account is active"}` + `${user === currentUser.id && formData.is_admin? " (Current user can\'t disabled themselves)" : "" }`} label={`${formData.is_disabled ? "Account is disabled" : "Account is active"}` + `${userData?.id === currentUser.id && formData.is_admin? " (Current user can\'t disabled themselves)" : "" }`}
sx={{ my: 2, display: 'block' }} sx={{ my: 2, display: 'block' }}
/> />
<FormControlLabel <FormControlLabel
@ -342,12 +342,12 @@ const AdminCustomersPage = () => {
<Switch <Switch
checked={formData.is_admin} checked={formData.is_admin}
onChange={handleFormChange} onChange={handleFormChange}
disabled={user === currentUser.id && formData.is_admin} disabled={userData?.id === currentUser.id && formData.is_admin}
name="is_admin" name="is_admin"
color="error" color="error"
/> />
} }
label={`${formData.is_admin ? "Account is Admin" : "Account is not Admin"}` + `${user === currentUser.id && formData.is_admin? " (Admin can't downgrade themselves)" : "" }`} label={`${formData.is_admin ? "Account is Admin" : "Account is not Admin"}` + `${userData?.id === currentUser.id && formData.is_admin? " (Admin can't downgrade themselves)" : "" }`}
sx={{ my: 2, display: 'block' }} sx={{ my: 2, display: 'block' }}
/> />