diff --git a/local.db-shm b/local.db-shm new file mode 100644 index 0000000..24256b5 Binary files /dev/null and b/local.db-shm differ diff --git a/local.db-wal b/local.db-wal new file mode 100644 index 0000000..722cde6 Binary files /dev/null and b/local.db-wal differ diff --git a/src/auth.config.ts b/src/auth.config.ts index 55037cd..787e97e 100644 --- a/src/auth.config.ts +++ b/src/auth.config.ts @@ -9,36 +9,8 @@ export const authConfig = { signIn: '/login', }, providers: [ - Credentials({ - async authorize(credentials) { - const parsedCredentials = z - .object({ email: z.string().email(), password: z.string().min(1) }) - .safeParse(credentials); - - if (parsedCredentials.success) { - const { email, password } = parsedCredentials.data; - - try { - const userStmt = db.prepare('SELECT * FROM users WHERE email = ?'); - const user = userStmt.get(email) as any; - - if (!user) return null; - - // WARNING: Storing passwords in plaintext is insecure. - // This is for demonstration purposes only. - // In a real application, you MUST hash and salt passwords. - const passwordsMatch = password === user.password; - - if (passwordsMatch) return user; - } catch (e) { - console.error(e) - return null - } - } - - return null; - }, - }), + // The Credentials provider logic has been moved to src/auth.ts + // to prevent the database module from being bundled with middleware. ], callbacks: { authorized({ auth, request: { nextUrl } }) { diff --git a/src/auth.ts b/src/auth.ts index caf447f..bfa7472 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -1,7 +1,44 @@ 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'; -export const { handlers, auth, signIn, signOut } = NextAuth(authConfig); +export const { handlers, auth, signIn, signOut } = NextAuth({ + ...authConfig, + providers: [ + Credentials({ + async authorize(credentials) { + const parsedCredentials = z + .object({ email: z.string().email(), password: z.string().min(1) }) + .safeParse(credentials); + + if (parsedCredentials.success) { + const { email, password } = parsedCredentials.data; + + try { + const userStmt = db.prepare('SELECT * FROM users WHERE email = ?'); + const user = userStmt.get(email) as any; + + if (!user) return null; + + // WARNING: Storing passwords in plaintext is insecure. + // This is for demonstration purposes only. + // In a real application, you MUST hash and salt passwords. + const passwordsMatch = password === user.password; + + if (passwordsMatch) return user; + } catch (e) { + console.error(e) + return null + } + } + + return null; + }, + }), + ] +}); export const runtime = "nodejs";