2016-12-10 07:47:58 +07:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
'auth': serveAuth,
|
|
|
|
'login': serveLogin,
|
|
|
|
'logout': serveLogout
|
|
|
|
}
|
|
|
|
|
|
|
|
var authentication = require('./authentication');
|
|
|
|
var replies = require('./replies');
|
|
|
|
|
|
|
|
function serveAuth(req, res) {
|
2016-12-17 08:06:40 +07:00
|
|
|
if(req.method == 'POST') {
|
|
|
|
serveAuthPost(req, res);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
serveAuthGet(req, res);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function serveAuthGet(req, res) {
|
2016-12-10 07:47:58 +07:00
|
|
|
authentication.verify(req, res)
|
|
|
|
.then(function(user) {
|
|
|
|
replies.already_authenticated(res, user);
|
|
|
|
})
|
|
|
|
.fail(function(err) {
|
|
|
|
replies.authentication_failed(res);
|
|
|
|
console.error(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-17 08:06:40 +07:00
|
|
|
function serveAuthPost(req, res) {
|
|
|
|
authentication.authenticate(req, res);
|
|
|
|
}
|
|
|
|
|
2016-12-10 07:47:58 +07:00
|
|
|
function serveLogin(req, res) {
|
2016-12-17 08:06:40 +07:00
|
|
|
res.render('login');
|
2016-12-10 07:47:58 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
function serveLogout(req, res) {
|
2016-12-18 01:36:41 +07:00
|
|
|
var redirect_param = req.query.redirect;
|
|
|
|
var redirect_url = redirect_param || '/';
|
2016-12-10 07:47:58 +07:00
|
|
|
res.clearCookie('access_token');
|
2016-12-18 01:36:41 +07:00
|
|
|
res.redirect(redirect_url);
|
2016-12-10 07:47:58 +07:00
|
|
|
}
|
2016-12-18 01:36:41 +07:00
|
|
|
|