-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
87 lines (69 loc) · 2.04 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
var express = require('express');
var bodyParser = require('body-parser');
var User = require('./models/user')
var app = express();
var jwt = require('jwt-simple')
var _ = require('lodash')
var bcrypt = require('bcrypt')
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({
extended: true
})); // support encoded bodies
var secretkey = "supersecretkey"
app.post('/session', function(req, res, next) {
console.log(req.body.username);
User.findOne({
username: req.body.username
})
.select('password')
.exec(function(err, user) {
//it gets a user instance which does not have a username property . Why ?
if (err) {
return next(err)
}
if (!user) {
return res.send(401)
}
bcrypt.compare(req.body.password, user.password, function(err, valid) {
if (err) {
return next(err)
}
if (!valid) {
return res.send(401)
}
//var test = {username:user.username};
//console.log(user.username);
var token = jwt.encode({
username: req.body.username
}, secretkey)
res.json(token)
})
})
})
app.get('/user', function(req, res, next) {
var token = req.headers['x-auth']
console.log(token)
var auth = jwt.decode(token, secretkey)
//console.log(auth)
User.findOne({
username: auth.username
}, function(err, user) {
res.json(user)
})
})
app.post('/user', function(req, res) {
var user = new User({
username: req.body.username
})
bcrypt.hash(req.body.password, 10, function(err, hash) {
user.password = hash
user.save(function(err, user) {
if (err) {
throw next(err)
}
// res.send(201)
res.json(user);
})
})
})
app.listen(3000)