2025-07-18 03:01:54 +00:00
|
|
|
|
|
|
|
|
'use server';
|
|
|
|
|
|
|
|
|
|
import { z } from 'zod';
|
|
|
|
|
import db from '@/lib/db';
|
|
|
|
|
import { revalidatePath } from 'next/cache';
|
2025-09-01 06:34:12 +00:00
|
|
|
import { auth } from '@/auth';
|
2025-07-18 03:01:54 +00:00
|
|
|
|
|
|
|
|
const formSchema = z.object({
|
|
|
|
|
name: z.string().min(1, 'Name is required'),
|
|
|
|
|
email: z.string().email('Invalid email address'),
|
|
|
|
|
password: z.string().optional(),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
type UserFormValues = z.infer<typeof formSchema>;
|
|
|
|
|
|
|
|
|
|
/**
|
2025-09-01 06:34:12 +00:00
|
|
|
* Gets the currently logged-in user from the session.
|
2025-07-18 03:01:54 +00:00
|
|
|
*/
|
2025-09-01 06:34:12 +00:00
|
|
|
export async function getUser(): Promise<{ id: string; name: string; email: string } | null> {
|
|
|
|
|
const session = await auth();
|
|
|
|
|
if (!session?.user?.id || !session.user.email || !session.user.name) {
|
2025-07-18 03:01:54 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
2025-09-01 06:34:12 +00:00
|
|
|
return {
|
|
|
|
|
id: session.user.id,
|
|
|
|
|
email: session.user.email,
|
|
|
|
|
name: session.user.name,
|
|
|
|
|
};
|
2025-07-18 03:01:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Updates a user's profile information in the database.
|
|
|
|
|
*/
|
|
|
|
|
export async function updateUser(data: UserFormValues): Promise<{ success: boolean; message: string }> {
|
2025-09-01 06:34:12 +00:00
|
|
|
const session = await auth();
|
|
|
|
|
if (!session?.user?.id) {
|
|
|
|
|
return { success: false, message: 'Not authenticated.' };
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-18 03:01:54 +00:00
|
|
|
const validation = formSchema.safeParse(data);
|
|
|
|
|
if (!validation.success) {
|
|
|
|
|
return { success: false, message: 'Invalid data provided.' };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { name, email, password } = validation.data;
|
|
|
|
|
|
|
|
|
|
try {
|
2025-09-01 06:34:12 +00:00
|
|
|
const userId = session.user.id;
|
2025-07-18 03:01:54 +00:00
|
|
|
|
|
|
|
|
// Check if the new email is already taken by another user
|
|
|
|
|
const checkEmailStmt = db.prepare('SELECT id FROM users WHERE email = ? AND id != ?');
|
|
|
|
|
const existingUser = checkEmailStmt.get(email, userId);
|
|
|
|
|
|
|
|
|
|
if (existingUser) {
|
|
|
|
|
return { success: false, message: 'This email is already taken.' };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (password) {
|
|
|
|
|
// If a new password is provided, update it along with name and email
|
|
|
|
|
const stmt = db.prepare('UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?');
|
2025-09-01 06:34:12 +00:00
|
|
|
// In a real app, hash the password! For this example, we store it as plain text.
|
2025-07-18 03:01:54 +00:00
|
|
|
stmt.run(name, email, password, userId);
|
|
|
|
|
} else {
|
|
|
|
|
// If no new password, only update name and email
|
|
|
|
|
const stmt = db.prepare('UPDATE users SET name = ?, email = ? WHERE id = ?');
|
|
|
|
|
stmt.run(name, email, userId);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
revalidatePath('/admin/settings/user');
|
|
|
|
|
return { success: true, message: 'Profile updated successfully!' };
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to update user:', error);
|
|
|
|
|
return { success: false, message: 'An unexpected error occurred.' };
|
|
|
|
|
}
|
|
|
|
|
}
|