task-manager-system / debug-auth.js
sarransh's picture
Upload batch 1/2
d1a980e verified
Raw History Blame Contribute Delete
1.78 kB
const { PrismaClient } = require('@prisma/client');
const prisma = new PrismaClient();
const bcrypt = require('bcryptjs');
async function debugAuth() {
try {
const email = 'admin@example.com';
const password = 'admin';
console.log(`Checking user: ${email}`);
const user = await prisma.user.findUnique({
where: { email }
});
if (!user) {
console.log('User NOT found in DB.');
console.log('Attempting to create user...');
const hashedPassword = await bcrypt.hash(password, 10);
await prisma.user.create({
data: {
email,
passwordHash: hashedPassword,
name: 'System Admin',
username: 'admin',
role: 'ADMIN'
}
});
console.log('User created successfully.');
} else {
console.log('User FOUND in DB.');
console.log('Stored Hash:', user.passwordHash);
const isValid = await bcrypt.compare(password, user.passwordHash);
console.log(`Password '${password}' is valid?`, isValid);
if (!isValid) {
console.log('Updating password to ensure it is correct...');
const newHash = await bcrypt.hash(password, 10);
await prisma.user.update({
where: { email },
data: { passwordHash: newHash }
});
console.log('Password updated.');
}
}
} catch (e) {
console.error('Error:', e);
} finally {
await prisma.$disconnect();
}
}
debugAuth();