Fix 403 error in notification preferences and profile updates
- Handle both Clerk ID and database ID in authorization checks - Compare Clerk IDs when URL parameter is a Clerk ID (user_xxx) - Convert Clerk ID to database ID for service layer operations - Apply fix to both updateNotificationPreferences and updateUserProfile endpoints
This commit is contained in:
@@ -125,7 +125,7 @@
|
|||||||
- Add profile editing functionality
|
- Add profile editing functionality
|
||||||
- _Requirements: 6.3, 6.4, 6.5_
|
- _Requirements: 6.3, 6.4, 6.5_
|
||||||
|
|
||||||
- [-] 16. Implement notification preferences
|
- [x] 16. Implement notification preferences
|
||||||
- Create notification settings interface
|
- Create notification settings interface
|
||||||
- Build area of interest selector with map
|
- Build area of interest selector with map
|
||||||
- Implement radius configuration (1-50km)
|
- Implement radius configuration (1-50km)
|
||||||
|
|||||||
@@ -54,20 +54,40 @@ export class UserController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find or create user to get database ID
|
// Check if the ID in the URL is a Clerk ID or database ID
|
||||||
const dbUserId = await userService.findOrCreateUser(req.userId);
|
const isClerkId = id.startsWith('user_');
|
||||||
|
let dbUserId: string;
|
||||||
if (dbUserId !== id) {
|
|
||||||
return res.status(403).json({
|
// If it's a Clerk ID, it should match the authenticated user's Clerk ID
|
||||||
error: {
|
// If it's a database ID, we need to get the database ID for the authenticated user
|
||||||
code: 'FORBIDDEN',
|
if (isClerkId) {
|
||||||
message: 'You can only update your own profile',
|
// Direct Clerk ID comparison
|
||||||
timestamp: new Date().toISOString(),
|
if (req.userId !== id) {
|
||||||
},
|
return res.status(403).json({
|
||||||
});
|
error: {
|
||||||
|
code: 'FORBIDDEN',
|
||||||
|
message: 'You can only update your own profile',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Convert Clerk ID to database ID for the update
|
||||||
|
dbUserId = await userService.findOrCreateUser(req.userId);
|
||||||
|
} else {
|
||||||
|
// Database ID comparison - need to get the database ID for the authenticated user
|
||||||
|
dbUserId = await userService.findOrCreateUser(req.userId);
|
||||||
|
if (dbUserId !== id) {
|
||||||
|
return res.status(403).json({
|
||||||
|
error: {
|
||||||
|
code: 'FORBIDDEN',
|
||||||
|
message: 'You can only update your own profile',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedProfile = await userService.updateUserProfile(id, {
|
const updatedProfile = await userService.updateUserProfile(dbUserId, {
|
||||||
firstName,
|
firstName,
|
||||||
lastName,
|
lastName,
|
||||||
username,
|
username,
|
||||||
@@ -168,17 +188,38 @@ export class UserController {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find or create user to get database ID
|
// Check if the ID in the URL is a Clerk ID or database ID
|
||||||
const dbUserId = await userService.findOrCreateUser(req.userId);
|
const isClerkId = id.startsWith('user_');
|
||||||
|
|
||||||
if (dbUserId !== id) {
|
// If it's a Clerk ID, it should match the authenticated user's Clerk ID
|
||||||
return res.status(403).json({
|
// If it's a database ID, we need to get the database ID for the authenticated user
|
||||||
error: {
|
if (isClerkId) {
|
||||||
code: 'FORBIDDEN',
|
// Direct Clerk ID comparison
|
||||||
message: 'You can only update your own preferences',
|
if (req.userId !== id) {
|
||||||
timestamp: new Date().toISOString(),
|
return res.status(403).json({
|
||||||
},
|
error: {
|
||||||
});
|
code: 'FORBIDDEN',
|
||||||
|
message: 'You can only update your own preferences',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
// Convert Clerk ID to database ID for the update
|
||||||
|
const dbUserId = await userService.findOrCreateUser(req.userId);
|
||||||
|
// Update the id variable to use database ID for the service call
|
||||||
|
req.params.id = dbUserId;
|
||||||
|
} else {
|
||||||
|
// Database ID comparison - need to get the database ID for the authenticated user
|
||||||
|
const dbUserId = await userService.findOrCreateUser(req.userId);
|
||||||
|
if (dbUserId !== id) {
|
||||||
|
return res.status(403).json({
|
||||||
|
error: {
|
||||||
|
code: 'FORBIDDEN',
|
||||||
|
message: 'You can only update your own preferences',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate areasOfInterest if provided
|
// Validate areasOfInterest if provided
|
||||||
@@ -216,7 +257,7 @@ export class UserController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const updatedPreferences = await userService.updateNotificationPreferences(id, {
|
const updatedPreferences = await userService.updateNotificationPreferences(req.params.id, {
|
||||||
notificationEmail,
|
notificationEmail,
|
||||||
notificationInApp,
|
notificationInApp,
|
||||||
areasOfInterest,
|
areasOfInterest,
|
||||||
|
|||||||
Reference in New Issue
Block a user