Files
estimation-flow/src/auth.ts
T

44 lines
1.3 KiB
TypeScript
Raw Normal View History

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,
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;
2025-09-01 07:06:07 +00:00
const userStmt = db.prepare('SELECT * FROM users WHERE email = ?');
const user = userStmt.get(email) as any;
2025-09-01 07:06:07 +00:00
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;
2025-09-01 07:05:09 +00:00
2025-09-01 07:06:07 +00:00
if (passwordsMatch) {
// The user object returned here will be encoded in the JWT.
return { id: user.id, name: user.name, email: user.email };
}
}
2025-09-01 07:06:07 +00:00
console.log('Invalid credentials');
return null;
},
}),
]
});
export const runtime = "nodejs";