2017-10-09 22:01:02 -04:00
|
|
|
var index = require('./routes/index.js');
|
2017-10-14 15:16:58 -04:00
|
|
|
var home = require('./routes/home.js');
|
2017-10-09 22:01:02 -04:00
|
|
|
var upload = require('./routes/upload.js');
|
|
|
|
var view = require('./routes/view.js');
|
2017-10-11 10:15:19 -04:00
|
|
|
var auth = require('./routes/auth.js');
|
|
|
|
var register = require('./routes/register.js');
|
2017-10-11 13:11:57 -04:00
|
|
|
var login = require('./routes/login.js');
|
2017-10-11 20:26:07 -04:00
|
|
|
var panel = require('./routes/panel');
|
2017-10-11 10:15:19 -04:00
|
|
|
|
|
|
|
var fs = require('fs');
|
|
|
|
var path = require('path');
|
|
|
|
var jwt = require('express-jwt');
|
|
|
|
var jwtauth = jwt({
|
|
|
|
secret: fs.readFileSync(path.join(__dirname, '../jwt.pem'), 'utf8'),
|
2017-10-14 17:49:11 -04:00
|
|
|
userProperty: 'payload',
|
|
|
|
getToken: function(req) {
|
|
|
|
if (req.headers.authorization && req.headers.authorization.split(' ')[0] === 'Bearer') {
|
|
|
|
// First check Authorization header
|
|
|
|
return req.headers.authorization.split(' ')[1];
|
|
|
|
} else if (req.cookies && req.cookies['shimapan-token']) {
|
|
|
|
// Get from cookies as fallback
|
|
|
|
return req.cookies['shimapan-token'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// no token received
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-11 10:15:19 -04:00
|
|
|
});
|
2017-10-09 22:01:02 -04:00
|
|
|
|
|
|
|
module.exports = function(app) {
|
|
|
|
app.use('/', index);
|
2017-10-14 17:55:57 -04:00
|
|
|
app.use('/home', jwtauth, home);
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use('/v', view);
|
2017-10-11 20:26:07 -04:00
|
|
|
app.use('/api/upload', jwtauth, upload);
|
2017-10-11 10:15:19 -04:00
|
|
|
app.use('/api/auth', auth);
|
|
|
|
app.use('/register', register);
|
2017-10-11 13:11:57 -04:00
|
|
|
app.use('/login', login);
|
2017-10-14 17:49:11 -04:00
|
|
|
app.use('/panel', jwtauth, panel);
|
|
|
|
app.use('/panel*', jwtauth, panel);
|
2017-10-11 10:15:19 -04:00
|
|
|
|
|
|
|
app.use(function(err, req, res, next) {
|
|
|
|
if (err.name === 'UnauthorizedError') {
|
|
|
|
res.status(401);
|
2017-10-14 17:55:57 -04:00
|
|
|
res.redirect('/login');
|
2017-10-11 10:15:19 -04:00
|
|
|
res.json({"message": err.name + ": " + err.message});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|