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',
userId: userId,
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
});
} catch (error) {

View file

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

View file

@ -143,7 +143,7 @@ export const useVerifyCode = () => {
return useMutation({
mutationFn: (verifyData) => authService.verifyCode(verifyData),
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');
},
onError: (error) => {

View file

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

View file

@ -39,7 +39,7 @@ import { format } from 'date-fns';
import EmailDialog from '@components/EmailDialog';
import { useAuth } from '@hooks/reduxHooks';
const AdminCustomersPage = () => {
const { user, isAuthenticated } = useAuth();
const { user, userData, isAuthenticated } = useAuth();
const [page, setPage] = useState(0);
const [rowsPerPage, setRowsPerPage] = useState(10);
const [search, setSearch] = useState('');
@ -329,12 +329,12 @@ const AdminCustomersPage = () => {
<Switch
checked={formData.is_disabled}
onChange={handleFormChange}
disabled={user === currentUser.id}
disabled={userData?.id === currentUser.id}
name="is_disabled"
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' }}
/>
<FormControlLabel
@ -342,12 +342,12 @@ const AdminCustomersPage = () => {
<Switch
checked={formData.is_admin}
onChange={handleFormChange}
disabled={user === currentUser.id && formData.is_admin}
disabled={userData?.id === currentUser.id && formData.is_admin}
name="is_admin"
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' }}
/>