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 { 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;

View file

@ -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,