2017-10-20 16:46:14 -04:00
|
|
|
var index = require('./index.js');
|
|
|
|
var home = require('./home.js');
|
|
|
|
var upload = require('./upload.js');
|
|
|
|
var view = require('./view.js');
|
|
|
|
var auth = require('./auth.js');
|
|
|
|
var register = require('./register.js');
|
|
|
|
var login = require('./login.js');
|
|
|
|
var panel = require('./panel.js');
|
|
|
|
var keys = require('./keys.js');
|
2017-10-11 10:15:19 -04:00
|
|
|
|
2017-10-22 12:19:51 -04:00
|
|
|
var Key = require('../models/Key.js');
|
2017-10-14 17:49:11 -04:00
|
|
|
|
2017-10-22 12:19:51 -04:00
|
|
|
var checkApiKey = function (key, cb) {
|
|
|
|
Key.find({key: key}, function (err, res) {
|
|
|
|
if (err) throw err;
|
|
|
|
cb(res.length === 1);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
var requireLogin = function (req, res, next) {
|
2017-10-21 15:10:55 -04:00
|
|
|
if (!req.session || !req.session.passport)
|
|
|
|
return res.redirect('/login');
|
2017-10-18 13:31:08 -04:00
|
|
|
else
|
2017-10-21 15:10:55 -04:00
|
|
|
return next();
|
2017-10-18 13:31:08 -04:00
|
|
|
};
|
2017-10-09 22:01:02 -04:00
|
|
|
|
2017-10-22 12:19:51 -04:00
|
|
|
var requireLoginApi = function(req, res, next) {
|
|
|
|
if (!req.session || !req.session.passport) {
|
|
|
|
if (!req.body.apikey) {
|
|
|
|
return res.redirect('/login');
|
|
|
|
} else {
|
|
|
|
checkApiKey(res.body.apikey, function(valid) {
|
|
|
|
if (!valid)
|
|
|
|
return res.sendStatus(401);
|
|
|
|
else
|
|
|
|
return next();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return next();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = function (app) {
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use('/', index);
|
2017-10-18 13:31:08 -04:00
|
|
|
app.use('/home', requireLogin, home);
|
2017-10-09 22:01:02 -04:00
|
|
|
app.use('/v', view);
|
2017-10-18 13:31:08 -04:00
|
|
|
app.use('/api/upload', upload);
|
2017-10-11 10:15:19 -04:00
|
|
|
app.use('/api/auth', auth);
|
2017-10-18 13:31:08 -04:00
|
|
|
app.use('/api/keys', requireLogin, keys);
|
2017-10-11 10:15:19 -04:00
|
|
|
app.use('/register', register);
|
2017-10-11 13:11:57 -04:00
|
|
|
app.use('/login', login);
|
2017-10-18 13:31:08 -04:00
|
|
|
app.use('/panel', requireLogin, panel);
|
|
|
|
app.use('/panel*', requireLogin, panel);
|
2017-10-11 10:15:19 -04:00
|
|
|
|
2017-10-22 12:19:51 -04:00
|
|
|
app.use(function (err, req, res, next) {
|
2017-10-11 10:15:19 -04:00
|
|
|
if (err.name === 'UnauthorizedError') {
|
|
|
|
res.status(401);
|
|
|
|
res.json({"message": err.name + ": " + err.message});
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|