Skip to content

Feature/bugfix branches #1242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion backend/.env
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#DATABASE = "mongodb://localhost:27017"
# DATABASE = "mongodb://localhost:27017"
DATABASE=mongodb+srv://penteAi:[email protected]/?retryWrites=true&w=majority&appName=Cluster0
#RESEND_API = "your resend_api"
#OPENAI_API_KEY = "your open_ai api key"
JWT_SECRET= "your_private_jwt_secret_key"
Expand Down
82 changes: 67 additions & 15 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"lodash": "^4.17.21",
"module-alias": "^2.2.3",
"moment": "^2.30.1",
"mongodb": "^6.15.0",
"mongoose": "^8.1.1",
"mongoose-autopopulate": "^1.1.0",
"multer": "^1.4.4",
Expand Down
11 changes: 5 additions & 6 deletions backend/src/app.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
const express = require('express');

const cors = require('cors');
const compression = require('compression');

const cookieParser = require('cookie-parser');
const fileUpload = require('express-fileupload');

// Load models first
require('./models/coreModels/Admin');
require('./models/coreModels/AdminPassword');

const coreAuthRouter = require('./routes/coreRoutes/coreAuth');
const coreApiRouter = require('./routes/coreRoutes/coreApi');
const coreDownloadRouter = require('./routes/coreRoutes/coreDownloadRouter');
const corePublicRouter = require('./routes/coreRoutes/corePublicRouter');
const adminAuth = require('./controllers/coreControllers/adminAuth');

const errorHandlers = require('./handlers/errorHandlers');
const erpApiRouter = require('./routes/appRoutes/appApi');

const fileUpload = require('express-fileupload');
// create our Express app
const app = express();

app.use(
Expand All @@ -28,7 +28,6 @@ app.use(
app.use(cookieParser());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));

app.use(compression());

// // default options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,60 @@ const bcrypt = require('bcryptjs');
const jwt = require('jsonwebtoken');

const authUser = async (req, res, { user, databasePassword, password, UserPasswordModel }) => {
const isMatch = await bcrypt.compare(databasePassword.salt + password, databasePassword.password);
try {
console.log('authUser received data:', {
userId: user?._id,
hasDatabasePassword: !!databasePassword,
passwordLength: password?.length,
hasSalt: !!databasePassword?.salt,
hasPassword: !!databasePassword?.password
});

if (!isMatch)
return res.status(403).json({
if (!databasePassword) {
console.error('databasePassword is null or undefined');
return res.status(500).json({
success: false,
result: null,
message: 'Invalid credentials.',
result: null,
message: 'Password record not found.',
});
}

if (!databasePassword.salt || !databasePassword.password) {
console.error('Invalid database password structure:', {
hasSalt: !!databasePassword.salt,
hasPassword: !!databasePassword.password
});
return res.status(500).json({
success: false,
result: null,
message: 'Invalid password record structure.',
});
}

// Log the values being compared
console.log('Comparing password with:', {
salt: databasePassword.salt,
passwordLength: password.length,
storedPasswordLength: databasePassword.password.length
});

if (isMatch === true) {
// Use bcrypt.compare to properly compare the passwords
const isMatch = await bcrypt.compare(databasePassword.salt + password, databasePassword.password);
console.log('Password comparison result:', isMatch);

if (!isMatch) {
console.log('Password mismatch. Details:', {
salt: databasePassword.salt,
passwordLength: password.length,
storedPasswordLength: databasePassword.password.length
});
return res.status(403).json({
success: false,
result: null,
message: 'Invalid credentials.',
});
}

const token = jwt.sign(
{
id: user._id,
Expand Down Expand Up @@ -51,11 +95,12 @@ const authUser = async (req, res, { user, databasePassword, password, UserPasswo
},
message: 'Successfully login user',
});
} else {
return res.status(403).json({
} catch (error) {
console.error('Error in authUser:', error);
return res.status(500).json({
success: false,
result: null,
message: 'Invalid credentials.',
message: 'An error occurred while processing the request.',
});
}
};
Expand Down
Loading