feat: enhance avatar handling and update TODO.md
- Added Avatar component with support for custom URLs and Gravatar integration, improving user profile visuals. - Implemented logic to determine avatar source based on user preferences in profile actions. - Updated ProfilePage to utilize the new Avatar component for better consistency. - Marked the integration of Gravatar and custom avatar handling as complete in TODO.md.
This commit is contained in:
@@ -4,12 +4,14 @@ import { getServerSession } from 'next-auth/next'
|
||||
import { authOptions } from '@/lib/auth'
|
||||
import { usersService } from '@/services/users'
|
||||
import { revalidatePath } from 'next/cache'
|
||||
import { getGravatarUrl } from '@/lib/gravatar'
|
||||
|
||||
export async function updateProfile(formData: {
|
||||
name?: string
|
||||
firstName?: string
|
||||
lastName?: string
|
||||
avatar?: string
|
||||
useGravatar?: boolean
|
||||
}) {
|
||||
try {
|
||||
const session = await getServerSession(authOptions)
|
||||
@@ -35,12 +37,27 @@ export async function updateProfile(formData: {
|
||||
return { success: false, error: 'L\'URL de l\'avatar ne peut pas dépasser 500 caractères' }
|
||||
}
|
||||
|
||||
// Déterminer l'URL de l'avatar
|
||||
let finalAvatarUrl: string | null = null
|
||||
|
||||
if (formData.useGravatar) {
|
||||
// Utiliser Gravatar si demandé
|
||||
finalAvatarUrl = getGravatarUrl(session.user.email || '', { size: 200 })
|
||||
} else if (formData.avatar) {
|
||||
// Utiliser l'URL custom si fournie
|
||||
finalAvatarUrl = formData.avatar
|
||||
} else {
|
||||
// Garder l'avatar actuel ou null
|
||||
const currentUser = await usersService.getUserById(session.user.id)
|
||||
finalAvatarUrl = currentUser?.avatar || null
|
||||
}
|
||||
|
||||
// Mettre à jour l'utilisateur
|
||||
const updatedUser = await usersService.updateUser(session.user.id, {
|
||||
name: formData.name || null,
|
||||
firstName: formData.firstName || null,
|
||||
lastName: formData.lastName || null,
|
||||
avatar: formData.avatar || null,
|
||||
avatar: finalAvatarUrl,
|
||||
})
|
||||
|
||||
// Revalider la page de profil
|
||||
@@ -101,3 +118,47 @@ export async function getProfile() {
|
||||
return { success: false, error: 'Erreur lors de la récupération du profil' }
|
||||
}
|
||||
}
|
||||
|
||||
export async function applyGravatar() {
|
||||
try {
|
||||
const session = await getServerSession(authOptions)
|
||||
|
||||
if (!session?.user?.id) {
|
||||
return { success: false, error: 'Non authentifié' }
|
||||
}
|
||||
|
||||
if (!session.user?.email) {
|
||||
return { success: false, error: 'Email requis pour Gravatar' }
|
||||
}
|
||||
|
||||
// Générer l'URL Gravatar
|
||||
const gravatarUrl = getGravatarUrl(session.user.email, { size: 200 })
|
||||
|
||||
// Mettre à jour l'utilisateur
|
||||
const updatedUser = await usersService.updateUser(session.user.id, {
|
||||
avatar: gravatarUrl,
|
||||
})
|
||||
|
||||
// Revalider la page de profil
|
||||
revalidatePath('/profile')
|
||||
|
||||
return {
|
||||
success: true,
|
||||
user: {
|
||||
id: updatedUser.id,
|
||||
email: updatedUser.email,
|
||||
name: updatedUser.name,
|
||||
firstName: updatedUser.firstName,
|
||||
lastName: updatedUser.lastName,
|
||||
avatar: updatedUser.avatar,
|
||||
role: updatedUser.role,
|
||||
createdAt: updatedUser.createdAt.toISOString(),
|
||||
lastLoginAt: updatedUser.lastLoginAt?.toISOString() || null,
|
||||
}
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('Gravatar update error:', error)
|
||||
return { success: false, error: 'Erreur lors de la mise à jour Gravatar' }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user