Skip to content

Commit

Permalink
Agent query: Can you try setting up your profile in first-time setup?…
Browse files Browse the repository at this point in the history
… Is the User ID mismatch error resolved?

Improve user profile update: remove redundant user ID check, add username uniqueness check, and handle new user profile creation.

Screenshot: https://storage.googleapis.com/screenshot-production-us-central1/322f7808-1b7c-4af4-b081-eca945479d9b/e043f138-931e-4783-ac05-16b1333310a7.jpg
thepavlosp committed Jan 26, 2025
1 parent b9d2434 commit e6e703e
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions server/routes.ts
Original file line number Diff line number Diff line change
@@ -310,16 +310,10 @@ export function registerRoutes(app: Express): Server {
// Update profile - requires auth
app.put("/api/users/profile", authenticateUser, async (req, res) => {
try {
const { username, bio, userId } = req.body;
const { username, bio } = req.body;
const userId = req.userId; // Use the authenticated user ID from middleware
console.log("Updating profile for user:", { username, bio, userId });

// Verify the authenticated user matches the requested user ID
if (userId !== req.userId) {
return res
.status(403)
.json({ error: "Unauthorized: User ID mismatch" });
}

if (!userId) {
return res.status(400).json({ error: "User ID is required" });
}
@@ -328,16 +322,45 @@ export function registerRoutes(app: Express): Server {
return res.status(400).json({ error: "Username is required" });
}

// Check if user exists
const [existingUser] = await db
.select()
.from(users)
.where(eq(users.id, userId))
.limit(1);

// Check username uniqueness before any user operations
const [userWithUsername] = await db
.select()
.from(users)
.where(eq(users.username, username))
.limit(1);

if (existingUser && existingUser.id !== userId) {
if (userWithUsername && userWithUsername.id !== userId) {
return res.status(400).json({ error: "Username is already taken" });
}

if (!existingUser) {
// For first-time setup, we should already have a user record from initial auth
// with at least an email. If not, that's an error in our auth flow.
console.log("Creating new user profile:", { userId, username, bio });
const [newUser] = await db
.insert(users)
.values({
id: userId,
username,
bio: bio || null,
email: req.body.email, // Email should be provided during first-time setup
currentChallenge: null,
})
.returning();

console.log("Created new user profile:", newUser);
return res.json(newUser);
}

// Update existing user
console.log("Updating existing user profile:", { userId, username, bio });
const [updatedUser] = await db
.update(users)
.set({

0 comments on commit e6e703e

Please sign in to comment.