-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
65 lines (49 loc) · 1.8 KB
/
server.js
File metadata and controls
65 lines (49 loc) · 1.8 KB
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
62
63
64
const express = require('express')
const app = express()
const bcrypt = require('bcrypt')
app.use(express.json())
//in a real application you would use a database instead to store users
const users = []
//in a real case you would not want to have a route that exposes password info. But is used for testing
app.get('/users', (req, res) => {
res.json(users)
})
//Create users with a post request
app.post('/users', async (req, res) => {
try {
const salt = await bcrypt.genSalt() //create a salt using bcrypt default is 10
const hashedPassword = await bcrypt.hash(req.body.password, salt) //returns a hashed passworde example: asdafsdfadsfasdf
//but with 'salt' produces a different hashed password every time
console.log("Salt" + salt)
console.log("Hashed Password: " + hashedPassword);
//bcrypt automatically saves salt with the hashed password
const user = {name: req.body.name, password: hashedPassword}
//Push user into the array
users.push(user)
res.status(201).send()
}
catch {
res.status(500).send()
}
//The below code can't be used as it doesn't hash the password leaving it open for anyone who can access the database
// const user = {name: req.body.name, password: req.body.password}
})
// Login
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 {
//bcrypt compare protects against timing attacks
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)