86 lines
2.5 KiB
JavaScript
86 lines
2.5 KiB
JavaScript
require('dotenv').config();
|
|
const mongoose = require('mongoose');
|
|
const User = require('./models/User');
|
|
|
|
const testLogin = async () => {
|
|
try {
|
|
// Connect to MongoDB
|
|
console.log('Connecting to MongoDB...');
|
|
console.log('MONGO_URI:', process.env.MONGO_URI);
|
|
|
|
await mongoose.connect(process.env.MONGO_URI, {
|
|
useNewUrlParser: true,
|
|
useUnifiedTopology: true
|
|
});
|
|
console.log('MongoDB connected');
|
|
|
|
// Test credentials
|
|
const email = 'admin@ininventer.com';
|
|
const password = 'admin123';
|
|
|
|
console.log(`Testing login with email: ${email} and password: ${password}`);
|
|
|
|
// Find the user
|
|
const user = await User.findOne({ email }).select('+password +password_plain');
|
|
|
|
if (!user) {
|
|
console.log('User not found!');
|
|
return;
|
|
}
|
|
|
|
console.log('User found:');
|
|
console.log('- ID:', user._id);
|
|
console.log('- Email:', user.email);
|
|
console.log('- Role:', user.role);
|
|
console.log('- Password (hashed):', user.password);
|
|
console.log('- Password (plain):', user.password_plain);
|
|
|
|
// Test password comparison
|
|
const isMatch = await user.comparePassword(password);
|
|
console.log(`Password comparison result: ${isMatch ? 'SUCCESS' : 'FAILED'}`);
|
|
|
|
// Test direct bcrypt comparison
|
|
const bcrypt = require('bcryptjs');
|
|
const bcryptMatch = await bcrypt.compare(password, user.password);
|
|
console.log(`Direct bcrypt comparison: ${bcryptMatch ? 'SUCCESS' : 'FAILED'}`);
|
|
|
|
// Create a test login function similar to the actual login controller
|
|
const testLoginFunction = async (email, password) => {
|
|
const user = await User.findOne({ email }).select('+password');
|
|
|
|
if (!user) {
|
|
return { success: false, message: 'User not found' };
|
|
}
|
|
|
|
const isMatch = await user.comparePassword(password);
|
|
|
|
if (!isMatch) {
|
|
return { success: false, message: 'Invalid credentials' };
|
|
}
|
|
|
|
return {
|
|
success: true,
|
|
user: {
|
|
_id: user._id,
|
|
email: user.email,
|
|
role: user.role
|
|
}
|
|
};
|
|
};
|
|
|
|
// Test the login function
|
|
const loginResult = await testLoginFunction(email, password);
|
|
console.log('Login function test result:', loginResult);
|
|
|
|
} catch (error) {
|
|
console.error('Error in test login:', error);
|
|
} finally {
|
|
// Disconnect from MongoDB
|
|
await mongoose.disconnect();
|
|
console.log('MongoDB disconnected');
|
|
}
|
|
};
|
|
|
|
// Run the test
|
|
testLogin();
|