Improve logging by using original path and method

This commit is contained in:
Adolfo Reyna
2025-02-08 07:52:52 -05:00
parent 6664dd5f89
commit 9f32ab01b8

View File

@@ -95,8 +95,8 @@ DB.getDB.then((DB) => {
// Log Reuquest
client_logger.capture({
distinctId: user_sid,
event: 'server@'+req.url,
})
event: 'server@'+req.method+'@'+req.originalUrl,
});
next();
} else {
return res.redirect('/login');
@@ -129,6 +129,10 @@ DB.getDB.then((DB) => {
if(!r) return res.json({status: "no invitation found with that email"});
let isUserAlreadyRegistered = await DB.getUser(email);
if(isUserAlreadyRegistered && isUserAlreadyRegistered._id) return res.json({status: "This user is already registered"});
client_logger.capture({
distinctId: 'app_level',
event: 'server@'+req.method+'@'+req.originalUrl,
});
return res.json({status: "ok", ... r});
});
@@ -140,7 +144,7 @@ DB.getDB.then((DB) => {
const password = req.query.password || req.body.password;
const email = req.query.email || req.body.email;
const profile = req.query.profile || req.body.profile;
if (!username || !password || !email) return res.json({ status: "fail" });
if (!username || !password || !email) return res.json({ status: "Incomplete information!" });
// Check if the new user has an invitation.
// TODO: Alert admin of signup attempts via email.
if (!await DB.getInvitation(email)) return res.json({ status: "Not invitation found!" });
@@ -163,6 +167,10 @@ DB.getDB.then((DB) => {
// Filter the provided information by the template, and adding to the DB, and login.
const userObj = new Profile(user);
await DB.newProfile(userObj);
client_logger.capture({
distinctId: newUserObject.insertedId,
event: 'server@'+req.method+'@'+req.originalUrl,
});
// TODO: this might fail, add catch scenarios
return await login(req, res);
}
@@ -195,6 +203,13 @@ DB.getDB.then((DB) => {
const username = req.body.username || req.query.username;
const password = req.body.password || req.query.password || "";
const user = await DB.getUser(username);
client_logger.capture({
distinctId: 'app_level',
event: 'server@'+req.method+'@'+req.originalUrl+'@userNotFound',
properties: {
username: username,
}
});
if (!user) return res.json({ status: "user not founded" });
// TODO: Also add salt parameter here.
const isSamePassword = await bcrypt.compare(password, user.password);
@@ -208,6 +223,10 @@ DB.getDB.then((DB) => {
// Chooses the most recent update profile as current active profile
const latestUpdatedProfile = await DB.latestProfile(user._id);
res.cookie('profile_id', latestUpdatedProfile._id, cookiesOptions);
client_logger.capture({
distinctId: user._id,
event: 'server@'+req.method+'@'+req.originalUrl,
});
return res.json({
status: "ok",
user_sid: user._id,
@@ -216,6 +235,10 @@ DB.getDB.then((DB) => {
});
} catch (error) {
console.error(error);
client_logger.capture({
distinctId: 'app_level',
event: 'server@'+req.method+'@'+req.originalUrl+'@error',
});
return res.json({ status: "Error on this User Profile, please contact admin." });
}
@@ -274,6 +297,13 @@ DB.getDB.then((DB) => {
<p>Blessings</p>
<p>Emmanuel International Ministries</p>
`)
client_logger.capture({
distinctId: user._id,
event: 'server@'+req.method+'@'+req.originalUrl,
properties: {
username: username,
}
});
return res.json({
status: "ok",
details: 'Check your email for new password' // Enum of details?
@@ -323,6 +353,10 @@ DB.getDB.then((DB) => {
res.clearCookie('user_sid');
//remove from DB
DB.removeSession(session_id);
client_logger.capture({
distinctId: user_sid,
event: 'server@'+req.method+'@'+req.originalUrl,
});
res.redirect('/');
} else {
res.redirect('/login');