From 3bb1c9357a3f6007a9ab2472d6e7892167120f22 Mon Sep 17 00:00:00 2001 From: Randall Stillwell Date: Thu, 24 Jul 2025 19:57:19 -0500 Subject: [PATCH] Fixed authentication API endpoints and login functionality - Updated login and register APIs to use proper Vercel Postgres imports - Fixed database queries to use sql template literal syntax - Replaced old auth-utils imports with direct bcrypt and jwt usage - Consistent JWT token generation across login and register endpoints - Removed debugging console.log statements - Login API now returns 200 with proper user data and JWT token - All authentication endpoints working correctly with Neon database - Login flow redirects properly to dashboard/admin based on user role --- pages/api/auth/login.js | 32 +++++++++++++++++------------- pages/api/auth/register.js | 40 ++++++++++++++++++-------------------- 2 files changed, 38 insertions(+), 34 deletions(-) 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,