The npm install command failed in my project. Analyze the following er

This commit is contained in:
Leon Serfaty G
2025-09-01 06:34:12 +00:00
parent b76754d151
commit a61b22689f
12 changed files with 342 additions and 117 deletions
+16 -32
View File
@@ -1,44 +1,28 @@
'use server';
import { redirect } from 'next/navigation';
import { z } from 'zod';
import db from '@/lib/db';
const loginSchema = z.object({
email: z.string().email(),
password: z.string(),
});
export async function login(data: z.infer<typeof loginSchema>): Promise<{ success: boolean, message: string }> {
const validatedFields = loginSchema.safeParse(data);
if (!validatedFields.success) {
return { success: false, message: 'Invalid fields.' };
}
const { email, password } = validatedFields.data;
import { signIn, signOut } from '@/auth';
export async function login(
prevState: { message: string } | undefined,
formData: FormData
) {
try {
const stmt = db.prepare('SELECT * FROM users WHERE email = ? AND password = ?');
const user = stmt.get(email, password);
if (user) {
// In a real app, you would set a session cookie here.
// For this simulated login, we'll just return success.
return { success: true, message: 'Login successful.' };
} else {
return { success: false, message: 'Invalid email or password.' };
await signIn('credentials', formData);
} catch (error: any) {
if (error) {
switch (error.type) {
case 'CredentialsSignin':
return { message: 'Invalid credentials.' };
default:
return { message: 'Something went wrong.' };
}
}
} catch (error) {
console.error('Login error:', error);
return { success: false, message: 'An internal error occurred.' };
throw error;
}
}
export async function logout() {
// In a real app with authentication, this would handle signing out the user.
// For now, it redirects to the login page to simulate logging out.
redirect('/login');
await signOut({ redirectTo: '/login' });
}
+17 -15
View File
@@ -4,6 +4,7 @@
import { z } from 'zod';
import db from '@/lib/db';
import { revalidatePath } from 'next/cache';
import { auth } from '@/auth';
const formSchema = z.object({
name: z.string().min(1, 'Name is required'),
@@ -14,28 +15,29 @@ const formSchema = z.object({
type UserFormValues = z.infer<typeof formSchema>;
/**
* Gets the user from the database.
* Since authentication isn't fully implemented, it defaults to the user with id 1.
* Gets the currently logged-in user from the session.
*/
export async function getUser(): Promise<{ id: number; name: string; email: string } | null> {
try {
const stmt = db.prepare('SELECT id, name, email FROM users WHERE id = ?');
// For now, we'll hardcode the user ID to 1 as login is simulated.
const user = stmt.get(1) as { id: number; name: string; email: string } | undefined;
if (!user) {
return null;
}
return user;
} catch (error) {
console.error('Failed to get user:', error);
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) {
return null;
}
return {
id: session.user.id,
email: session.user.email,
name: session.user.name,
};
}
/**
* Updates a user's profile information in the database.
*/
export async function updateUser(data: UserFormValues): Promise<{ success: boolean; message: string }> {
const session = await auth();
if (!session?.user?.id) {
return { success: false, message: 'Not authenticated.' };
}
const validation = formSchema.safeParse(data);
if (!validation.success) {
return { success: false, message: 'Invalid data provided.' };
@@ -44,8 +46,7 @@ export async function updateUser(data: UserFormValues): Promise<{ success: boole
const { name, email, password } = validation.data;
try {
// For now, we'll assume we're updating the user with ID 1.
const userId = 1;
const userId = session.user.id;
// Check if the new email is already taken by another user
const checkEmailStmt = db.prepare('SELECT id FROM users WHERE email = ? AND id != ?');
@@ -58,6 +59,7 @@ export async function updateUser(data: UserFormValues): Promise<{ success: boole
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 = ?');
// In a real app, hash the password! For this example, we store it as plain text.
stmt.run(name, email, password, userId);
} else {
// If no new password, only update name and email