2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00
socketio-jwt/test/fixture/index.js
José F. Romaniello 54dfbdf987 improve tests
2015-05-29 09:39:49 -03:00

80 lines
1.7 KiB
JavaScript

var express = require('express');
var http = require('http');
var socketIo = require('socket.io');
var socketio_jwt = require('../../lib');
var jwt = require('jsonwebtoken');
var xtend = require('xtend');
var server, sio;
exports.start = function (options, callback) {
if(typeof options == 'function'){
callback = options;
options = {};
}
options = xtend({
secret: 'aaafoo super sercret',
timeout: 1000,
handshake: true
}, options);
var app = express();
app.configure(function(){
this.use(express.json());
this.use(express.urlencoded());
});
app.post('/login', function (req, res) {
var profile = {
first_name: 'John',
last_name: 'Doe',
email: 'john@doe.com',
id: 123
};
// We are sending the profile inside the token
var token = jwt.sign(profile, options.secret, { expiresInMinutes: 60*5 });
res.json({token: token});
});
server = http.createServer(app);
sio = socketIo.listen(server);
if (options.handshake) {
// this.set('authorization', socketio_jwt.authorize(options));
sio.use(socketio_jwt.authorize(options));
}
if (options.handshake) {
sio.sockets.on('echo', function (m) {
sio.sockets.emit('echo-response', m);
});
} else {
sio.sockets
.on('connection', socketio_jwt.authorize(options))
.on('authenticated', function (socket) {
socket.on('echo', function (m) {
socket.emit('echo-response', m);
});
});
}
server.__sockets = [];
server.on('connection', function (c) {
server.__sockets.push(c);
});
server.listen(9000, callback);
};
exports.stop = function (callback) {
sio.close();
callback();
};