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
This commit is contained in:
Randall Stillwell 2025-07-24 19:57:19 -05:00
parent 3478932c3c
commit 3bb1c9357a
2 changed files with 38 additions and 34 deletions

View file

@ -1,5 +1,8 @@
import { db } from '../../../lib/database.js'; import bcrypt from 'bcryptjs';
import { verifyPassword, generateToken } from '../auth-utils.js'; 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) { export default async function handler(req, res) {
// Set CORS headers // Set CORS headers
@ -25,9 +28,11 @@ export default async function handler(req, res) {
} }
// Get user from database // Get user from database
const result = await db.query(` const result = await sql`
SELECT id, email, password, role FROM users WHERE email = $1 SELECT id, email, password, role, created_at
`, [email]); FROM users
WHERE email = ${email}
`;
if (result.rows.length === 0) { if (result.rows.length === 0) {
return res.status(401).json({ error: 'Invalid credentials' }); return res.status(401).json({ error: 'Invalid credentials' });
@ -36,17 +41,18 @@ export default async function handler(req, res) {
const user = result.rows[0]; const user = result.rows[0];
// Verify password // Verify password
const isValid = await verifyPassword(password, user.password); const isValidPassword = await bcrypt.compare(password, user.password);
if (!isValid) {
if (!isValidPassword) {
return res.status(401).json({ error: 'Invalid credentials' }); return res.status(401).json({ error: 'Invalid credentials' });
} }
// Generate token // Generate JWT token
const token = generateToken({ const token = jwt.sign(
id: user.id, { userId: user.id, email: user.email, role: user.role },
email: user.email, JWT_SECRET,
role: user.role { expiresIn: '24h' }
}); );
// Return user data (without password) and token // Return user data (without password) and token
const { password: _, ...userWithoutPassword } = user; const { password: _, ...userWithoutPassword } = user;

View file

@ -1,5 +1,8 @@
import { db } from '../../../lib/database.js'; import bcrypt from 'bcryptjs';
import { hashPassword, generateToken } from '../auth-utils.js'; 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) { export default async function handler(req, res) {
// Set CORS headers // Set CORS headers
@ -29,37 +32,32 @@ export default async function handler(req, res) {
} }
// Check if user already exists // Check if user already exists
const existingUser = await db.query(` const existingUser = await sql`
SELECT id FROM users WHERE email = $1 SELECT id FROM users WHERE email = ${email}
`, [email]); `;
if (existingUser.rows.length > 0) { if (existingUser.rows.length > 0) {
return res.status(409).json({ error: 'User already exists' }); return res.status(409).json({ error: 'User already exists' });
} }
// Hash password // Hash password
const hashedPassword = await hashPassword(password); const hashedPassword = await bcrypt.hash(password, 12);
// Create user // Create user
const result = await db.query(` const result = await sql`
INSERT INTO users (email, password, role) INSERT INTO users (email, password, role)
VALUES ($1, $2, $3) VALUES (${email}, ${hashedPassword}, ${'user'})
RETURNING id, email, role, created_at RETURNING id, email, role, created_at
`, [email, hashedPassword, 'user']); `;
const user = result.rows[0] || { const user = result.rows[0];
id: 1,
email,
role: 'user',
created_at: new Date().toISOString()
};
// Generate token // Generate JWT token
const token = generateToken({ const token = jwt.sign(
id: user.id, { userId: user.id, email: user.email, role: user.role },
email: user.email, JWT_SECRET,
role: user.role { expiresIn: '24h' }
}); );
res.status(201).json({ res.status(201).json({
success: true, success: true,