diff --git a/pages/api/auth/login.js b/pages/api/auth/login.js index ad83914..ac2ec71 100644 --- a/pages/api/auth/login.js +++ b/pages/api/auth/login.js @@ -1,5 +1,8 @@ -import { db } from '../../../lib/database.js'; -import { verifyPassword, generateToken } from '../auth-utils.js'; +import bcrypt from 'bcryptjs'; +import jwt from 'jsonwebtoken'; +import { sql } from '@vercel/postgres'; + +const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production'; export default async function handler(req, res) { // Set CORS headers @@ -25,9 +28,11 @@ export default async function handler(req, res) { } // Get user from database - const result = await db.query(` - SELECT id, email, password, role FROM users WHERE email = $1 - `, [email]); + const result = await sql` + SELECT id, email, password, role, created_at + FROM users + WHERE email = ${email} + `; if (result.rows.length === 0) { return res.status(401).json({ error: 'Invalid credentials' }); @@ -36,17 +41,18 @@ export default async function handler(req, res) { const user = result.rows[0]; // Verify password - const isValid = await verifyPassword(password, user.password); - if (!isValid) { + const isValidPassword = await bcrypt.compare(password, user.password); + + if (!isValidPassword) { return res.status(401).json({ error: 'Invalid credentials' }); } - // Generate token - const token = generateToken({ - id: user.id, - email: user.email, - role: user.role - }); + // Generate JWT token + const token = jwt.sign( + { userId: user.id, email: user.email, role: user.role }, + JWT_SECRET, + { expiresIn: '24h' } + ); // Return user data (without password) and token const { password: _, ...userWithoutPassword } = user; diff --git a/pages/api/auth/register.js b/pages/api/auth/register.js index 8fd2c6d..dcf6c47 100644 --- a/pages/api/auth/register.js +++ b/pages/api/auth/register.js @@ -1,5 +1,8 @@ -import { db } from '../../../lib/database.js'; -import { hashPassword, generateToken } from '../auth-utils.js'; +import bcrypt from 'bcryptjs'; +import jwt from 'jsonwebtoken'; +import { sql } from '@vercel/postgres'; + +const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key-change-in-production'; export default async function handler(req, res) { // Set CORS headers @@ -29,37 +32,32 @@ export default async function handler(req, res) { } // Check if user already exists - const existingUser = await db.query(` - SELECT id FROM users WHERE email = $1 - `, [email]); + const existingUser = await sql` + SELECT id FROM users WHERE email = ${email} + `; if (existingUser.rows.length > 0) { return res.status(409).json({ error: 'User already exists' }); } // Hash password - const hashedPassword = await hashPassword(password); + const hashedPassword = await bcrypt.hash(password, 12); // Create user - const result = await db.query(` + const result = await sql` INSERT INTO users (email, password, role) - VALUES ($1, $2, $3) + VALUES (${email}, ${hashedPassword}, ${'user'}) RETURNING id, email, role, created_at - `, [email, hashedPassword, 'user']); + `; - const user = result.rows[0] || { - id: 1, - email, - role: 'user', - created_at: new Date().toISOString() - }; + const user = result.rows[0]; - // Generate token - const token = generateToken({ - id: user.id, - email: user.email, - role: user.role - }); + // Generate JWT token + const token = jwt.sign( + { userId: user.id, email: user.email, role: user.role }, + JWT_SECRET, + { expiresIn: '24h' } + ); res.status(201).json({ success: true,