Skip to content

Node.js based forum software built for the modern web

logout functionality

Julian Lamf77497480355
diff --git a/public/src/app.js b/public/src/app.js
index 21d0f4bf3d..7d64a87537 100644
--- a/public/src/app.js
+++ b/public/src/app.js
@@ -108,6 +108,31 @@ var socket,
};
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);
+
+ // Logout
+ logoutEl.addEventListener('click', function() {
+ socket.emit('api:user.logout');
+ });
+ socket.on('api:user.logout', function(data) {
+ if (data.status === 'ok') alert('Logged out.');
+ });
})
}());
diff --git a/public/templates/footer.tpl b/public/templates/footer.tpl
index 0b37f9001e..9cea2835f6 100644
--- a/public/templates/footer.tpl
+++ b/public/templates/footer.tpl
@@ -25,24 +25,5 @@
}());
</script>
<!-- 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>
</html>
\ No newline at end of file
diff --git a/public/templates/header.tpl b/public/templates/header.tpl
index 8d6f9fa6b4..ecac91904d 100644
--- a/public/templates/header.tpl
+++ b/public/templates/header.tpl
@@ -11,140 +11,144 @@
<script type="text/javascript" src="/src/templates.js"></script>
<script type="text/javascript" src="/src/ajaxify.js"></script>
<style type="text/css">
- body {
- padding-top: 60px;
- }
+ body {
+ padding-top: 60px;
+ }
- #notification_window {
- position: absolute;
- right: 20px;
- top: 80px;
- width: 300px;
- height: 0px;
- }
+ #notification_window {
+ position: absolute;
+ right: 20px;
+ top: 80px;
+ width: 300px;
+ height: 0px;
+ }
- .toaster-alert {
+ .toaster-alert {
- cursor: pointer;
- }
+ cursor: pointer;
+ }
- footer.footer {
- color: #555;
- text-align: center;
- }
- footer.footer a {
- color: #222;
- }
+ footer.footer {
+ color: #555;
+ text-align: center;
+ }
+ footer.footer a {
+ color: #222;
+ }
- #post_window {
- width: 100%;
- position: absolute;
- height: 350px;
- left: 0px;
- bottom: 0px;
- background: white;
- }
+ #post_window {
+ width: 100%;
+ position: absolute;
+ height: 350px;
+ left: 0px;
+ bottom: 0px;
+ background: white;
+ }
- #post_window input {
- width: 100%;
- height: 30px;
- padding: 5px;
- }
- #post_window textarea {
- width: 100%;
- background: #222;
- height: 220px;
- resize: none;
- border-radius: 0;
- border: 1px solid #111;
- font-size: 16px;
- color: #bebebe;
- outline: 0;
- }
- #post_window textarea:focus {
- outline: 0;
- border:none !important;
- box-shadow:none !important;
- }
+ #post_window input {
+ width: 100%;
+ height: 30px;
+ padding: 5px;
+ }
+ #post_window textarea {
+ width: 100%;
+ background: #222;
+ height: 220px;
+ resize: none;
+ border-radius: 0;
+ border: 1px solid #111;
+ font-size: 16px;
+ color: #bebebe;
+ outline: 0;
+ }
+ #post_window textarea:focus {
+ outline: 0;
+ border:none !important;
+ box-shadow:none !important;
+ }
- #post_window .post-title-container {
- opacity: 0.8;
- height: 50px;
- }
+ #post_window .post-title-container {
+ opacity: 0.8;
+ height: 50px;
+ }
- #post_window .post-content-container {
- opacity: 0.8;
- background: #000;
- width: 100%;
- height: 300px;
+ #post_window .post-content-container {
+ opacity: 0.8;
+ background: #000;
+ width: 100%;
+ height: 300px;
- }
+ }
- .topic-container {
- list-style-type: none;
- padding: 0;
- margin: 0;
- border: 1px solid #eee;
- margin-top: 50px;
- }
- .topic-container li.topic-row:nth-child(odd) {
- background-color:#fdfdfd;
- }
- .topic-container li.topic-row:nth-child(even) {
- background-color:#fff;
- }
- .topic-container li.topic-row {
- cursor: pointer;
- border-bottom: 1px solid #eee;
- padding: 10px;
+ .topic-container {
+ list-style-type: none;
+ padding: 0;
+ margin: 0;
+ border: 1px solid #eee;
+ margin-top: 50px;
+ }
+ .topic-container li.topic-row:nth-child(odd) {
+ background-color:#fdfdfd;
+ }
+ .topic-container li.topic-row:nth-child(even) {
+ background-color:#fff;
+ }
+ .topic-container li.topic-row {
+ cursor: pointer;
+ border-bottom: 1px solid #eee;
+ padding: 10px;
- }
- .topic-container li.topic-row:hover {
- background-color: #eee;
- }
+ }
+ .topic-container li.topic-row:hover {
+ background-color: #eee;
+ }
</style>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
- <div class="navbar-inner">
- <div class="container">
- <div class="nav-collapse collapse">
- <ul class="nav">
- <li class="active"><a href="/">Home</a></li>
- <li><a href="/register">Register</a></li>
- <li><a href="/login">Login</a></li>
- </ul>
- </div>
- </div>
- </div>
- </div>
- <div id="post_window">
- <div class="post-title-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="navbar-inner">
+ <div class="container">
+ <div class="nav-collapse collapse">
+ <a class="brand" href="#">NodeBB</a>
+ <ul class="nav">
+ <li class="active"><a href="/">Home</a></li>
+ <li><a href="/register">Register</a></li>
+ <li><a href="/login">Login</a></li>
+ </ul>
+ <ul class="nav pull-right">
+ <li><a href="/logout" id="logout">Log out</a></li>
+ </ul>
+ </div>
+ </div>
+ </div>
+ </div>
+ <div id="post_window">
+ <div class="post-title-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-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-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-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 class="btn-toolbar">
+ <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-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-list"></i></a>
</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 class="container" id="content">
\ No newline at end of file
+ <div class="container" id="content">
\ No newline at end of file
diff --git a/public/templates/logout.tpl b/public/templates/logout.tpl
new file mode 100644
index 0000000000..987b4b68ef
--- /dev/null
+++ b/public/templates/logout.tpl
@@ -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>
diff --git a/src/templates.js b/src/templates.js
index 9c909aceec..4cd4ae86bf 100644
--- a/src/templates.js
+++ b/src/templates.js
@@ -27,6 +27,7 @@ var fs = require('fs');
loadTemplates([
'header', 'footer', 'register', 'home',
'login', 'reset', 'reset_code', 'account_settings',
+ 'logout',
'emails/reset', 'emails/reset_plaintext'
]);
}
diff --git a/src/user.js b/src/user.js
index 95ee11cf59..e08727f3d4 100644
--- a/src/user.js
+++ b/src/user.js
@@ -21,20 +21,33 @@ var config = require('../config.js'),
return global.socket.emit('user.login', {'status': 0, 'message': 'Incorrect username / password combination.'});
} else {
// Start, replace, or extend a session
- RDB.get('session:' + user.sessionID, function(session) {
+ RDB.get('sess:' + user.sessionID, function(session) {
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 {
- 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!'});
}
});
});
};
+ 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) {
if (username == null || password == null) {
@@ -104,7 +117,7 @@ var config = require('../config.js'),
};
User.get_uid_by_session = function(session, callback) {
- RDB.get('session:' + session, callback);
+ RDB.get('sess:' + session + ':uid', callback);
};
User.reset = {
diff --git a/src/webserver.js b/src/webserver.js
index f166d1b7af..678ad74b19 100644
--- a/src/webserver.js
+++ b/src/webserver.js
@@ -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) {
// Include this middleware if the endpoint requires a logged in user to view
- hasAuth(req, res, function() {
- if (!global.uid) {
- res.redirect('/403');
- } else {
- console.log('info: [Auth] User is logged in as uid: ' + uid);
- next();
- }
- });
+ if (!global.uid) {
+ res.redirect('/403');
+ } else {
+ next();
+ }
}
// Middlewares
@@ -46,16 +29,32 @@ var express = require('express'),
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.session({
- store: new RedisStore(),
+ store: new RedisStore({
+ ttl: 60*60*24*14
+ }),
secret: 'nodebb',
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
// 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)
// app.use(express.methodOverride());
- app.get('/', hasAuth, function(req, res) {
+ app.get('/', function(req, res) {
global.modules.topics.generate_forum_body(function(forum_body) {
res.send(templates['header'] + forum_body + templates['footer']);
})
@@ -63,10 +62,19 @@ var express = require('express'),
//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']);
});
+ 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) {
res.send(templates['header'] + templates['reset_code'].parse({ reset_code: req.params.code }) + templates['footer']);
});