-
-
Notifications
You must be signed in to change notification settings - Fork 94
/
server.js
40 lines (34 loc) · 868 Bytes
/
server.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
const express = require('express')
const app = express()
const bcrypt = require('bcrypt')
app.use(express.json())
const users = []
app.get('/users', (req, res) => {
res.json(users)
})
app.post('/users', async (req, res) => {
try {
const hashedPassword = await bcrypt.hash(req.body.password, 10)
const user = { name: req.body.name, password: hashedPassword }
users.push(user)
res.status(201).send()
} catch {
res.status(500).send()
}
})
app.post('/users/login', async (req, res) => {
const user = users.find(user => user.name === req.body.name)
if (user == null) {
return res.status(400).send('Cannot find user')
}
try {
if(await bcrypt.compare(req.body.password, user.password)) {
res.send('Success')
} else {
res.send('Not Allowed')
}
} catch {
res.status(500).send()
}
})
app.listen(3000)