update/save modified account settings
This commit is contained in:
2 files changed
+66
-6
No files matched your search
+56
-1
@@ -1,5 +1,7 @@
|
|||||||
const status = require("../util/statuses");
|
const status = require("../util/statuses");
|
||||||
|
const bcrypt = require('bcrypt');
|
||||||
const joi = require("joi");
|
const joi = require("joi");
|
||||||
|
const salt = 12;
|
||||||
|
|
||||||
module.exports = (middleware, users) => {
|
module.exports = (middleware, users) => {
|
||||||
const router = require("express").Router();
|
const router = require("express").Router();
|
||||||
@@ -27,7 +29,7 @@ module.exports = (middleware, users) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
router.get('/profile', (req, res) => {
|
router.get('/profile', (req, res) => {
|
||||||
res.render('profile', { user: req.user });
|
res.render('profile', { user: req.user, errMessage: req.session.errMessage });
|
||||||
return res.status(status.Ok);
|
return res.status(status.Ok);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -106,5 +108,58 @@ module.exports = (middleware, users) => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
router.post("/updateAccount", async (req, res) => {
|
||||||
|
const accountSchema = joi.object({
|
||||||
|
email: joi.string().email(),
|
||||||
|
name: joi.string().alphanum().max(20),
|
||||||
|
password: joi.string().max(20).min(8),
|
||||||
|
repassword: joi.string().max(20).min(8),
|
||||||
|
});
|
||||||
|
|
||||||
|
const valid = accountSchema.validate(req.body);
|
||||||
|
|
||||||
|
if (valid.err) {
|
||||||
|
req.session.errMessage = "Invalid input",
|
||||||
|
res.status(status.BadRequest);
|
||||||
|
return res.redirect("/profile");
|
||||||
|
}
|
||||||
|
|
||||||
|
let update = {
|
||||||
|
// email: req.body.email,
|
||||||
|
name: req.body.name,
|
||||||
|
};
|
||||||
|
|
||||||
|
if ((req.body.password != "") && (req.body.password != req.body.repassword)) {
|
||||||
|
req.session.errMessage = "New passwords must match";
|
||||||
|
res.status(status.BadRequest);
|
||||||
|
return res.redirect("/profile");
|
||||||
|
} else if (req.body.password != "") {
|
||||||
|
let hashedPassword = await bcrypt.hashSync(req.body.password, salt);
|
||||||
|
update.password = hashedPassword;
|
||||||
|
}
|
||||||
|
|
||||||
|
users.updateOne(
|
||||||
|
{ email: req.session.email },
|
||||||
|
{ $set: update }
|
||||||
|
).then((result) => {
|
||||||
|
if (result.matchedCount === 0) {
|
||||||
|
console.log(`User not found during account update: ${req.session.email}`);
|
||||||
|
req.session.errMessage = "User session invalid. Please log in again.";
|
||||||
|
res.status(status.NotFound).redirect("/login");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (result.modifiedCount === 0 && result.matchedCount === 1) {
|
||||||
|
console.log(`User account data unchanged (already up-to-date): ${req.session.email}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
req.session.errMessage = "";
|
||||||
|
return res.status(status.Ok).redirect("/profile");
|
||||||
|
}).catch(err => {
|
||||||
|
console.error("Error updating account in database:", err);
|
||||||
|
req.session.errMessage = "An error occurred while saving your information. Please try again.";
|
||||||
|
return res.status(status.InternalServerError).redirect("/profile");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return router;
|
return router;
|
||||||
};
|
};
|
||||||
+10
-5
@@ -2,7 +2,12 @@
|
|||||||
<%- include("./partials/header") %>
|
<%- include("./partials/header") %>
|
||||||
|
|
||||||
<main>
|
<main>
|
||||||
<h2 class="ml-11 mt-5 mb-6 text-xl font-semibold mb-4 text-white">Welcome: <%= user.name %></h2>
|
<h2 class="ml-11 mt-5 mb-6 text-xl font-semibold mb-4 text-white">Welcome <%= user.name %></h2>
|
||||||
|
|
||||||
|
<% if (errMessage != "") { %>
|
||||||
|
<div class="text-white font-bold bg-red-400 h-5 px-8 py-4"> <%= errMessage %></div>
|
||||||
|
<% } %>
|
||||||
|
|
||||||
<div class="max-w-md mx-auto bg-white p-8 mb-10 rounded-lg shadow-md">
|
<div class="max-w-md mx-auto bg-white p-8 mb-10 rounded-lg shadow-md">
|
||||||
<div class="flex flex-row justify-between">
|
<div class="flex flex-row justify-between">
|
||||||
<h3 class="pt-2 pr-2 pb-2">Account settings</h3>
|
<h3 class="pt-2 pr-2 pb-2">Account settings</h3>
|
||||||
@@ -16,11 +21,11 @@
|
|||||||
<label for="name" class="block text-sm font-medium text-gray-700">Name</label>
|
<label for="name" class="block text-sm font-medium text-gray-700">Name</label>
|
||||||
<input id="name" disabled type="text" name="name" value="<%= user.name %>" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500 disabled:shadow-none">
|
<input id="name" disabled type="text" name="name" value="<%= user.name %>" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500 disabled:shadow-none">
|
||||||
|
|
||||||
<label for="password" class="block text-sm font-medium text-gray-700">Update Password</label>
|
<label for="password" class="block text-sm font-medium text-gray-700">New password</label>
|
||||||
<input id="password" disabled type="text" name="email" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500 disabled:shadow-none">
|
<input id="password" disabled type="password" name="password" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500 disabled:shadow-none">
|
||||||
|
|
||||||
<label for="repassword" class="block text-sm font-medium text-gray-700">Confirm new Password</label>
|
<label for="repassword" class="block text-sm font-medium text-gray-700">Confirm new password</label>
|
||||||
<input id="repassword" disabled type="text" name="email" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500 disabled:shadow-none">
|
<input id="repassword" disabled type="password" name="repassword" class="mt-1 block w-full px-3 py-2 border border-gray-300 rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm disabled:border-gray-200 disabled:bg-gray-50 disabled:text-gray-500 disabled:shadow-none">
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<button id="save-account" disabled type="submit" class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 cursor-not-allowed">Save</button>
|
<button id="save-account" disabled type="submit" class="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500 cursor-not-allowed">Save</button>
|
||||||
|
|||||||
Reference in new issue
Block a user