From 23e8f298d39eccda243cadf57ea3e0a7ba1af281 Mon Sep 17 00:00:00 2001 From: Leon Serfaty G Date: Mon, 1 Sep 2025 07:06:52 +0000 Subject: [PATCH] Error Something went wrong. --- src/app/admin/settings/user/page.tsx | 1 - src/app/api/auth/[...nextauth]/route.ts | 3 --- src/auth.ts | 9 ++------- src/lib/actions/user.ts | 20 +++++++++++++++++++- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/src/app/admin/settings/user/page.tsx b/src/app/admin/settings/user/page.tsx index 797cc9c..51182f0 100644 --- a/src/app/admin/settings/user/page.tsx +++ b/src/app/admin/settings/user/page.tsx @@ -1,4 +1,3 @@ - 'use client'; import { useEffect } from 'react'; diff --git a/src/app/api/auth/[...nextauth]/route.ts b/src/app/api/auth/[...nextauth]/route.ts index 901e8d7..0a98352 100644 --- a/src/app/api/auth/[...nextauth]/route.ts +++ b/src/app/api/auth/[...nextauth]/route.ts @@ -1,6 +1,3 @@ - import { handlers } from '@/auth'; export const { GET, POST } = handlers; - -export const runtime = "nodejs"; diff --git a/src/auth.ts b/src/auth.ts index 8c6597d..2bde941 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,9 +1,8 @@ - import NextAuth from 'next-auth'; import { authConfig } from './auth.config'; import Credentials from 'next-auth/providers/credentials'; import { z } from 'zod'; -import db from '@/lib/db'; +import { getUserByEmail } from '@/lib/actions/user'; export const { handlers, auth, signIn, signOut } = NextAuth({ ...authConfig, @@ -17,9 +16,7 @@ export const { handlers, auth, signIn, signOut } = NextAuth({ if (parsedCredentials.success) { const { email, password } = parsedCredentials.data; - const userStmt = db.prepare('SELECT * FROM users WHERE email = ?'); - const user = userStmt.get(email) as any; - + const user = await getUserByEmail(email); if (!user) return null; // WARNING: Storing passwords in plaintext is insecure. @@ -39,5 +36,3 @@ export const { handlers, auth, signIn, signOut } = NextAuth({ }), ] }); - -export const runtime = "nodejs"; diff --git a/src/lib/actions/user.ts b/src/lib/actions/user.ts index 94228b9..02e6f24 100644 --- a/src/lib/actions/user.ts +++ b/src/lib/actions/user.ts @@ -1,4 +1,3 @@ - 'use server'; import { z } from 'zod'; @@ -14,6 +13,25 @@ const formSchema = z.object({ type UserFormValues = z.infer; +type User = { + id: string; + name: string | null; + email: string; + password?: string | null; +}; + +export async function getUserByEmail(email: string): Promise { + try { + const stmt = db.prepare('SELECT id, name, email, password FROM users WHERE email = ?'); + const user = stmt.get(email) as User | undefined; + return user || null; + } catch (error) { + console.error('Failed to get user by email:', error); + throw new Error('Database error while fetching user.'); + } +} + + /** * Gets the currently logged-in user from the session. */