2012-11-16 16:43:12 +01:00
|
|
|
var express = require('express'),
|
|
|
|
connect = require('connect'),
|
|
|
|
passport = require('passport'),
|
2013-01-07 15:30:15 +01:00
|
|
|
http = require('http'),
|
|
|
|
xtend = require('xtend');
|
2012-11-16 16:43:12 +01:00
|
|
|
|
|
|
|
var socketIo = require('socket.io'),
|
|
|
|
passportSocketIo = require('../../lib');
|
|
|
|
|
|
|
|
var sessionStore = new connect.session.MemoryStore(),
|
2013-02-05 23:15:04 +01:00
|
|
|
sessionSecret = 'asdasdsdas1312312',
|
|
|
|
sessionKey = 'test-session-key',
|
|
|
|
sessionOptions = {
|
|
|
|
store: sessionStore,
|
|
|
|
key: sessionKey,
|
|
|
|
secret: sessionSecret
|
|
|
|
};
|
2012-11-16 16:43:12 +01:00
|
|
|
|
|
|
|
var server;
|
|
|
|
|
|
|
|
require('./setupPassport');
|
|
|
|
|
2013-01-07 15:30:15 +01:00
|
|
|
exports.start = function (options, callback) {
|
|
|
|
|
|
|
|
if(typeof options == 'function'){
|
|
|
|
callback = options;
|
2013-06-05 13:38:33 +02:00
|
|
|
options = {
|
|
|
|
};
|
|
|
|
}
|
|
|
|
options.cookieParser = express.cookieParser;
|
2013-01-07 15:30:15 +01:00
|
|
|
|
2012-11-16 16:43:12 +01:00
|
|
|
var app = express();
|
|
|
|
app.configure(function(){
|
2013-02-05 23:15:04 +01:00
|
|
|
app.use(express.cookieParser());
|
2012-11-16 16:43:12 +01:00
|
|
|
|
|
|
|
app.use(express.bodyParser());
|
|
|
|
app.use(express.methodOverride());
|
|
|
|
|
2013-02-05 23:15:04 +01:00
|
|
|
app.use(express.session(sessionOptions));
|
2012-11-16 16:43:12 +01:00
|
|
|
|
|
|
|
app.use(passport.initialize());
|
|
|
|
app.use(passport.session());
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
app.post('/login', passport.authenticate('local', { successRedirect: '/',
|
|
|
|
failureRedirect: '/login',
|
|
|
|
failureFlash: true }));
|
|
|
|
|
|
|
|
app.get('/', function(req, res){
|
|
|
|
if(!req.user){
|
|
|
|
res.send(401);
|
|
|
|
}else{
|
|
|
|
res.json(req.user);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
server = http.createServer(app);
|
|
|
|
|
|
|
|
var sio = socketIo.listen(server);
|
|
|
|
sio.configure(function(){
|
2013-02-05 23:15:04 +01:00
|
|
|
this.set('authorization', passportSocketIo.authorize(xtend(sessionOptions, options)));
|
2012-11-16 16:43:12 +01:00
|
|
|
|
|
|
|
this.set('log level', 0);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
sio.sockets.on('echo', function (m) {
|
|
|
|
sio.sockets.emit('echo-response', m);
|
|
|
|
});
|
|
|
|
|
|
|
|
server.listen(9000, callback);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.stop = function (callback) {
|
|
|
|
server.close();
|
|
|
|
callback();
|
|
|
|
};
|