feat: add profile link to header and implement user profile update functionality with email and password management

This commit is contained in:
Julien Froidefond
2025-11-27 13:37:18 +01:00
parent 10ff15392f
commit 873b3dd9f3
7 changed files with 417 additions and 0 deletions

View File

@@ -66,3 +66,81 @@ export async function getUserById(id: string) {
});
}
export interface UpdateProfileInput {
name?: string;
email?: string;
}
export async function updateUserProfile(
userId: string,
input: UpdateProfileInput
): Promise<AuthResult> {
const { name, email } = input;
// If changing email, check it's not already taken
if (email) {
const existingUser = await prisma.user.findFirst({
where: {
email,
NOT: { id: userId },
},
});
if (existingUser) {
return {
success: false,
error: 'Cet email est déjà utilisé par un autre compte',
};
}
}
const user = await prisma.user.update({
where: { id: userId },
data: {
...(name !== undefined && { name: name || null }),
...(email && { email }),
},
});
return {
success: true,
user: {
id: user.id,
email: user.email,
name: user.name,
},
};
}
export async function updateUserPassword(
userId: string,
currentPassword: string,
newPassword: string
): Promise<{ success: boolean; error?: string }> {
const { compare } = await import('bcryptjs');
const user = await prisma.user.findUnique({
where: { id: userId },
});
if (!user) {
return { success: false, error: 'Utilisateur non trouvé' };
}
// Verify current password
const isValid = await compare(currentPassword, user.password);
if (!isValid) {
return { success: false, error: 'Mot de passe actuel incorrect' };
}
// Hash new password
const hashedPassword = await hash(newPassword, 12);
await prisma.user.update({
where: { id: userId },
data: { password: hashedPassword },
});
return { success: true };
}