ernest
Version:
Web framework for HTTP and HTTPS, using ExpressJS, Session, Mongo, Socket IO, Redis
69 lines (59 loc) • 1.28 kB
JavaScript
;
const str_user = 'user';
const str_pass = 'password';
const str_set = '$set';
const str_unset = '$unset';
const str_all = 'all_users';
class Ernest_Session
{
constructor(app,users)
{
this.app = app;
this.users = users;
};
InitSessionUrl(url)
{
var _this = this;
_this.app.post(url,function(req,res,next)
{
if(isNotAuth(req))
{
_this.users.isUser(req['body'][str_user],req['body'][str_pass],function(is,user)
{
is ? (() =>
{
req.session.user = req['body'][str_user];
req.session.data = user;
req.session.vars = {};
res.json({result: true});
})():res.json({result: false});
});
}
else
{
next();
};
});
_this.app.post('/user',function(req,res,next)
{
isNotAuth(req) ? res.json({result:null}): res.json({result:req.session.user});
});
_this.app.post('/logout',function(req,res,next)
{
delete req.session.user; res.json({result:true});
});
};
OnLoginRedirect(login_html,board_html)
{
var _this = this;
_this.app.get(login_html,function(req,res,next)
{
isNotAuth(req) ? next() : res.redirect(board_html);
});
};
};
module.exports = Ernest_Session;
function isNotAuth(req)
{
return (typeof req.session.user === "undefined");
};