-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
134 lines (114 loc) · 3.46 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*jslint node: true es5: true nomen: true*/
"use strict";
var express = require('express'),
bodyParser = require('body-parser'),
jwt = require('jwt-simple'),
cors = require('cors'),
moment = require('moment'),
jwauth = require('./jwtauth.js'),
passport = require('passport'),
app = express();
app.use(bodyParser());
app.set('jwtTokenSecret', 'LOL_not_real');
app.use(cors());
var requireAuth = function(req, res, next) {
if (!req.user) {
res.end('Not authorized', 401)
} else {
next();
}
}
var port = process.env.PORT || 1337,
mongoose = require('mongoose'),
User = require('./app/models/user');
mongoose.connect('mongodb://localhost/testAPI');
var router = express.Router();
router.use(function (req, res, next) {
// do logging
next();
});
router.get('/', jwauth, requireAuth, function (req, res) {
res.json({ message: 'Welcome, ' + req.user.username});
});
router.route('/auth')
.post(function (req, res) {
console.log(req.headers.username);
//auth here
User.findOne({ username: req.headers.username }, function (err, user) {
if (err) {
// user not found
return res.send(401);
}
if (!user) {
// incorrect username
return res.send(401);
}
// User has authenticated OK
var expires = moment().add('days', 1).valueOf();
var token = jwt.encode({
iss: user.id,
exp: expires
}, app.get('jwtTokenSecret'));
res.json({
token : token,
expires: expires,
user: user.toJSON()
});
res.send(200);
});
});
router.route('/users')
.post(function (req, res) {
var user = new User();
user.username = req.body.username; // extract the user's 'name' from the request
//password_hash: String,
//password_salt: String,
user.type = req.body.type; //TODO: Set the default type to user - must be promoted to become anything else
user.email = req.body.email;
user.firstname = req.body.firstname;
user.lastname = req.body.lastname;
user.save(function (err) {
if (err) { res.send(err); }
res.json({ message: 'User ' + user.name + ' created!' });
});
})
// get all the users (accessed at GET http://localhost:8080/api/users)
.get(jwauth, requireAuth, function (req, res) {
User.find(function (err, users) {
if (err) { res.send(err); }
res.json(users);
});
});
// on routes that end in /users/:user_id
router.route('/users/:user_id')
.get(jwauth, requireAuth, function (req, res) {
User.findById(req.params.user_id, function (err, user) {
if (err) { res.send(err); }
res.json(user);
});
})
// update the user with this id
.put(jwauth, requireAuth, function (req, res) {
User.findById(req.params.user_id, function (err, user) {
if (err) { res.send(err); }
user.name = req.body.name;
user.save(function (err) {
if (err) { res.send(err); }
res.json({ message: 'User updated!' });
});
});
})
// delete the user with this id
.delete(jwauth, requireAuth, function (req, res) {
User.remove({
_id: req.params.user_id
}, function (err, user) {
if (err) { res.send(err); }
res.json({ message: 'User successfully deleted' });
});
});
// register routs
app.use('/api', router);
// vroom
app.listen(port);
console.log('Magic happens on port ' + port);