Password changing functionality.

Signed-off-by: Armored Dragon <publicmail@armoreddragon.com>
account-personalization
Armored Dragon 2024-05-27 22:10:49 -05:00
parent 0dd6a7fa00
commit 5b0f0121ac
Signed by: ArmoredDragon
GPG Key ID: C7207ACC3382AD8B
5 changed files with 623 additions and 490 deletions

View File

@ -5,6 +5,7 @@ const { S3Client, PutObjectCommand, GetObjectCommand, DeleteObjectCommand, ListO
const { getSignedUrl } = require("@aws-sdk/s3-request-presigner");
let s3;
const crypto = require("crypto");
const bcrypt = require("bcrypt");
const validate = require("../form_validation");
const permissions = require("../permissions");
const md = require("markdown-it")()
@ -98,7 +99,6 @@ async function getUser({ user_id, username, include_password = false }) {
return { success: true, data: user };
}
// TODO: Rename patchUser
async function editUser({ requester_id, user_id, user_content }) {
let user = await getUser({ user_id: user_id });
if (!user.success) return _r(false, "User not found");
@ -116,6 +116,11 @@ async function editUser({ requester_id, user_id, user_content }) {
let formatted = {};
formatted[user_content.setting_name] = user_content.value;
if (formatted.password) {
// TODO: Validate password
formatted.password = await bcrypt.hash(formatted.password, 10);
}
await prisma.user.update({ where: { id: user.id }, data: formatted });
return _r(true);
}

View File

@ -121,3 +121,41 @@ input:checked + .slider:before {
.slider.round:before {
border-radius: 50%;
}
dialog {
border-radius: 5px;
border: 0;
min-width: 300px;
}
dialog .title {
font-size: 1.1rem;
text-align: center;
margin-bottom: 1rem;
}
dialog .entry {
width: 100%;
margin-bottom: 0.5rem;
}
dialog .entry input {
width: 100%;
margin: 0;
box-sizing: border-box;
font-size: 1.1rem;
}
dialog .status {
margin-bottom: 1rem;
color: red;
text-align: center;
}
dialog .horizontal-button-container {
display: flex;
flex-direction: row;
}
dialog .horizontal-button-container * {
flex-grow: 1;
margin: 0 0.1rem;
}
dialog::backdrop {
background-color: rgba(0, 0, 0, 0.5);
}

View File

@ -125,3 +125,44 @@ input:checked + .slider:before {
.slider.round:before {
border-radius: 50%;
}
dialog {
border-radius: 5px;
border: 0;
min-width: 300px;
.title {
font-size: 1.1rem;
text-align: center;
margin-bottom: 1rem;
}
.entry {
width: 100%;
margin-bottom: 0.5rem;
input {
width: 100%;
margin: 0;
box-sizing: border-box;
font-size: 1.1rem;
}
}
.status {
margin-bottom: 1rem;
color: red;
text-align: center;
}
.horizontal-button-container {
display: flex;
flex-direction: row;
* {
flex-grow: 1;
margin: 0 0.1rem;
}
}
}
dialog::backdrop {
background-color: rgba(0, 0, 0, 0.5);
}

View File

@ -8,6 +8,30 @@
<title>Yet-Another-Blog</title>
</head>
<body>
<dialog id="change-password-dialog">
<div class="title">
<span>Change Password</span>
</div>
<div class="entry">
<div>Current password</div>
<input id="cp-current" type="password" oninput="changePasswordInputUpdate()" />
</div>
<div class="entry">
<div>New password</div>
<input id="cp-new-1" type="password" oninput="changePasswordInputUpdate()" />
</div>
<div class="entry">
<div>Confirm password</div>
<input id="cp-new-2" type="password" oninput="changePasswordInputUpdate()" />
</div>
<div class="status">Please enter your current password.</div>
<div class="horizontal-button-container">
<button class="button good" onclick="sendPasswordUpdate()">Accept</button>
<button id="cp-cancel" class="button bad">Cancel</button>
</div>
</dialog>
<%- include("partials/header.ejs") %>
<div class="page">
<div class="page-center">
@ -21,7 +45,7 @@
</div>
<div class="setting">
<div class="title">Change Password</div>
<button class="button bad"><span>Open Dialog</span></button>
<button id="change-password-button" class="button bad"><span>Open Dialog</span></button>
</div>
<div class="setting">
<div class="title">Change Profile Picture</div>

View File

@ -8,5 +8,30 @@ async function changeValue(setting_name, element) {
// TODO: On failure, notify the user
if (response.body.success) {
alert("Successfully changed password");
}
}
const change_password_dialog = qs("#change-password-dialog");
qs("#change-password-button").addEventListener("click", () => change_password_dialog.showModal());
qs("#cp-cancel").addEventListener("click", () => change_password_dialog.close());
function changePasswordInputUpdate() {
const status = qs("#change-password-dialog .status");
const current_password = qs("#cp-current");
const new_password_1 = qs("#cp-new-1");
const new_password_2 = qs("#cp-new-2");
if (current_password.value === "") return (status.innerText = "Please enter your current password.");
if (new_password_1.value !== new_password_2.value) return (status.innerText = "New password does not match.");
return (status.innerHTML = "&nbsp;");
}
function sendPasswordUpdate() {
const new_password_1 = qs("#cp-new-1");
// Check fields match
// Send post update
changeValue("password", new_password_1);
}