-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathserver-final.js
56 lines (42 loc) · 1.41 KB
/
server-final.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
const express = require('express');
const bcrypt = require('bcrypt');
const app = express();
app.use(express.json());
const users = [];
const hashPassword = async (password, saltRounds = 8) => {
try {
const hashedPassword = await bcrypt.hash(password, saltRounds);
return hashedPassword;
} catch (err) {
return console.error(err.message);
}
};
const isValidPassword = async (password, hash) => {
try {
const isValid = await bcrypt.compare(password, hash);
return isValid;
} catch (err) {
return console.error(err.message);
}
};
app.get('/users', (req, res) => {
res.send(users);
});
app.post('/users', async (req, res) => {
const { body: { username, password } } = req;
const hashedPassword = await hashPassword(password);
if (!hashPassword) return res.status(404).send({msg: 'could not hash'});
users.push({ username, hashedPassword });
res.status(201).send()
});
app.post('/users/login', async (req, res) => {
const { body: { username, password } } = req;
const user = users.find(user => user.username === username);
if (!user) return res.status(403).send({ msg: 'No user with that username found' });
const isValid = await isValidPassword(password, user.hashedPassword);
if (!isValid) return res.status(403).send({ msg: 'Wrong password' });
res.send({ msg: 'Password Accepted!' });
});
app.listen(3000, () => {
console.log(`Server listening on localhost:3000`)
})