1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-30 22:41:47 -05:00

Remove auth checking from routes.js

This commit is contained in:
Jack Foltz 2018-07-28 17:13:50 -04:00
parent d6aa85ae80
commit 28555cff3f
Signed by: foltik
GPG Key ID: 303F88F996E95541

View File

@ -1,66 +1,26 @@
var index = require('./index.js'); const index = require('./index.js');
var home = require('./home.js'); const home = require('./home.js');
var upload = require('./upload.js'); const upload = require('./upload.js');
var view = require('./view.js'); const view = require('./view.js');
var auth = require('./auth.js'); const auth = require('./auth.js');
var register = require('./register.js'); const register = require('./register.js');
var login = require('./login.js'); const login = require('./login.js');
var panel = require('./panel.js'); const panel = require('./panel.js');
var keys = require('./keys.js'); const keys = require('./keys.js');
var invites = require('./invites.js'); const invites = require('./invite.js');
var users = require('./users.js'); const users = require('./users.js');
var Key = require('../models/Key.js');
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) {
if (!req.session || !req.session.passport)
return res.redirect('/login');
else
return next();
};
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) { module.exports = function (app) {
app.use('/', index); app.use('/', index);
app.use('/home', requireLogin, home); app.use('/home', home);
app.use('/v', view); app.use('/v', view);
app.use('/api/upload', upload); app.use('/api/upload', upload);
app.use('/api/auth', auth); app.use('/api/auth', auth);
app.use('/api/keys', requireLogin, keys); app.use('/api/keys', keys);
app.use('/api/invites', requireLogin, invites); app.use('/api/invites', invites);
app.use('/api/users', requireLogin, users); app.use('/api/users', users);
app.use('/register', register); app.use('/register', register);
app.use('/login', login); app.use('/login', login);
app.use('/panel', requireLogin, panel); app.use('/panel', panel);
app.use('/panel*', requireLogin, panel); app.use('/panel*', panel);
app.use(function (err, req, res) {
if (err.name === 'UnauthorizedError') {
res.status(401);
res.json({"message": err.name + ": " + err.message});
}
})
}; };