|
| 1 | +'use server'; |
| 2 | + |
| 3 | +import bcrypt from 'bcryptjs'; |
| 4 | +import { getTranslations } from 'next-intl/server'; |
| 5 | +import { revalidatePath } from 'next/cache'; |
| 6 | + |
| 7 | +import { getUserProfile, updateUser } from '@/db/queries/users'; |
| 8 | +import { getCurrentUser } from '@/lib/auth'; |
| 9 | + |
| 10 | +import { createNotification } from './notifications'; |
| 11 | + |
| 12 | +export async function updateName(formData: FormData) { |
| 13 | + const session = await getCurrentUser(); |
| 14 | + if (!session) { |
| 15 | + return { error: 'Unauthorized' }; |
| 16 | + } |
| 17 | + |
| 18 | + const name = formData.get('name') as string; |
| 19 | + if (!name || name.trim().length === 0) { |
| 20 | + return { error: 'Name is required' }; |
| 21 | + } |
| 22 | + |
| 23 | + try { |
| 24 | + await updateUser(session.id, { name: name.trim() }); |
| 25 | + |
| 26 | + // Create notification |
| 27 | + const tNotify = await getTranslations('notifications.account'); |
| 28 | + await createNotification({ |
| 29 | + userId: session.id, |
| 30 | + type: 'SYSTEM', |
| 31 | + title: tNotify('nameChanged.title'), |
| 32 | + message: tNotify('nameChanged.message', { name: name.trim() }), |
| 33 | + }); |
| 34 | + |
| 35 | + revalidatePath('/[locale]/dashboard', 'page'); |
| 36 | + return { success: true }; |
| 37 | + } catch (error) { |
| 38 | + console.error('Failed to update name:', error); |
| 39 | + return { error: 'Failed to update name' }; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +export async function updatePassword(formData: FormData) { |
| 44 | + const session = await getCurrentUser(); |
| 45 | + if (!session) { |
| 46 | + return { error: 'Unauthorized' }; |
| 47 | + } |
| 48 | + |
| 49 | + const currentPassword = formData.get('currentPassword') as string; |
| 50 | + const newPassword = formData.get('newPassword') as string; |
| 51 | + |
| 52 | + if (!currentPassword || !newPassword) { |
| 53 | + return { error: 'Both current and new passwords are required' }; |
| 54 | + } |
| 55 | + |
| 56 | + if (newPassword.length < 8) { |
| 57 | + return { error: 'New password must be at least 8 characters long' }; |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + // Better to fetch specifically for verification |
| 62 | + const { db } = await import('@/db'); |
| 63 | + const { users } = await import('@/db/schema/users'); |
| 64 | + const { eq } = await import('drizzle-orm'); |
| 65 | + |
| 66 | + const dbUser = await db.query.users.findFirst({ |
| 67 | + where: eq(users.id, session.id), |
| 68 | + }); |
| 69 | + |
| 70 | + if (!dbUser || !dbUser.passwordHash) { |
| 71 | + return { error: 'Password not set for this account (Social Login?)' }; |
| 72 | + } |
| 73 | + |
| 74 | + const isValid = await bcrypt.compare(currentPassword, dbUser.passwordHash); |
| 75 | + if (!isValid) { |
| 76 | + return { error: 'Invalid current password' }; |
| 77 | + } |
| 78 | + |
| 79 | + const newPasswordHash = await bcrypt.hash(newPassword, 10); |
| 80 | + await updateUser(session.id, { passwordHash: newPasswordHash }); |
| 81 | + |
| 82 | + // Create notification |
| 83 | + const tNotify = await getTranslations('notifications.account'); |
| 84 | + await createNotification({ |
| 85 | + userId: session.id, |
| 86 | + type: 'SYSTEM', |
| 87 | + title: tNotify('passwordChanged.title'), |
| 88 | + message: tNotify('passwordChanged.message'), |
| 89 | + }); |
| 90 | + |
| 91 | + return { success: true }; |
| 92 | + } catch (error) { |
| 93 | + console.error('Failed to update password:', error); |
| 94 | + return { error: 'Failed to update password' }; |
| 95 | + } |
| 96 | +} |
0 commit comments