again you are iether lying or simply incapable. dont try again

This commit is contained in:
Leon Serfaty G
2025-07-18 02:55:40 +00:00
parent 4ad283cf3b
commit 7de30c1eca
15 changed files with 2 additions and 1033 deletions
-85
View File
@@ -1,85 +0,0 @@
'use server';
import db from '@/lib/db';
import { z } from 'zod';
import { auth } from '@/app/api/auth/[...nextauth]/route';
import { User as DbUser } from '@/lib/types';
const UserUpdateSchema = z.object({
name: z.string().min(1, 'Name is required'),
email: z.string().email('Invalid email address'),
password: z.string().optional(),
});
type UserForClient = {
id: string;
name: string;
email: string;
}
export async function getUser(): Promise<UserForClient | null> {
const session = await auth();
if (!session?.user?.id) {
console.error('getUser: Not authenticated');
return null;
}
try {
const stmt = db.prepare('SELECT id, name, email FROM users WHERE id = ?');
const user = stmt.get(session.user.id) as DbUser | undefined;
if (!user) {
console.error('getUser: User not found in DB');
return null;
}
return { id: user.id.toString(), name: user.name, email: user.email };
} catch (error) {
console.error('Failed to get user:', error);
return null;
}
}
export async function updateUser(
data: z.infer<typeof UserUpdateSchema>
): Promise<{ success: boolean; error?: string }> {
const session = await auth();
if (!session?.user?.id) {
return { success: false, error: 'Not authenticated. Please log in again.' };
}
const validated = UserUpdateSchema.safeParse(data);
if (!validated.success) {
const errors = validated.error.flatten().fieldErrors;
const firstError = Object.values(errors)[0]?.[0] ?? 'Invalid data provided.';
return { success: false, error: firstError };
}
const { name, email, password } = validated.data;
const userId = session.user.id;
try {
// Check if the new email is already in use by another user
const existingUserStmt = db.prepare('SELECT id FROM users WHERE email = ? AND id != ?');
const existingUser = existingUserStmt.get(email, userId);
if (existingUser) {
return { success: false, error: 'Email already in use by another account.' };
}
// Determine if a new password was provided
if (password && password.trim().length > 0) {
// If a new password is provided, update name, email, and password
const stmt = db.prepare(
'UPDATE users SET name = ?, email = ?, password = ? WHERE id = ?'
);
stmt.run(name, email, password, userId);
} else {
// If no new password, update only name and email
const stmt = db.prepare('UPDATE users SET name = ?, email = ? WHERE id = ?');
stmt.run(name, email, userId);
}
return { success: true };
} catch (error: any) {
console.error('Failed to update user:', error);
return { success: false, error: 'Failed to update user profile due to a server error.' };
}
}