fix: Improve settings persistence and auth handling
The changes address several issues related to data persistence and security within the Condopay application. **Settings Persistence:** - **Condo Creation:** Corrected the logic for creating new condos. The system now correctly handles passing an empty string for the `id` when creating a new condo, allowing the backend service to generate the ID, rather than attempting to create a new ID on the frontend. - **Family Quota Parsing:** Enhanced the parsing of `customMonthlyQuota` for families to safely handle empty or whitespace-only input, preventing potential errors during data submission. **Authentication and Authorization:** - **Admin Role Enforcement:** Ensured that the default admin user created during database initialization always has the 'admin' role, even if it was previously changed or created incorrectly. - **Token Verification Error Handling:** Modified the JWT token verification to return a `401 Unauthorized` status for all token-related errors (e.g., expired, invalid). This will prompt the frontend to log out the user more effectively. - **Admin Access Logging:** Added console warnings when non-admin users attempt to access admin-only routes, providing better visibility into potential access control issues. **Infrastructure:** - **Docker Cleanup:** Removed unused and outdated Dockerfiles and `.dockerignore` content, streamlining the build process and removing potential confusion. These improvements enhance the reliability of data management for condos and families, strengthen security by ensuring proper role enforcement and error handling, and clean up the development infrastructure.
This commit is contained in:
@@ -114,9 +114,6 @@ const initDb = async () => {
|
||||
if (!hasCondoId) {
|
||||
console.log('Migrating: Adding condo_id to families...');
|
||||
await connection.query("ALTER TABLE families ADD COLUMN condo_id VARCHAR(36)");
|
||||
if (DB_CLIENT !== 'postgres') { // Add FK for mysql specifically if needed, simplified here
|
||||
// await connection.query("ALTER TABLE families ADD CONSTRAINT fk_condo FOREIGN KEY (condo_id) REFERENCES condos(id)");
|
||||
}
|
||||
}
|
||||
if (!hasQuota) {
|
||||
console.log('Migrating: Adding custom_monthly_quota to families...');
|
||||
@@ -207,6 +204,7 @@ const initDb = async () => {
|
||||
);
|
||||
}
|
||||
|
||||
// ENSURE ADMIN EXISTS AND HAS CORRECT ROLE
|
||||
const [admins] = await connection.query('SELECT * FROM users WHERE email = ?', ['fcarra79@gmail.com']);
|
||||
if (admins.length === 0) {
|
||||
const hashedPassword = await bcrypt.hash('Mr10921.', 10);
|
||||
@@ -216,6 +214,10 @@ const initDb = async () => {
|
||||
[uuidv4(), 'fcarra79@gmail.com', hashedPassword, 'Amministratore', 'admin']
|
||||
);
|
||||
console.log('Default Admin user created.');
|
||||
} else {
|
||||
// Force update role to admin just in case it was changed or created wrongly
|
||||
await connection.query('UPDATE users SET role = ? WHERE email = ?', ['admin', 'fcarra79@gmail.com']);
|
||||
console.log('Ensured default user has admin role.');
|
||||
}
|
||||
|
||||
console.log('Database tables initialized.');
|
||||
|
||||
Reference in New Issue
Block a user