-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
45 lines (38 loc) · 1.06 KB
/
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
41
42
43
44
45
const express =require('express');
const app = express()
const bcrypt = require('bcrypt');
const { urlencoded } = require('express');
app.use(express.json())
const users =[]
app.get('/users', (req, res) =>{
res.json(users)
})
app.post('/users',async(req,res)=>{
try{
const salt = await bcrypt.genSalt()
const hashedPassword = await bcrypt.hash(req.body.password, 10)
console.log(salt);
console.log(hashedPassword);
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)