diff --git a/src/lib/actions/user.ts b/src/lib/actions/user.ts index 298764b..4067ead 100644 --- a/src/lib/actions/user.ts +++ b/src/lib/actions/user.ts @@ -57,6 +57,7 @@ export async function updateUser( const userId = session.user.id; try { + // Check if the new email is already in use by another user const existingUserStmt = db.prepare('SELECT id FROM users WHERE email = ? AND id != ?'); const existingUser = existingUserStmt.get(email, userId); @@ -64,12 +65,15 @@ export async function updateUser( return { success: false, error: 'Email already in use by another account.' }; } + // Determine if a new password was provided if (password && password.trim().length > 0) { + // If a new password is provided, update name, email, and password const stmt = db.prepare( 'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?' ); stmt.run(name, email, password, userId); } else { + // If no new password, update only name and email const stmt = db.prepare('UPDATE users SET name = ?, email = ? WHERE id = ?'); stmt.run(name, email, userId); }