2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-11-09 22:20:08 +01:00
socketio-jwt/test/fixture/namespace.js

72 lines
1.5 KiB
JavaScript
Raw Normal View History

2015-08-31 16:04:04 +02:00
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');
2015-08-31 16:08:31 +02:00
var bodyParser = require('body-parser');
2015-08-31 16:04:04 +02:00
var server, sio;
var enableDestroy = require('server-destroy');
/**
* This is an example server that shows how to do namespace authentication.
*
* The /admin namespace is protected by JWTs while the global namespace is public.
*/
exports.start = function (callback) {
options = {
secret: 'aaafoo super sercret',
timeout: 1000,
handshake: false
};
var app = express();
2015-08-31 16:08:31 +02:00
app.use(bodyParser.json());
2015-08-31 16:04:04 +02:00
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);
sio.on('connection', function (socket) {
socket.emit('hi');
});
var admin_nsp = sio.of('/admin');
admin_nsp.on('connection', socketio_jwt.authorize(options))
.on('authenticated', function (socket) {
socket.emit('hi admin');
});
server.listen(9000, callback);
enableDestroy(server);
};
exports.stop = function (callback) {
sio.close();
2015-08-31 16:14:48 +02:00
try {
server.destroy();
} catch (er) {}
2015-08-31 16:04:04 +02:00
callback();
};