forked from michaelakbaraly/ka-ching-back
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
55 lines (43 loc) · 1.48 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
48
49
50
51
52
53
54
55
var express = require("express");
var routes = require("./routes/routes.js");
var expressJwt = require('express-jwt');
var jwt = require('jsonwebtoken');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var corsHelper = require("./helpers/cors.js");
var _SECRET = "secret_a_changer"
var app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: false}));
app.use(cookieParser());
app.use("/", routes());
app.use(corsHelper.setCorsHeaders);
// We are going to protect /api routes with JWT
app.use('/api', expressJwt({secret: _SECRET}));
app.post('/authenticate', function (req, res) {
//TODO validate req.body.username and req.body.password
//if is invalid, return 401
if (req.body === undefined || (!(req.body.login === 'john.doe' && req.body.password === 'foobar'))) {
res.send(401, 'Wrong user or password');
return;
}
var profile = {
first_name: 'John',
last_name: 'Doe',
email: 'john@doe.com',
id: 123
};
// We are sending the profile inside the token
var token = jwt.sign(profile, _SECRET, {expiresInMinutes: 60 * 5});
res.json({token: token});
});
app.get('/api/restricted', function (req, res) {
console.log('user ' + req.user.email + ' is calling /api/restricted');
res.json({
name: 'foo'
});
});
var server = app.listen(3000, function () {
var host = server.address().address;
var port = server.address().port;
});