mirror of
https://github.com/NodeBB/NodeBB.git
synced 2025-11-01 03:26:04 +01:00
fixing login and logout sessions
This commit is contained in:
@@ -30,8 +30,7 @@
|
||||
jQuery('#error').show(50);
|
||||
jQuery('#error p').html(data.message);
|
||||
} else {
|
||||
jQuery('#error').hide(50);
|
||||
ajaxify.go('/');
|
||||
document.location.href = '/';
|
||||
}
|
||||
});
|
||||
}());
|
||||
|
||||
22
src/user.js
22
src/user.js
@@ -33,25 +33,17 @@ var config = require('../config.js'),
|
||||
|
||||
global.uid = uid;
|
||||
|
||||
global.socket.emit('event:alert', {
|
||||
title: 'Welcome ' + user.username,
|
||||
message: 'You have successfully logged in.',
|
||||
type: 'notify',
|
||||
timeout: 2000
|
||||
});
|
||||
|
||||
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) {
|
||||
User.logout = function(sessionID, callback) {
|
||||
User.get_uid_by_session(sessionID, function(uid) {
|
||||
if (uid) {
|
||||
RDB.del('sess:' + sessionID + ':uid');
|
||||
RDB.del('uid:' + global.uid + ':session');
|
||||
global.uid = null;
|
||||
RDB.del('uid:' + uid + ':session');
|
||||
callback(true);
|
||||
} else callback(false);
|
||||
});
|
||||
@@ -236,6 +228,7 @@ var config = require('../config.js'),
|
||||
},
|
||||
keys = [];
|
||||
|
||||
if (active.length > 0) {
|
||||
for(var a in active) {
|
||||
keys.push('sess:' + active[a].split(':')[1] + ':uid');
|
||||
}
|
||||
@@ -258,10 +251,13 @@ var config = require('../config.js'),
|
||||
callback(returnObj);
|
||||
}
|
||||
});
|
||||
} else {
|
||||
global.socket.emit('api:user.active.get', returnObj)
|
||||
}
|
||||
});
|
||||
},
|
||||
register: function(sessionID) {
|
||||
RDB.set('active:' + sessionID, 60*10); // Active state persists for 10 minutes
|
||||
RDB.set('active:' + sessionID, '', 60*10); // Active state persists for 10 minutes
|
||||
}
|
||||
}
|
||||
}(exports));
|
||||
@@ -36,18 +36,33 @@ var express = require('express'),
|
||||
key: 'express.sid'
|
||||
}));
|
||||
app.use(function(req, res, next) {
|
||||
if (global.uid === undefined) {
|
||||
var hasExtension = /\.[\w]{2,4}$/;
|
||||
if (!hasExtension.test(req.url.indexOf('?') !== -1 ? req.url.substr(0, req.url.indexOf('?')) : req.url)) {
|
||||
console.log('REQUESTING: ' + req.url);
|
||||
if (req.session.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.');
|
||||
if (uid !== null) {
|
||||
req.session.uid = uid;
|
||||
|
||||
global.socket.emit('event:alert', {
|
||||
title: 'Welcome ' + user.username,
|
||||
message: 'You have successfully logged in.',
|
||||
type: 'notify',
|
||||
timeout: 2000
|
||||
});
|
||||
} else req.session.uid = 0;
|
||||
|
||||
if (req.session.uid) console.log('info: [Auth] uid ' + req.session.uid + ' found. Welcome back.');
|
||||
else console.log('info: [Auth] No login session found.');
|
||||
});
|
||||
} else {
|
||||
// console.log('SESSION: ' + req.sessionID);
|
||||
// console.log('info: [Auth] Ping from uid ' + req.session.uid);
|
||||
}
|
||||
|
||||
// (Re-)register the session as active
|
||||
global.modules.user.active.register(req.sessionID);
|
||||
} else {
|
||||
console.log('info: [Auth] Ping from uid ' + global.uid);
|
||||
}
|
||||
|
||||
next();
|
||||
@@ -85,6 +100,7 @@ var express = require('express'),
|
||||
break;
|
||||
default :
|
||||
res.send('{}');
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
@@ -94,8 +110,11 @@ var express = require('express'),
|
||||
|
||||
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();
|
||||
global.modules.user.logout(req.sessionID, function(logout) {
|
||||
if (logout === true) {
|
||||
delete(req.session.uid);
|
||||
req.session.destroy();
|
||||
}
|
||||
});
|
||||
|
||||
res.send(templates['header'] + templates['logout'] + templates['footer']);
|
||||
|
||||
Reference in New Issue
Block a user