Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Session.auth = function (req, res, next) {
var sessionID = req.headers.authorization;
console.log(sessionID);
var sessionArray = sessionID.split(" ");
if (sessionArray[0] === "Bearer") {
var getSession = N1qlQuery.fromString("SELECT userID FROM `" + userBucketName + "` WHERE type = \"session\" AND sessionID = $1");
userBucket.query(getSession, [sessionArray[1]], function (error, result) {
if(error) {
callback(error, null);
return;
}
if (!result[0]) {
console.log("Session expired, please login again.");
res.send("Session expired, please login again.");
return;
}
req.userID = result[0].userID;
next();
});
}
};
Statistics.newGraph = function (timeUnit, callback) {
var graphObj = {};
var dayQuery = N1qlQuery.fromString("SELECT DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") AS deltaTime, COUNT(*) AS countTime from users UNNEST timeTracker.loginTimes AS time GROUP BY DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") HAVING DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") < 24 ORDER BY deltaTime");
var weekQuery = N1qlQuery.fromString("SELECT DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") AS deltaTime, COUNT(*) AS countTime from users UNNEST timeTracker.loginTimes AS time GROUP BY DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") HAVING DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") < 7 ORDER BY deltaTime");
if (timeUnit === 'day') {
userBucket.query(dayQuery, function (error, result) {
if (error) {
console.log(error);
return callback(error, null);
}
graphObj.logins = Array.apply(null, Array(24)).map(Number.prototype.valueOf,0);
graphObj.x = Array.apply(null, Array(24)).map(Number.prototype.valueOf,0);
var hoursX = ['12am', '1am', '2am', '3am', '4am', '5am', '6am', '7am', '8am', '9am', '10am', '11am', '12pm', '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm', '8pm', '9pm', '10pm', '11pm'];
var counter = 0;
var starter = moment().hour();
console.log(starter);
while (counter < 24) {
var index = 0;
if ((starter-counter) < 0) {
userBucket.query(findValidation, [verifyID], function (error, result) {
if (error) {
callback(error, null);
return;
}
console.log(result);
if (!result[0]) {
callback(null, 'email already verified/verification expired, please login again');
return;
}
var userID = result[0].users.userID;
var updateUserValidation = N1qlQuery.fromString('UPDATE '+userBucketName+' USE KEYS($1) SET login.emailVerified=true');
console.log(updateUserValidation);
userBucket.query(updateUserValidation, [userID], function (err, update) {
if (err) {
callback(err, null);
console.log(err);
return;
}
console.log(update);
var deleteVerify = N1qlQuery.fromString('DELETE FROM '+userBucketName+' USE KEYS ($1)');
userBucket.query(deleteVerify, [verifyID], function (error, deleteMessage) {
if (error) {
callback(error, null);
return;
}
console.log(deleteMessage);
callback(null, 'verifySession successfully deleted, and userModel updated');
User.addLoginTime = function(userID, callback) {
var currentTime = new Date().toISOString();
var addLoginTime = N1qlQuery.fromString("UPDATE " + userBucketName + " USE KEYS($2) SET timeTracker.loginTimes=ARRAY_PREPEND($1, timeTracker.loginTimes)");
console.log("addLoginTime: " + addLoginTime);
userBucket.query(addLoginTime, [currentTime, userID], function (err, result) {
if (err) {
console.log(err);
callback(err, null);
return;
}
callback(null, {message: "success", data: result});
});
};
Session.findUser = function (sessionID, callback) {
var findUser = N1qlQuery.fromString('SELECT userID FROM `'+userBucketName+'` WHERE sessionID=$1 AND type=\"session\"');
userBucket.query(findUser,[sessionID], function (error, result) {
if(error) {
callback(error, null);
}
console.log(result[0]);
callback(null, result[0].userID);
})
};
Statistics.newGraph = function (timeUnit, callback) {
var graphObj = {};
var dayQuery = N1qlQuery.fromString("SELECT DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") AS deltaTime, COUNT(*) AS countTime from "+userBucketName+" UNNEST timeTracker.loginTimes AS time GROUP BY DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") HAVING DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") < 24 ORDER BY deltaTime");
var weekQuery = N1qlQuery.fromString("SELECT DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") AS deltaTime, COUNT(*) AS countTime from "+userBucketName+" UNNEST timeTracker.loginTimes AS time GROUP BY DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") HAVING DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") < 7 ORDER BY deltaTime");
if (timeUnit === 'day') {
userBucket.query(dayQuery, function (error, result) {
if (error) {
console.log(error);
return callback(error, null);
}
graphObj.logins = Array.apply(null, Array(24)).map(Number.prototype.valueOf,0);
graphObj.x = Array.apply(null, Array(24)).map(Number.prototype.valueOf,0);
var hoursX = ['12am', '1am', '2am', '3am', '4am', '5am', '6am', '7am', '8am', '9am', '10am', '11am', '12pm', '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm', '8pm', '9pm', '10pm', '11pm'];
var counter = 0;
var starter = moment().hour();
console.log(starter);
while (counter < 24) {
var index = 0;
if ((starter-counter) < 0) {
User.addLoginTime = function(userID, callback) {
var currentTime = new Date().toISOString();
var addLoginTime = N1qlQuery.fromString("UPDATE `" + userBucketName + "` USE KEYS($2) SET timeTracker.loginTimes=ARRAY_PREPEND($1, timeTracker.loginTimes)");
console.log("addLoginTime: " + addLoginTime);
userBucket.query(addLoginTime, [currentTime, userID], function (err, result) {
if (err) {
console.log(err);
callback(err, null);
return;
}
callback(null, {message: "success", data: result});
});
};
var indexCreator = function(bucketname) {
var indexOnUsers = N1qlQuery.fromString("CREATE PRIMARY INDEX ON `" + bucketname + "`");
console.log(indexOnUsers);
return indexOnUsers;
};
userBucket.query(indexCreator(userBucketName), function (err, result) {
Statistics.newGraph = function (timeUnit, callback) {
var graphObj = {};
var dayQuery = N1qlQuery.fromString("SELECT DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") AS deltaTime, COUNT(*) AS countTime from users UNNEST timeTracker.loginTimes AS time GROUP BY DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") HAVING DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"hour\") < 24 ORDER BY deltaTime");
var weekQuery = N1qlQuery.fromString("SELECT DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") AS deltaTime, COUNT(*) AS countTime from users UNNEST timeTracker.loginTimes AS time GROUP BY DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") HAVING DATE_DIFF_STR(STR_TO_UTC(NOW_STR()), time, \"day\") < 7 ORDER BY deltaTime");
if (timeUnit === 'day') {
userBucket.query(dayQuery, function (error, result) {
if (error) {
console.log(error);
return callback(error, null);
}
graphObj.logins = Array.apply(null, Array(24)).map(Number.prototype.valueOf,0);
graphObj.x = Array.apply(null, Array(24)).map(Number.prototype.valueOf,0);
var hoursX = ['12am', '1am', '2am', '3am', '4am', '5am', '6am', '7am', '8am', '9am', '10am', '11am', '12pm', '1pm', '2pm', '3pm', '4pm', '5pm', '6pm', '7pm', '8pm', '9pm', '10pm', '11pm'];
var counter = 0;
var starter = moment().hour();
console.log(starter);
while (counter < 24) {
var index = 0;
if ((starter-counter) < 0) {
index = (starter-counter) + 24;
userBucket.query(findValidation, [verifyID], function (error, result) {
if (error) {
callback(error, null);
return;
}
console.log(result);
if (!result[0]) {
callback(null, 'email already verified/verification expired, please login again');
return;
}
var userID = result[0].userID;
var updateUserValidation = N1qlQuery.fromString('UPDATE '+userBucketName+' USE KEYS($1) SET login.emailVerified=true');
console.log(updateUserValidation);
userBucket.query(updateUserValidation, [userID], function (err, update) {
if (err) {
callback(err, null);
console.log(err);
return;
}
console.log(update);
var deleteVerify = N1qlQuery.fromString('DELETE FROM '+userBucketName+' USE KEYS ($1)');
userBucket.query(deleteVerify, [verifyID], function (error, deleteMessage) {
if (error) {
callback(error, null);
return;
}
console.log(deleteMessage);
callback(null, 'verifySession successfully deleted, and userModel updated');