feat: add profile link to header and implement user profile update functionality with email and password management
This commit is contained in:
58
src/actions/profile.ts
Normal file
58
src/actions/profile.ts
Normal file
@@ -0,0 +1,58 @@
|
||||
'use server';
|
||||
|
||||
import { auth } from '@/lib/auth';
|
||||
import { updateUserProfile, updateUserPassword, getUserById } from '@/services/auth';
|
||||
|
||||
export async function getProfileAction() {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return { success: false, error: 'Non authentifié', data: null };
|
||||
}
|
||||
|
||||
const user = await getUserById(session.user.id);
|
||||
if (!user) {
|
||||
return { success: false, error: 'Utilisateur non trouvé', data: null };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
data: {
|
||||
id: user.id,
|
||||
name: user.name,
|
||||
email: user.email,
|
||||
createdAt: user.createdAt,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export async function updateProfileAction(data: { name?: string; email?: string }) {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return { success: false, error: 'Non authentifié' };
|
||||
}
|
||||
|
||||
const result = await updateUserProfile(session.user.id, data);
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function updatePasswordAction(data: {
|
||||
currentPassword: string;
|
||||
newPassword: string;
|
||||
}) {
|
||||
const session = await auth();
|
||||
if (!session?.user?.id) {
|
||||
return { success: false, error: 'Non authentifié' };
|
||||
}
|
||||
|
||||
if (data.newPassword.length < 6) {
|
||||
return { success: false, error: 'Le nouveau mot de passe doit faire au moins 6 caractères' };
|
||||
}
|
||||
|
||||
const result = await updateUserPassword(
|
||||
session.user.id,
|
||||
data.currentPassword,
|
||||
data.newPassword
|
||||
);
|
||||
return result;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user