Skip to content

Commit 648f8a1

Browse files
committed
Added password changing
1 parent 2d7f689 commit 648f8a1

File tree

4 files changed

+35
-8
lines changed

4 files changed

+35
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ There are external components to this template referred to as "microservices". T
4343
- ~~login (with cookies)~~
4444
- ~~logout (with cookies)~~
4545
- ~~account deletion~~
46-
- Change passwords
46+
- ~~Change passwords~~
4747
- Administration Panel
4848
- ~~Default admin account~~
4949
- ~~Exclusive to admin accounts~~

client/components/pages/account.jsx

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const Account = props => {
1313
}
1414

1515
//refs
16-
let contactElement;
16+
let contactElement, passwordElement, retypeElement;
1717

1818
//once before render
1919
useEffect(() => {
@@ -31,11 +31,24 @@ const Account = props => {
3131
<h1 className='centered'>Account</h1>
3232
<form className='constricted' onSubmit={async evt => {
3333
evt.preventDefault();
34-
await update(contactElement.checked);
34+
await update(contactElement.checked, passwordElement.value, retypeElement.value);
35+
passwordElement.value = retypeElement.value = '';
3536
}}>
3637
<div>
37-
<label htmlFor='contact'>Allow Promotional Emails:</label>
38-
<input type='checkbox' name='contact' ref={e => contactElement = e} />
38+
<div>
39+
<label htmlFor='contact'>Allow Promotional Emails:</label>
40+
<input type='checkbox' name='contact' ref={e => contactElement = e} />
41+
</div>
42+
43+
<div>
44+
<label htmlFor='password'>Change Password:</label>
45+
<input type='password' name='password' ref={e => passwordElement = e} />
46+
</div>
47+
48+
<div>
49+
<label htmlFor='retype'>Retype Password:</label>
50+
<input type='password' name='retype' ref={e => retypeElement = e} />
51+
</div>
3952
</div>
4053

4154
<button type='submit'>Update Information</button>
@@ -46,12 +59,20 @@ const Account = props => {
4659
);
4760
};
4861

49-
const update = async (contact) => {
62+
const update = async (contact, password, retype) => {
63+
if (password != retype) {
64+
alert('Passwords do not match');
65+
}
66+
5067
//generate a new formdata payload
5168
let formData = new FormData();
5269

5370
formData.append('contact', contact);
5471

72+
if (password) {
73+
formData.append('password', password);
74+
}
75+
5576
const result = await fetch('/api/accounts', { method: 'PATCH', body: formData });
5677

5778
if (result.ok) {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "server/server.js",
66
"scripts": {
77
"configure": "node configure-script.js",
8-
"clean": "rm docker-compose.yml; rm Dockerfile; rm startup.sql",
8+
"clean": "rm docker-compose.yml; rm Dockerfile; rm startup.sql",
99
"start": "npm run build && node server/server.js",
1010
"build": "npm run build:server && npm run build:client",
1111
"build:server": "exit 0",

server/accounts/update.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
1+
const bcrypt = require('bcryptjs');
12
const { accounts } = require('../database/models');
23

34
const route = async (req, res) => {
45
if (!req.session.account.id) {
56
return res.status(500).send('missing account data');
67
}
78

9+
//generate the password hash
10+
const salt = await bcrypt.genSalt(11);
11+
const hash = await bcrypt.hash(req.fields.password, salt);
12+
813
//update the account
914
await accounts.update({
10-
contact: req.fields.contact
15+
contact: req.fields.contact,
16+
hash: hash
1117
}, {
1218
where: {
1319
id: req.session.account.id

0 commit comments

Comments
 (0)