-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (33 loc) · 1.08 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
var jwt = require('jsonwebtoken');
var express = require('express');
var expressJwt = require('express-jwt');
var bodyParser = require('body-parser');
var user = require('./queries/user');
var secret = 'AZ6Chvpihim6D25/7nMXcZsFy8HykfuEPMsslU2b8mw=';
var app = express();
app.use(bodyParser.json());
app.use(function(err, req, res, next){
if (err.constructor.name === 'UnauthorizedError') {
res.status(401).send('Unauthorized');
}
});
app.use('/api', expressJwt({secret: secret}));
app.use('/api/v1', require('./api')());
app.post('/authenticate', function (req, res) {
user.login(req.body.username, req.body.password, function(err, data) {
if (err) {
if (err.name === 'InvalidCredentialsError') {
//if is invalid, return 401
res.status(401).send(err.message);
}
return;
}
var token = jwt.sign(data, secret, { expiresIn: "4h" });
res.json({ token: token });
});
});
app.use(express.static('www'));
var server = app.listen(3000, function() {
console.log('Speedy Quiz listening on port 3000!');
});
require('./socket')(server, jwt, secret);