ensl_gathers/config/socketio.js

73 lines
1.9 KiB
JavaScript
Raw Normal View History

2015-07-20 09:41:58 +00:00
"use strict";
2015-07-22 15:35:40 +00:00
var winston = require("winston");
2015-07-22 23:30:14 +00:00
var User = require("../lib/user/user");
2015-08-18 09:56:35 +00:00
var config = require("./config");
var EnslClient = require("../lib/ensl/client");
var client = EnslClient();
2015-07-22 23:30:14 +00:00
var chatController = require("../lib/chat/controller");
var gatherController = require("../lib/gather/controller");
var userController = require("../lib/user/controller");
2015-08-18 11:40:48 +00:00
var usersHelper = require("../lib/user/helper");
var env = process.env.NODE_ENV || "development";
2015-07-21 14:10:24 +00:00
2015-08-18 11:09:33 +00:00
var parseCookies = EnslClient.parseCookies;
2015-08-18 09:56:35 +00:00
2015-08-18 11:40:48 +00:00
var assignRandomUser = (socket, next) => {
usersHelper.getRandomUser(function (error, _, body) {
if (error) {
winston.error(error);
return next(new Error("Authentication Failed"))
}
socket._user = new User(body);
return next();
});
};
2015-08-10 23:56:53 +00:00
module.exports = io => {
var rootNamespace = io.of('/')
2015-08-18 09:56:35 +00:00
// Authentication
2015-08-10 23:56:53 +00:00
io.use((socket, next) => {
2015-08-19 13:59:28 +00:00
let cookies = parseCookies(socket);
let session;
if (cookies) {
session = EnslClient.decodeSession(cookies[config.session_store_name]);
}
2015-08-18 09:56:35 +00:00
2015-08-27 11:07:45 +00:00
2015-08-18 11:40:48 +00:00
if (!session || typeof session.user !== 'number') {
// return next(new Error("Authentication Failed"));
/* Temporarily Allow Random Users in staging*/
if (env === 'staging') {
return assignRandomUser(socket, next);
} else {
return next(new Error("Authentication Failed"));
}
/****************************/
}
2015-08-18 09:56:35 +00:00
client.getUserById({
id: session.user
}, (error, response, body) => {
2015-08-18 10:40:15 +00:00
if (error || response.statusCode !== 200) {
2015-07-22 15:35:40 +00:00
winston.error(error);
return next(new Error("Authentication Failed"))
2015-07-22 15:35:40 +00:00
};
2015-07-22 23:30:14 +00:00
socket._user = new User(body);
2015-08-27 12:06:54 +00:00
console.log(socket._user)
2015-08-27 12:02:45 +00:00
if (socket._user.bans.gather) {
return next(new Error("Gather Banned"));
}
2015-08-18 09:56:35 +00:00
winston.info("Logged in:", body.username, body.id);
return next();
});
2015-07-21 00:24:14 +00:00
});
userController(rootNamespace);
chatController(rootNamespace);
gatherController(rootNamespace);
};