deckhearth/pages/api/test-auth.js

51 lines
1.3 KiB
JavaScript
Raw Normal View History

import { hashPassword, verifyPassword, generateToken } from './auth-utils.js';
export default async function handler(req, res) {
// Set CORS headers
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization');
// Handle preflight requests
if (req.method === 'OPTIONS') {
res.status(200).end();
return;
}
try {
// Test password hashing
const password = 'test123';
const hashedPassword = await hashPassword(password);
const isValid = await verifyPassword(password, hashedPassword);
// Test token generation
const user = {
id: 1,
email: 'test@example.com',
role: 'user'
};
const token = generateToken(user);
res.status(200).json({
success: true,
message: 'Authentication utilities working!',
passwordTest: {
original: password,
hashed: hashedPassword,
isValid
},
tokenTest: {
token,
user
},
timestamp: new Date().toISOString()
});
} catch (error) {
console.error('Auth test error:', error);
res.status(500).json({
error: 'Authentication test failed',
details: error.message
});
}
}