Commit b5520411 authored by Killian Kemps's avatar Killian Kemps

Manage user sessions per socket

Should allow multi-user to use the app at the same time
parent a699c921
......@@ -11,16 +11,18 @@ var facebookMessengerService = require('./facebookMessengerService');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); // for parsing application/x-www-form-urlencoded
app.use(sessions({
var cookieOptions = {
cookieName: 'userSession', // cookie name dictates the key name added to the request object
secret: 'reaaaalsecretblargadeeblargblarg', // should be a large unguessable string
duration: 24 * 60 * 60 * 1000, // how long the session will stay valid in ms
activeDuration: 1000 * 60 * 5 // if expiresIn < activeDuration, the session will be extended by activeDuration milliseconds
}));
};
app.use(sessions(cookieOptions));
app.get('/', function(req, res){
if (req.userSession) {
loginFacebookAppstate(req.userSession.username).then(
if (req.userSession.email) {
loginFacebookAppstate(req.userSession.email).then(
function() {
res.sendFile(__dirname + '/index.html');
}
......@@ -42,10 +44,10 @@ app.get('/login', function(req, res){
});
app.post('/login', function(req, res){
loginFacebookCredentials(req.body.username, req.body.password).then(
loginFacebookCredentials(req.body.email, req.body.password).then(
function(data) {
console.log('Finished login in with credentials');
req.userSession.username = req.body.username;
req.userSession.email = req.body.email;
res.redirect('/');
}
).catch(function(err) {
......@@ -55,10 +57,8 @@ app.post('/login', function(req, res){
});
});
var lastThreadID = '';
var currentUserID = 0;
var fbApi = {};
var chatHistory = {};
var users = {};
function loginFacebookAppstate (email) {
return new Promise(function (resolve, reject) {
......@@ -67,8 +67,13 @@ function loginFacebookAppstate (email) {
if (exists) {
login({appState: JSON.parse(fs.readFileSync(getAppstateName(email), 'utf8'))}, function callback (err, api) {
if(err) return reject(err);
fbApi = api;
resolve(fbApi);
users[email] = {
ID: 0,
fbID: 0,
fbApi: api,
lastThreadID: 0
};
resolve(users[email]);
});
}
else {
......@@ -85,12 +90,13 @@ function loginFacebookCredentials (email, password) {
login({email: email, password: password}, function callback (err, api) {
if(err) return reject(err);
fs.writeFileSync(getAppstateName(email), JSON.stringify(api.getAppState()));
fbApi = api;
users[email] = {
ID:0,
fbID: 0,
fbApi: api,
lastThreadID: 0
};
resolve(fbApi);
resolve(users[email]);
});
});
}
......@@ -99,10 +105,14 @@ function getAppstateName(email) {
return 'appstate-' + email + '.json'
}
function displayCurrentUser () {
function displayCurrentUser() {
console.log('tutu');
return new Promise(function (resolve, reject) {
currentUserID = fbApi.getCurrentUserID();
facebookMessengerService.getUserInfo(fbApi, currentUserID).then(
console.log('tata');
console.log('current user :', currentUser);
reject(currentUser);
currentUser.ID = currentUser.fbApi.getCurrentUserID();
facebookMessengerService.getUserInfo(currentUser.fbApi, currentUser.ID).then(
function(data) {
console.log(data);
io.emit('chat message', 'Logged in as ' + data);
......@@ -112,48 +122,69 @@ function displayCurrentUser () {
}
io.on('connection', function(socket){
displayCurrentUser().then(
var cookieArray = socket.request.headers.cookie.split('=');
var email = sessions.util.decode(cookieOptions, cookieArray[ cookieArray.length - 1 ]).content.email;
console.log('Socket email: ', email);
var currentUser = users[email];
(function() {
return new Promise(function (resolve, reject) {
if (currentUser === undefined) {
reject('currentUser is undefined');
}
currentUser.ID = currentUser.fbApi.getCurrentUserID();
facebookMessengerService.getUserInfo(currentUser.fbApi, currentUser.ID).then(
function(data) {
console.log(data);
io.emit('chat message', 'Logged in as ' + data);
resolve(data);
});
});
})().then(
function() {
console.log('Restoring history');
if (Array.isArray(chatHistory[currentUserID])) {
for (var index in chatHistory[currentUserID]) {
io.emit('chat message', chatHistory[currentUserID][index]);
if (Array.isArray(chatHistory[currentUser.ID])) {
for (var index in chatHistory[currentUser.ID]) {
io.emit('chat message', chatHistory[currentUser.ID][index]);
}
}
}).then(
function() {
console.log('Listening for messages...');
fbApi.listen(function callback(err, message) {
currentUser.fbApi.listen(function callback(err, message) {
if (err) return console.error(err);
console.log(message);
lastThreadID = message.threadID;
currentUser.lastThreadID = message.threadID;
var allInfos = [];
console.log(Object.keys(facebookMessengerService));
allInfos.push(facebookMessengerService.getUserInfo(fbApi, message.senderID));
allInfos.push(facebookMessengerService.getThreadInfo(fbApi, message.threadID));
allInfos.push(facebookMessengerService.getUserInfo(currentUser.fbApi, message.senderID));
allInfos.push(facebookMessengerService.getThreadInfo(currentUser.fbApi, message.threadID));
Promise.all(allInfos).then(function(data) {
console.log(data);
messageToSend = '[thread: ' + data[1] + '] ' + data[0] + ': ' + message.body;
io.emit('chat message', messageToSend);
if (Array.isArray(chatHistory[currentUserID])) {
chatHistory[currentUserID].push(messageToSend);
if (Array.isArray(chatHistory[currentUser.ID])) {
chatHistory[currentUser.ID].push(messageToSend);
}
else {
chatHistory[currentUserID] = [];
chatHistory[currentUserID].push(messageToSend);
chatHistory[currentUser.ID] = [];
chatHistory[currentUser.ID].push(messageToSend);
}
});
});
}
);
).catch(function(err) {
console.log('An error occured: ', err);
});
socket.on('chat message', function(msg){
if (lastThreadID != '') {
if (currentUser.lastThreadID != 0) {
console.log('Sending to FB: ', msg);
fbApi.sendMessage(msg, lastThreadID);
currentUser.fbApi.sendMessage(msg, currentUser.lastThreadID);
}
io.emit('chat message', msg);
});
});
http.listen(3000, function(){
......
......@@ -6,7 +6,7 @@
</head>
<body>
<form action="/login" method="post">
<input type="text" name="username" />
<input type="text" name="email" placeholder="Email" />
<input type="password" name="password" />
<input id="submit" type="submit" />
</form>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment