Added password changing

This commit is contained in:
2021-02-27 04:38:59 +11:00
parent 2d7f6894ab
commit 648f8a16cc
4 changed files with 35 additions and 8 deletions
+1 -1
View File
@@ -43,7 +43,7 @@ There are external components to this template referred to as "microservices". T
- ~~login (with cookies)~~ - ~~login (with cookies)~~
- ~~logout (with cookies)~~ - ~~logout (with cookies)~~
- ~~account deletion~~ - ~~account deletion~~
- Change passwords - ~~Change passwords~~
- Administration Panel - Administration Panel
- ~~Default admin account~~ - ~~Default admin account~~
- ~~Exclusive to admin accounts~~ - ~~Exclusive to admin accounts~~
+24 -3
View File
@@ -13,7 +13,7 @@ const Account = props => {
} }
//refs //refs
let contactElement; let contactElement, passwordElement, retypeElement;
//once before render //once before render
useEffect(() => { useEffect(() => {
@@ -31,13 +31,26 @@ const Account = props => {
<h1 className='centered'>Account</h1> <h1 className='centered'>Account</h1>
<form className='constricted' onSubmit={async evt => { <form className='constricted' onSubmit={async evt => {
evt.preventDefault(); evt.preventDefault();
await update(contactElement.checked); await update(contactElement.checked, passwordElement.value, retypeElement.value);
passwordElement.value = retypeElement.value = '';
}}> }}>
<div>
<div> <div>
<label htmlFor='contact'>Allow Promotional Emails:</label> <label htmlFor='contact'>Allow Promotional Emails:</label>
<input type='checkbox' name='contact' ref={e => contactElement = e} /> <input type='checkbox' name='contact' ref={e => contactElement = e} />
</div> </div>
<div>
<label htmlFor='password'>Change Password:</label>
<input type='password' name='password' ref={e => passwordElement = e} />
</div>
<div>
<label htmlFor='retype'>Retype Password:</label>
<input type='password' name='retype' ref={e => retypeElement = e} />
</div>
</div>
<button type='submit'>Update Information</button> <button type='submit'>Update Information</button>
</form> </form>
@@ -46,12 +59,20 @@ const Account = props => {
); );
}; };
const update = async (contact) => { const update = async (contact, password, retype) => {
if (password != retype) {
alert('Passwords do not match');
}
//generate a new formdata payload //generate a new formdata payload
let formData = new FormData(); let formData = new FormData();
formData.append('contact', contact); formData.append('contact', contact);
if (password) {
formData.append('password', password);
}
const result = await fetch('/api/accounts', { method: 'PATCH', body: formData }); const result = await fetch('/api/accounts', { method: 'PATCH', body: formData });
if (result.ok) { if (result.ok) {
+7 -1
View File
@@ -1,3 +1,4 @@
const bcrypt = require('bcryptjs');
const { accounts } = require('../database/models'); const { accounts } = require('../database/models');
const route = async (req, res) => { const route = async (req, res) => {
@@ -5,9 +6,14 @@ const route = async (req, res) => {
return res.status(500).send('missing account data'); return res.status(500).send('missing account data');
} }
//generate the password hash
const salt = await bcrypt.genSalt(11);
const hash = await bcrypt.hash(req.fields.password, salt);
//update the account //update the account
await accounts.update({ await accounts.update({
contact: req.fields.contact contact: req.fields.contact,
hash: hash
}, { }, {
where: { where: {
id: req.session.account.id id: req.session.account.id