logout functionality

v1.18.x
Julian Lam 12 years ago
parent 8d4fb77565
commit f774974803

@ -108,6 +108,31 @@ var socket,
}; };
jQuery('document').ready(function() { jQuery('document').ready(function() {
// On menu click, change "active" state
var menuEl = document.querySelector('.nav'),
liEls = menuEl.querySelectorAll('li'),
logoutEl = document.getElementById('logout'),
parentEl;
menuEl.addEventListener('click', function(e) {
parentEl = e.target.parentNode;
if (parentEl.nodeName === 'LI') {
for(var x=0,numLis=liEls.length;x<numLis;x++) {
if (liEls[x] !== parentEl) liEls[x].className = '';
else parentEl.className = 'active';
}
}
}, false);
// Posting
jQuery('#post_window').slideToggle(0); jQuery('#post_window').slideToggle(0);
// Logout
logoutEl.addEventListener('click', function() {
socket.emit('api:user.logout');
});
socket.on('api:user.logout', function(data) {
if (data.status === 'ok') alert('Logged out.');
});
}) })
}()); }());

@ -25,24 +25,5 @@
}()); }());
</script> </script>
<!-- END Forum Info --> <!-- END Forum Info -->
<script>
(function() {
// On menu click, change "active" state
var menuEl = document.querySelector('.nav'),
liEls = menuEl.querySelectorAll('li'),
parentEl;
menuEl.addEventListener('click', function(e) {
parentEl = e.target.parentNode;
if (parentEl.nodeName === 'LI') {
for(var x=0,numLis=liEls.length;x<numLis;x++) {
if (liEls[x] !== parentEl) liEls[x].className = '';
else parentEl.className = 'active';
}
}
}, false);
})();
</script>
</body> </body>
</html> </html>

@ -11,140 +11,144 @@
<script type="text/javascript" src="/src/templates.js"></script> <script type="text/javascript" src="/src/templates.js"></script>
<script type="text/javascript" src="/src/ajaxify.js"></script> <script type="text/javascript" src="/src/ajaxify.js"></script>
<style type="text/css"> <style type="text/css">
body { body {
padding-top: 60px; padding-top: 60px;
} }
#notification_window { #notification_window {
position: absolute; position: absolute;
right: 20px; right: 20px;
top: 80px; top: 80px;
width: 300px; width: 300px;
height: 0px; height: 0px;
} }
.toaster-alert { .toaster-alert {
cursor: pointer; cursor: pointer;
} }
footer.footer { footer.footer {
color: #555; color: #555;
text-align: center; text-align: center;
} }
footer.footer a { footer.footer a {
color: #222; color: #222;
} }
#post_window { #post_window {
width: 100%; width: 100%;
position: absolute; position: absolute;
height: 350px; height: 350px;
left: 0px; left: 0px;
bottom: 0px; bottom: 0px;
background: white; background: white;
} }
#post_window input { #post_window input {
width: 100%; width: 100%;
height: 30px; height: 30px;
padding: 5px; padding: 5px;
} }
#post_window textarea { #post_window textarea {
width: 100%; width: 100%;
background: #222; background: #222;
height: 220px; height: 220px;
resize: none; resize: none;
border-radius: 0; border-radius: 0;
border: 1px solid #111; border: 1px solid #111;
font-size: 16px; font-size: 16px;
color: #bebebe; color: #bebebe;
outline: 0; outline: 0;
} }
#post_window textarea:focus { #post_window textarea:focus {
outline: 0; outline: 0;
border:none !important; border:none !important;
box-shadow:none !important; box-shadow:none !important;
} }
#post_window .post-title-container { #post_window .post-title-container {
opacity: 0.8; opacity: 0.8;
height: 50px; height: 50px;
} }
#post_window .post-content-container { #post_window .post-content-container {
opacity: 0.8; opacity: 0.8;
background: #000; background: #000;
width: 100%; width: 100%;
height: 300px; height: 300px;
} }
.topic-container { .topic-container {
list-style-type: none; list-style-type: none;
padding: 0; padding: 0;
margin: 0; margin: 0;
border: 1px solid #eee; border: 1px solid #eee;
margin-top: 50px; margin-top: 50px;
} }
.topic-container li.topic-row:nth-child(odd) { .topic-container li.topic-row:nth-child(odd) {
background-color:#fdfdfd; background-color:#fdfdfd;
} }
.topic-container li.topic-row:nth-child(even) { .topic-container li.topic-row:nth-child(even) {
background-color:#fff; background-color:#fff;
} }
.topic-container li.topic-row { .topic-container li.topic-row {
cursor: pointer; cursor: pointer;
border-bottom: 1px solid #eee; border-bottom: 1px solid #eee;
padding: 10px; padding: 10px;
} }
.topic-container li.topic-row:hover { .topic-container li.topic-row:hover {
background-color: #eee; background-color: #eee;
} }
</style> </style>
</head> </head>
<body> <body>
<div class="navbar navbar-inverse navbar-fixed-top"> <div class="navbar navbar-inverse navbar-fixed-top">
<div class="navbar-inner"> <div class="navbar-inner">
<div class="container"> <div class="container">
<div class="nav-collapse collapse"> <div class="nav-collapse collapse">
<ul class="nav"> <a class="brand" href="#">NodeBB</a>
<li class="active"><a href="/">Home</a></li> <ul class="nav">
<li><a href="/register">Register</a></li> <li class="active"><a href="/">Home</a></li>
<li><a href="/login">Login</a></li> <li><a href="/register">Register</a></li>
</ul> <li><a href="/login">Login</a></li>
</div> </ul>
</div> <ul class="nav pull-right">
</div> <li><a href="/logout" id="logout">Log out</a></li>
</div> </ul>
<div id="post_window"> </div>
<div class="post-title-container"> </div>
<div class="container"> </div>
<input id="post_title" placeholder="Enter your topic title here." /> </div>
</div> <div id="post_window">
</div> <div class="post-title-container">
<div class="post-content-container"> <div class="container">
<div class="container"> <input id="post_title" placeholder="Enter your topic title here." />
</div>
</div>
<div class="post-content-container">
<div class="container">
<div class="btn-toolbar"> <div class="btn-toolbar">
<div class="btn-group"> <div class="btn-group">
<a class="btn btn-link" href="#" tabindex="-1"><i class="icon-bold"></i></a> <a class="btn btn-link" href="#" tabindex="-1"><i class="icon-bold"></i></a>
<a class="btn btn-link" href="#" tabindex="-1"><i class="icon-italic"></i></a> <a class="btn btn-link" href="#" tabindex="-1"><i class="icon-italic"></i></a>
<a class="btn btn-link" href="#" tabindex="-1"><i class="icon-font"></i></a> <a class="btn btn-link" href="#" tabindex="-1"><i class="icon-font"></i></a>
<a class="btn btn-link" href="#" tabindex="-1"><i class="icon-list"></i></a> <a class="btn btn-link" href="#" tabindex="-1"><i class="icon-list"></i></a>
</div>
<div class="btn-group" style="float: right; margin-right: -12px">
<a class="btn" onclick="app.post_topic()"><i class="icon-ok"></i> Submit</a>
<a class="btn" onclick="jQuery(post_window).slideToggle(250);"><i class="icon-remove"></i> Discard</a>
</div> </div>
</div> <div class="btn-group" style="float: right; margin-right: -12px">
<a class="btn" onclick="app.post_topic()"><i class="icon-ok"></i> Submit</a>
<a class="btn" onclick="jQuery(post_window).slideToggle(250);"><i class="icon-remove"></i> Discard</a>
</div>
</div>
<textarea id="post_content" placeholder="Type your message here."></textarea> <textarea id="post_content" placeholder="Type your message here."></textarea>
</div> </div>
</div> </div>
</div> </div>
<div id="notification_window"></div> <div id="notification_window"></div>
<div class="container" id="content"> <div class="container" id="content">

@ -0,0 +1,7 @@
<div class="alert" id="message">
<strong>Logout</strong>
<p>You have successfully logged out of NodeBB</p>
<p>
<a href="/">NodeBB Home</a>
</p>
</div>

@ -27,6 +27,7 @@ var fs = require('fs');
loadTemplates([ loadTemplates([
'header', 'footer', 'register', 'home', 'header', 'footer', 'register', 'home',
'login', 'reset', 'reset_code', 'account_settings', 'login', 'reset', 'reset_code', 'account_settings',
'logout',
'emails/reset', 'emails/reset_plaintext' 'emails/reset', 'emails/reset_plaintext'
]); ]);
} }

@ -21,20 +21,33 @@ var config = require('../config.js'),
return global.socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'}); return global.socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'});
} else { } else {
// Start, replace, or extend a session // Start, replace, or extend a session
RDB.get('session:' + user.sessionID, function(session) { RDB.get('sess:' + user.sessionID, function(session) {
if (session !== user.sessionID) { if (session !== user.sessionID) {
RDB.set('session:' + user.sessionID, uid, 60*60*24*14); // Login valid for two weeks RDB.set('sess:' + user.sessionID + ':uid', uid, 60*60*24*14); // Login valid for two weeks
RDB.set('uid:' + uid + ':session', user.sessionID, 60*60*24*14);
} else { } else {
RDB.expire('session:' + user.sessionID, 60*60*24*14); // Defer expiration to two weeks from now RDB.expire('sess:' + user.sessionID + ':uid', 60*60*24*14); // Defer expiration to two weeks from now
RDB.expire('uid:' + uid + ':session', 60*60*24*14);
} }
}); });
global.uid = uid;
return global.socket.emit('user.login', {'status': 1, 'message': 'Logged in!'}); return global.socket.emit('user.login', {'status': 1, 'message': 'Logged in!'});
} }
}); });
}); });
}; };
User.logout = function(callback) {
RDB.get('uid:' + global.uid + ':session', function(sessionID) {
if (sessionID) {
RDB.del('sess:' + sessionID + ':uid');
RDB.del('uid:' + global.uid + ':session');
global.uid = null;
callback(true);
} else callback(false);
});
}
User.create = function(username, password, email) { User.create = function(username, password, email) {
if (username == null || password == null) { if (username == null || password == null) {
@ -104,7 +117,7 @@ var config = require('../config.js'),
}; };
User.get_uid_by_session = function(session, callback) { User.get_uid_by_session = function(session, callback) {
RDB.get('session:' + session, callback); RDB.get('sess:' + session + ':uid', callback);
}; };
User.reset = { User.reset = {

@ -15,30 +15,13 @@ var express = require('express'),
} }
} }
function hasAuth(req, res, next) {
// Include this middleware if the endpoint is publically accessible, but has elements that logged in users can see
global.modules.user.get_uid_by_session(req.sessionID, function(uid) {
if (uid) {
global.uid = uid;
console.log('info: [Auth] User is logged in as uid: ' + uid);
} else {
console.log('info: [Auth] User is not logged in');
}
next();
});
}
function requireAuth(req, res, next) { function requireAuth(req, res, next) {
// Include this middleware if the endpoint requires a logged in user to view // Include this middleware if the endpoint requires a logged in user to view
hasAuth(req, res, function() { if (!global.uid) {
if (!global.uid) { res.redirect('/403');
res.redirect('/403'); } else {
} else { next();
console.log('info: [Auth] User is logged in as uid: ' + uid); }
next();
}
});
} }
// Middlewares // Middlewares
@ -46,16 +29,32 @@ var express = require('express'),
app.use(express.bodyParser()); // Puts POST vars in request.body app.use(express.bodyParser()); // Puts POST vars in request.body
app.use(express.cookieParser()); // If you want to parse cookies (res.cookies) app.use(express.cookieParser()); // If you want to parse cookies (res.cookies)
app.use(express.session({ app.use(express.session({
store: new RedisStore(), store: new RedisStore({
ttl: 60*60*24*14
}),
secret: 'nodebb', secret: 'nodebb',
key: 'express.sid' key: 'express.sid'
})); }));
app.use(function(req, res, next) {
if (global.uid === undefined) {
console.log('info: [Auth] First load, retrieving uid...');
global.modules.user.get_uid_by_session(req.sessionID, function(uid) {
global.uid = uid;
if (global.uid !== null) console.log('info: [Auth] uid ' + global.uid + ' found. Welcome back.');
else console.log('info: [Auth] No login session found.');
});
} else {
console.log('info: [Auth] Ping from uid ' + global.uid);
}
next();
});
// Dunno wtf this does // Dunno wtf this does
// app.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' })); // app.use(express.logger({ format: '\x1b[1m:method\x1b[0m \x1b[33m:url\x1b[0m :response-time ms' }));
// Useful if you want to use app.put and app.delete (instead of app.post all the time) // Useful if you want to use app.put and app.delete (instead of app.post all the time)
// app.use(express.methodOverride()); // app.use(express.methodOverride());
app.get('/', hasAuth, function(req, res) { app.get('/', function(req, res) {
global.modules.topics.generate_forum_body(function(forum_body) { global.modules.topics.generate_forum_body(function(forum_body) {
res.send(templates['header'] + forum_body + templates['footer']); res.send(templates['header'] + forum_body + templates['footer']);
}) })
@ -63,10 +62,19 @@ var express = require('express'),
//res.send(templates['header'] + templates['home'] + templates['footer']); //res.send(templates['header'] + templates['home'] + templates['footer']);
}); });
app.get('/login', hasAuth, function(req, res) { app.get('/login', function(req, res) {
res.send(templates['header'] + templates['login'] + templates['footer']); res.send(templates['header'] + templates['login'] + templates['footer']);
}); });
app.get('/logout', function(req, res) {
console.log('info: [Auth] Session ' + res.sessionID + ' logout (uid: ' + global.uid + ')');
global.modules.user.logout(function(logout) {
if (logout === true) req.session.destroy();
});
res.send(templates['header'] + templates['logout'] + templates['footer']);
});
app.get('/reset/:code', function(req, res) { app.get('/reset/:code', function(req, res) {
res.send(templates['header'] + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']); res.send(templates['header'] + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']);
}); });

Loading…
Cancel
Save