This repository was archived by the owner on Jan 5, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathexpress-handlers.js
61 lines (54 loc) · 1.51 KB
/
express-handlers.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
57
58
59
60
61
const bcrypt = require('bcrypt');
const users = [
{
name: 'hugo',
// generated from 'boss' with bcrypt
// using work factor 10
password: '$2a$10$IYTsvP51gvUfM2SvZ47acekm05qdyxQbVW5Yy2q3dPp1EipWx7clm'
},
{
name: 'guest',
// generated from 'guest-boss' with bcrypt
// using work factor 10
password: '$2a$10$6rfA.JiURAnuGhVAKpaoneXhsOuKBBRfKDRUgfLxMnVvQUWK5u6h2'
}
];
function getUser(username) {
return users.find(({ name }) => name === username);
}
async function login(req, res) {
try {
const { username, password } = req.body;
if (!username || !password) {
return res.status(400).json({ message: 'username and password are required' });
}
const user = getUser(username);
if (!user) {
return res.status(401).json({ message: 'No user with matching username' });
}
if (!(await bcrypt.compare(password, user.password))) {
return res.status(401).json({ message: 'Wrong password' });
}
req.session.data = { username };
return res.status(201).json();
} catch (e) {
console.error(`Error during login of "${req.body.username}": ${e.stack}`);
res.status(500).json({ message: e.message });
}
}
async function logout(req, res) {
req.session.data = null;
return res.status(200).json();
}
async function checkAuth(req, res) {
if (!req.session.data) {
return res.status(401).json();
}
const { username } = req.session.data;
return res.status(200).json({ username });
}
module.exports = {
login,
logout,
checkAuth,
};