Skip to content

Commit f1faf48

Browse files
committed
Debugging improvements
1 parent 4705335 commit f1faf48

7 files changed

Lines changed: 36 additions & 15 deletions

File tree

config/app.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ const APP_CONFIG = {
66
'MESSAGES' : {
77

88
}
9+
},
10+
'SECURITY' : {
11+
PROXY_CHECK : true,
12+
},
13+
'DEBUG' : {
14+
DEBUG_ENABLED : false,
15+
DEBUG_PREFIX : '[DEBUG]',
916
}
1017
}
1118

controllers/analyticsController.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const Site = require('../models/Sites');
77
let cors = require('cors');
88
const ipTools = require('../utils/ipTools');
99
const security = require('../utils/security');
10+
const config = require('../config/app');
1011
let corsOptions = {
1112
origin: function (origin, callback) {
1213
if (whitelist.indexOf(origin) !== -1) {
@@ -20,10 +21,12 @@ router.post('/sites/:id', async (req, res) => {
2021
// Get Site URL
2122

2223
try {
23-
const checkProxy = await security.isProxy(req.ip);
24-
if(checkProxy.isProxy){
25-
res.status(403).send({SecurityException : 'You are not allowed to send analytics data using Proxy'});
26-
return;
24+
if(config.APP_CONFIG.SECURITY.PROXY_CHECK){
25+
const checkProxy = await security.isProxy(req.ip);
26+
if(checkProxy.isProxy){
27+
res.status(403).send({SecurityException : 'You are not allowed to send analytics data using Proxy'});
28+
return;
29+
}
2730
}
2831
const siteUrl = await Site.findById(req.params.id).exec();
2932
if (siteUrl) {
@@ -58,7 +61,9 @@ router.post('/sites/:id', async (req, res) => {
5861
});
5962
}
6063
} else {
61-
console.log(false);
64+
if(config.APP_CONFIG.DEBUG.DEBUG_ENABLED){
65+
console.log(`${config.APP_CONFIG.DEBUG.DEBUG_PREFIX} This site does not exists!`);
66+
}
6267
}
6368
} catch (error) {
6469
res.send({

controllers/authController.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
const router = require("express").Router();
44

5+
const { APP_CONFIG } = require("../config/app");
56
const middlewares = require('../middlewares/auth');
67

78
const userServices = require('../services/userServices');
@@ -17,7 +18,9 @@ router.post('/login',middlewares.preventAuthenticatedUser,async (req,res) => {
1718
const token = await userServices.login(email,password);
1819

1920
if(token.hasOwnProperty('error')){
20-
console.log('Invalid username or password.');
21+
if(APP_CONFIG.DEBUG.DEBUG_ENABLED){
22+
console.log(`${APP_CONFIG.DEBUG.DEBUG_PREFIX} Invalid Username OR Password !`);
23+
}
2124
res.render('auth/login',{hasError:true,errorMessage:token.error});
2225

2326
return;
@@ -43,7 +46,9 @@ router.post('/register',middlewares.preventAuthenticatedUser,async(req,res) => {
4346
return;
4447
}
4548

46-
console.log(token);
49+
if(APP_CONFIG.DEBUG.DEBUG_ENABLED){
50+
console.log(`${APP_CONFIG.DEBUG.DEBUG_PREFIX} Token - ${token}`);
51+
}
4752
res.cookie("auth", token, { httpOnly: true });
4853

4954
res.redirect("/");

controllers/sitesController.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const middlewares = require('../middlewares/auth');
66
const siteServices = require('../services/siteServices');
77

88
const notificationServices = require('../services/notificationServices');
9+
const { APP_CONFIG } = require("../config/app");
910
router.get('/create', middlewares.protectedRoute, async (req, res) => {
1011

1112
res.render('sites/create');
@@ -52,8 +53,9 @@ router.get('/:id/manage', middlewares.protectedRoute, async (req, res) => {
5253

5354

5455
const mapped = JSON.stringify(siteData);
55-
56-
console.log(siteData.browser_list);
56+
if(APP_CONFIG.DEBUG.DEBUG_ENABLED){
57+
console.log(`${APP_CONFIG.DEBUG.DEBUG_PREFIX} Browser List ${siteData.browser_list}`);
58+
}
5759
if (hasAccess) {
5860
res.render('index', {
5961
siteData: JSON.parse(mapped),

middlewares/auth.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ exports.auth = async (req, res, next) => {
1616
res.locals.userImage = await utils.getAvatar(decodedToken.email);
1717
next();
1818
} catch (error) {
19-
console.log({ error });
2019
res.clearCookie("auth");
2120
res.redirect("/auth/login");
2221
}

services/siteServices.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
const User = require('../models/User');
22
const Site = require('../models/Sites');
3+
const { APP_CONFIG } = require('../config/app');
34
// const jwt = require('jsonwebtoken');
45
// const config = require('../database/config');
56
async function createSite(siteData,userId) {
@@ -21,8 +22,9 @@ async function createSite(siteData,userId) {
2122
// Add the created site to the user's sites array
2223
user.sites.push(site);
2324
await user.save();
24-
25-
console.log('Site Created!');
25+
if(APP_CONFIG.DEBUG.DEBUG_ENABLED){
26+
console.log(`${APP_CONFIG.DEBUG.DEBUG_PREFIX} Site Created!`);
27+
}
2628
return site;
2729
} catch (error) {
2830
console.error('Error creating site:', error);
@@ -71,8 +73,9 @@ async function checkUserAccess(req,res){
7173
return true;
7274

7375
} else {
74-
75-
console.log('User does not have access to the site');
76+
if(APP_CONFIG.DEBUG.DEBUG_ENABLED){
77+
console.log(`${APP_CONFIG.DEBUG.DEBUG_PREFIX} User does not have access to the site!`);
78+
}
7679
return false;
7780

7881
}

services/userServices.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ async function login(email,password){
4444
return {error:"Invalid email or password!"}
4545
}
4646

47-
console.log(isValid);
47+
4848
const payload = {
4949
_id: user._id,
5050
email: user.email,

0 commit comments

Comments
 (0)