Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 31 additions & 32 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"overrides": {
"inflight": "npm:@isaacs/inflight@^1.0.6",
"glob": "10.5.0",
"validator": "13.15.20",
"validator": "^13.15.22",
"js-yaml": "3.14.2"
}
}
7 changes: 3 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,6 @@ import authRoutes from './src/routes/auth.js';
const app = express();
const server = http.createServer(app);


//Socket.io
setupSocket(server);

// Middleware
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
Expand Down Expand Up @@ -61,6 +57,9 @@ async function startServer() {
console.log('Initializing MinIO storage...');
await initializeStorage();

//Socket.io
await setupSocket(server);

// Start server
server.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
Expand Down
137 changes: 0 additions & 137 deletions src/__tests__/socket.test.js

This file was deleted.

30 changes: 28 additions & 2 deletions src/routes/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ if (!process.env.JWT_SECRET) {
const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production';
const JWT_EXPIRES_IN = process.env.JWT_EXPIRES_IN || '7d';

// Helper: get role-specific profile id (if present) from a user object
function getRoleSpecificId(user) {
if (!user || !user.role) return null;

// Profiles are included by userService with these aliases
if (user.role === 'technician' && user.technicianProfile && user.technicianProfile.id) {
return user.technicianProfile.id;
}

if (user.role === 'branch_manager' && user.branchManagerProfile && user.branchManagerProfile.id) {
return user.branchManagerProfile.id;
}

if (user.role === 'maintenance_executive' && user.maintenanceExecutiveProfile && user.maintenanceExecutiveProfile.id) {
return user.maintenanceExecutiveProfile.id;
}

return null;
}

/**
* @route POST /api/v1/auth/register
* @desc Register a new user
Expand Down Expand Up @@ -82,11 +102,14 @@ router.post('/register', async (req, res) => {
const newUser = await userService.createUser(userData, profileData);

// Generate JWT token
// include the role-specific profile id in the token as `roleId`
const roleSpecificId = getRoleSpecificId(newUser);
const token = jwt.sign(
{
id: newUser.id,
email: newUser.email,
role: newUser.role
role: newUser.role,
roleSpecificId: roleSpecificId
},
JWT_SECRET,
{ expiresIn: JWT_EXPIRES_IN }
Expand Down Expand Up @@ -168,11 +191,14 @@ router.post('/login', async (req, res) => {
}

// Generate JWT token
// include the role-specific profile id in the token as `roleId`
const roleSpecificId = getRoleSpecificId(user);
const token = jwt.sign(
{
id: user.id,
email: user.email,
role: user.role
role: user.role,
roleSpecificId: roleSpecificId
},
JWT_SECRET,
{ expiresIn: JWT_EXPIRES_IN }
Expand Down
Loading