Updated all routes (#12)

This commit is contained in:
2023-01-19 10:41:12 -05:00
committed by GitHub
parent eabb8199dc
commit ec9092abf3
6 changed files with 105 additions and 99 deletions

View File

@@ -2,9 +2,9 @@ const utils = require('../utils/utils');
const express = require('express');
const bcrypt = require('bcrypt');
const userRoutes = express.Router();
const config = require('../config.js');
const saltRounds = 10;
const sessionLength = 25;
let Session = require('../schema/session.model');
let User = require('../schema/user.model');
@@ -19,44 +19,44 @@ let Reset = require('../schema/reset.model');
*/
userRoutes.route('/create').post((req, res) => {
if (!req.body) {
res.status(401).send("Missing body");
res.status(401).json({ success: false, response: "Missing body" });
return;
} else if (!req.body.email || !req.body.password || !req.body.name) {
res.status(401).send("Missing body");
res.status(401).json({ success: false, response: "Missing fields" });
return;
} else if (req.body.email == "" || req.body.password == "" || req.body.name == "") {
res.status(401).send("Empty fields");
res.status(401).json({ success: false, response: "Empty fields" });
return;
}
let u = new User(req.body);
bcrypt.hash(u.password, saltRounds, (err, hash) => {
if (err) {
console.error(err);
res.status(500).send("Error creating user");;
res.status(500).json({ success: false, response: "Error creating user" });
} else {
u.password = hash;
User.find({ email: u.email }, (err, arr) => {
if (err) {
console.error(err);
res.status(500).send("Error creating user");
res.status(500).json({ success: false, response: "Error creating user" });
}
// Account already exists
if (arr.length != 0) {
res.status(409).send("Account already exists");
res.status(409).json({ success: false, response: "Account already exists" });
return;
}
u.permission = 0;
u.save()
.then(() => {
res.status(201).send("Success creating user");
res.status(201).json({ success: true, response: "Success creating user" });
})
.catch(() => {
res.status(500).send("Error creating user");;
res.status(500).json({ success: false, response: "Error creating user" });
});
});
}
});
});
/*
@@ -67,49 +67,57 @@ userRoutes.route('/create').post((req, res) => {
*/
userRoutes.route('/login').post((req, res) => {
if (!req.body) {
res.status(401).send("Missing body");
res.status(401).json({ success: false, response: "Missing body" });
return;
} else if (!req.body.email || !req.body.password) {
res.status(401).send("Missing body");
res.status(401).json({ success: false, response: "Missing body" });
return;
} else if (req.body.email == "" || req.body.password == "") {
res.status(401).send("Empty fields");
res.status(401).json({ success: false, response: "Empty fields" });
return;
}
User.findOne({ email: req.body.email }, (err, u) => {
if (err) {
console.error(err);
res.status(500).send("Error logging in user");
res.status(500).json({ success: false, response: "Error logging in user" });
return;
}
if (!u) {
res.status(401).send("No user exists with that email");
res.status(401).json({ success: false, response: "No user exists with that email" });
return;
}
bcrypt.compare(req.body.password, u.password, (err, result) => {
if (err) {
console.error(err);
res.status(500).send("Error logging in user");
res.status(500).json({ success: false, response: "Error logging in user" });
return;
}
if (result) {
let refresh = new Session();
refresh.sessionId = generateSession(config.refreshChracterLength);
refresh.userId = u._id;
refresh.date = new Date();
refresh.type = 1;
let s = new Session();
s.sessionId = generateSession();
s.sessionId = generateSession(config.sessionCharacterLength);
s.userId = u._id;
s.date = new Date();
s.type = 0;
s.save()
.then(() => {
res.json(s);
let send = { userId: u._id, sessionId: s.sessionId, refresh: refresh.sessionId}
res.status(200).json({ success: true, response: send});
})
.catch(() => {
res.status(500).send("Error logging in user");
res.status(500).json({ success: false, response: "Error logging in user"});
});
} else {
res.status(401).send("Incorrect password");
res.status(401).json({ success: false, response: "Incorrect password"});
}
});
@@ -126,24 +134,23 @@ userRoutes.route('/logout').post((req, res) => {
Session.findOne({ sessionId: req.body.sessionId }, (err, sess) => {
if (err) {
console.error(err);
res.status(500).send("Error logging out");
res.status(500).json({ success: false, response: "Error logging out" });
return;
}
if (!sess) {
res.status(400).send("No session found");
res.status(400).json({ success: false, response: "No session found" });
return;
}
sess.delete()
.then(() => {
res.status(201).send("Success deleting session");
res.status(201).json({ success: true, response: "Success deleting session" });
})
.catch((e) => {
console.error(e);
res.status(500).send("Error logging out");
res.status(500).json({ success: false, response: "Error logging out" });
});
});
});
@@ -161,21 +168,21 @@ userRoutes.route('/favorite/add').post((req, res) => {
User.findById(req.body.userId, (err, user) => {
if (err) {
console.error(err);
res.status(500).send("Error adding article");
res.status(500).json({ success: false, response: "Error adding article" });
return;
}
user.favorites.push(req.body.postId);
user.save()
.then(() => {
res.status(201).send("Success saving article");
res.status(201).json({ success: true, response: "Success saving article" });
})
.catch((e) => {
console.error(e);
res.status(500).send("Error saving article");
res.status(500).json({ success: false, response: "Error saving article" });
});
})
} else {
res.status(401).send("Unauthorized");
res.status(401).json({ success: false, response: "Unauthorized" });
}
})
})
@@ -192,21 +199,21 @@ userRoutes.route('/favorite/remove').post((req, res) => {
User.findById(req.body.userId, (err, user) => {
if (err) {
console.error(err);
res.status(500).send("Error removing article");
res.status(500).json({ success: false, response: "Error removing article" });
return;
}
user.favorites = utils.array.removeValue(user.favorites, req.body.articleId);
user.save()
.then(() => {
res.status(201).send("Success removing article");
res.status(201).json({ success: true, response: "Success removing article" });
})
.catch((e) => {
console.error(e);
res.status(500).send("Error removing article");
res.status(500).json({ success: false, response: "Error removing article" });
});
})
} else {
res.status(401).send("Unauthorized");
res.status(401).json({ success: false, response: "Unauthorized"});
}
})
})
@@ -223,15 +230,15 @@ userRoutes.route('/favorite/get').post((req, res) => {
User.findById(req.body.userId, (err, user) => {
if (err) {
console.error(err);
res.status(500).send("Error removing article");
res.status(500).json({ success: false, response: "Error removing article" });
return;
}
Post.find({ '_id': { $in: user.favorites } }, (err, postArray) => {
res.json(postArray);
res.status(200).json({ success: true, response: postArray });
})
})
} else {
res.status(401).send("Unauthorized");
res.status(401).json({ success: false, response: "Unauthorized" });
}
})
})
@@ -246,13 +253,13 @@ userRoutes.route('/refresh').post((req, res) => {
s.save()
.then(() => {
res.json(s);
res.status(200).json({ success: true, response: s });
})
.catch(() => {
res.status(500).send("Error logging in user");
res.status(500).json({ success: false, response: "Error logging in user" });
});
} else {
res.status(401).send("Incorrect refresh token");
res.status(401).json({ success: false, response: "Incorrect refresh token" });
}
}
)
@@ -265,9 +272,9 @@ userRoutes.route('/check-email').post((req, res) => {
}
if (arr.length > 0) {
res.status(400).send("Email already in use");
res.status(400).json({ success: false, response: "Email already in use" });
} else {
res.status(200).send("Email not in use");
res.status(200).json({ success: true, response: "Email not in use" });
}
})
});
@@ -282,13 +289,13 @@ userRoutes.route('/change-name').post((req, res) => {
if (user) {
user.name = req.body.name;
user.save();
res.status(200).send("Success changing name");
res.status(200).json({ success: true, response: "Success changing name" });
} else {
res.status(400).send("No user found with that ID");
res.status(400).json({ success: false, response: "No user found with that ID" });
}
})
} else {
res.status(401).send("Unauthorized");
res.status(401).json({ success: false, response: "Unauthorized" });
}
})
});
@@ -303,13 +310,13 @@ userRoutes.route('/change-email').post((req, res) => {
if (user) {
user.email = req.body.email;
user.save();
res.status(200).send("Success changing email");
res.status(200).json({ success: true, response: "Success changing email" });
} else {
res.status(400).send("No user found with that ID");
res.status(400).json({ success: false, response: "No user found with that ID" });
}
})
} else {
res.status(401).send("Unauthorized");
res.status(401).json({ success: false, response: "Unauthorized" });
}
})
});
@@ -324,13 +331,13 @@ userRoutes.route('/change-password').post((req, res) => {
if (user) {
user.password = req.body.password;
user.save();
res.status(200).send("Success changing password");
res.status(200).json({ success: true, response: "Success changing password" });
} else {
res.status(400).send("No user found with that ID");
res.status(400).json({ success: false, response: "No user found with that ID" });
}
})
} else {
res.status(401).send("Unauthorized");
res.status(401).json({ success: false, response: "Unauthorized" });
}
})
});
@@ -349,9 +356,9 @@ userRoutes.route('/forgot-password').post((req, res) => {
r.date = new Date();
r.save();
utils.mail.sendMail(user, "forgotPassword", [{from: "%name%", to: user.name}, {from: "%pin%", to: pin}]);
res.status(200).send("Success sending reset email");
res.status(200).json({ success: true, response: "Success sending reset email" });
} else {
res.status(400).send("No user found with that email");
res.status(400).json({ success: false, response: "No user found with that email" });
}
}
)
@@ -373,13 +380,13 @@ userRoutes.route('/reset-password').post((req, res) => {
reset.remove();
user.password = req.body.password;
user.save();
res.status(200).send("Success resetting password");
res.status(200).json({ success: true, response: "Success resetting password" });
} else {
res.status(400).send("Invalid pin");
res.status(400).json({ success: false, response: "Invalid pin" });
}
})
} else {
res.status(400).send("No user found with that email");
res.status(400).json({ success: false, response: "No user found with that email" });
}
}
)
@@ -389,8 +396,7 @@ userRoutes.route('/reset-password').post((req, res) => {
module.exports = userRoutes;
function generateSession() {
var length = sessionLength;
function generateSession(length) {
var result = [];
var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+!@#$%^&*()';
var charactersLength = characters.length;