Skip to content
This repository has been archived by the owner on Mar 22, 2022. It is now read-only.

allow runtime auth via socket.io #4

Merged
merged 2 commits into from Nov 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions package.json
@@ -1,9 +1,9 @@
{
"name": "feathers-authentication",
"description": "Add Authentication to your FeathersJS app.",
"version": "0.0.0",
"version": "0.0.1",
"homepage": "https://github.com/feathersjs/feathers-authentication",
"main": "lib/",
"main": "src/",
"keywords": [
"feathers",
"feathers-plugin"
Expand Down
38 changes: 20 additions & 18 deletions src/index.js
Expand Up @@ -101,19 +101,31 @@ export default function(config) {

debug('running app.setup');

function setUserData(socket, data) {
socket.feathers = _.extend({ user: data }, socket.feathers);
};

function checkToken(token, socket, callback) {
if (!token) return callback(null, true);
jwt.verify(token, settings.secret, function(err, data) {
if (err) return callback(err);
setUserData(socket, data);
callback(null, data);
});
};

// Socket.io middleware
if(io) {
debug('intializing SocketIO middleware');
io.use(function(socket, next) {
// If there's a token in place, decode it and set up the feathers.user
if (socket.handshake.query.token) {
jwt.verify(socket.handshake.query.token, settings.secret, function(err, data) {
if (err) {
return next(err);
}
socket.feathers = _.extend({ user: data }, socket.feathers);
checkToken(socket.handshake.query.token, socket, next);
socket.on('authenticate', (data) => {
checkToken(data.token, socket, (err, data) => {
delete data.password;
if (data) socket.emit('authenticated', data);
});
}
});
// If no token was passed, still allow the websocket. Service hooks can take care of Auth.
return next(null, true);
});
Expand All @@ -123,17 +135,7 @@ export default function(config) {
if(primus) {
debug('intializing Primus middleware');
primus.authorize(function(req, done) {
// If there's a token in place, decode it and set up the feathers.user
if (req.handshake.query.token) {
jwt.verify(req.handshake.query.token, settings.secret, function(err, data) {
if (err) {
return done(err);
}
req.feathers = _.extend({ user: data }, req.feathers);
});
}
// If no token was passed, still allow the websocket. Service hooks can take care of Auth.
return done(null, true);
checkToken(req.handshake.query.token, socket, done);
});
}

Expand Down