-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
47 lines (38 loc) · 1.24 KB
/
index.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
const express = require("express");
const app = express();
const models = require('./models');
const bodyParser = require("body-parser");
var pbkdf2 = require('pbkdf2');
var crypto = require('crypto');
var salt = crypto.randomBytes(20).toString('hex');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function (request, response, next) {
console.log(request.method, request.path);
next();
});
function encryptionPassword(req, response, next){
var key = pbkdf2.pbkdf2Sync(
req.body.password, salt, 36000, 256, 'sha256'
);
var hash = key.toString('hex');
req.body.password = `pbkdf2_sha256$36000$${salt}$${hash}`;
next();
}
app.post("/sign-up", encryptionPassword, function (req, response) {
models.user.create({ username: req.body.username, password: req.body.password})
.then(function (user) {
response.send(user);
});
});
app.post("/login", encryptionPassword, function (req, response) {
models.user.findOne({where: {username: req.body.username, password: req.body.password}})
.then(function (user) {
console.log("There was an user");
console.log(user);
response.send(user);
});
})
app.listen(3000, function () {
console.log('server listening on port 3000');
})