feathers-authentication-client
Advanced tools
Comparing version 0.1.2 to 0.1.3
@@ -31,2 +31,6 @@ 'use strict'; | ||
if (app.passport) { | ||
throw new Error('You have already registered authentication on this client app instance. You only need to do it once.'); | ||
} | ||
this.options = options; | ||
@@ -40,92 +44,97 @@ this.app = app; | ||
this.getJWT().then(this.setJWT); | ||
this.setupSocketListeners(); | ||
} | ||
_createClass(Passport, [{ | ||
key: 'connected', | ||
value: function connected() { | ||
var _this = this, | ||
_arguments = arguments; | ||
key: 'setupSocketListeners', | ||
value: function setupSocketListeners() { | ||
var _this = this; | ||
var app = this.app; | ||
var socket = app.io || app.primus; | ||
var emit = app.io ? 'emit' : 'send'; | ||
var reconnected = app.io ? 'reconnect' : 'reconnected'; | ||
return new Promise(function (resolve, reject) { | ||
if (app.rest) { | ||
return resolve(); | ||
} | ||
if (!socket) { | ||
return; | ||
} | ||
var socket = app.io || app.primus; | ||
socket.on(reconnected, function () { | ||
debug('Socket reconnected'); | ||
if (!socket) { | ||
return reject(new Error('It looks like your client connection has not been configured.')); | ||
// If socket was already authenticated then re-authenticate | ||
// it with the server automatically. | ||
if (socket.authenticated) { | ||
var data = { | ||
strategy: _this.options.jwtStrategy, | ||
accessToken: app.get('accessToken') | ||
}; | ||
_this.authenticateSocket(data, socket, emit).then(_this.setJWT).catch(function (error) { | ||
debug('Error re-authenticating after socket reconnect', error); | ||
socket.authenticated = false; | ||
app.emit('reauthentication-error', error); | ||
}); | ||
} | ||
}); | ||
// If the socket is not connected yet we have to wait for the `connect` event | ||
if (app.io && !socket.connected || app.primus && socket.readyState !== 3) { | ||
var connected = app.primus ? 'open' : 'connect'; | ||
debug('Waiting for socket connection'); | ||
if (socket.io) { | ||
socket.io.engine.on('upgrade', function () { | ||
debug('Socket upgrading'); | ||
socket.on(connected, function () { | ||
debug('Socket connected'); | ||
// If socket was already authenticated then re-authenticate | ||
// it with the server automatically. | ||
if (socket.authenticated) { | ||
var data = { | ||
strategy: _this.options.jwtStrategy, | ||
accessToken: app.get('accessToken') | ||
}; | ||
var emit = app.io ? 'emit' : 'send'; | ||
var disconnect = app.io ? 'disconnect' : 'end'; | ||
var reconnecting = app.io ? 'reconnecting' : 'reconnect'; | ||
var reconnected = app.io ? 'reconnect' : 'reconnected'; | ||
// If one of those events happens before `connect` the promise will be rejected | ||
// If it happens after, it will do nothing (since Promises can only resolve once) | ||
socket.on(disconnect, function () { | ||
debug('Socket disconnected'); | ||
_this.authenticateSocket(data, socket, emit).then(_this.setJWT).catch(function (error) { | ||
debug('Error re-authenticating after socket upgrade', error); | ||
socket.authenticated = false; | ||
socket.removeAllListeners(); | ||
app.emit('reauthentication-error', error); | ||
}); | ||
} | ||
}); | ||
} | ||
} | ||
}, { | ||
key: 'connected', | ||
value: function connected() { | ||
var app = this.app; | ||
socket.on(reconnecting, function () { | ||
debug('Socket reconnecting'); | ||
}); | ||
if (app.rest) { | ||
return Promise.resolve(); | ||
} | ||
socket.on(reconnected, function () { | ||
debug('Socket reconnected'); | ||
var socket = app.io || app.primus; | ||
// If socket was already authenticated then re-authenticate | ||
// it with the server automatically. | ||
if (socket.authenticated) { | ||
var data = { | ||
strategy: _this.options.jwtStrategy, | ||
accessToken: app.get('accessToken') | ||
}; | ||
_this.authenticateSocket(data, socket, emit).then(_this.setJWT).catch(function (error) { | ||
debug('Error re-authenticating after socket upgrade', error); | ||
socket.authenticated = false; | ||
app.emit('reauthentication-error', error); | ||
}); | ||
} | ||
}); | ||
if (!socket) { | ||
return Promise.reject(new Error('It looks like your client connection has not been configured.')); | ||
} | ||
if (socket.io) { | ||
socket.io.engine.on('upgrade', function () { | ||
debug('Socket upgrading', _arguments); | ||
if (app.io && socket.connected || app.primus && socket.readyState === 3) { | ||
debug('Socket already connected'); | ||
return Promise.resolve(socket); | ||
} | ||
// If socket was already authenticated then re-authenticate | ||
// it with the server automatically. | ||
if (socket.authenticated) { | ||
var data = { | ||
strategy: _this.options.jwtStrategy, | ||
accessToken: app.get('accessToken') | ||
}; | ||
_this.authenticateSocket(data, socket, emit).then(_this.setJWT).catch(function (error) { | ||
debug('Error re-authenticating after socket upgrade', error); | ||
socket.authenticated = false; | ||
app.emit('reauthentication-error', error); | ||
}); | ||
} | ||
}); | ||
} | ||
return new Promise(function (resolve, reject) { | ||
var connected = app.primus ? 'open' : 'connect'; | ||
var disconnect = app.io ? 'disconnect' : 'end'; | ||
debug('Waiting for socket connection'); | ||
resolve(socket); | ||
}); | ||
} else { | ||
debug('Socket already connected'); | ||
var handleDisconnect = function handleDisconnect() { | ||
debug('Socket disconnected before it could connect'); | ||
socket.authenticated = false; | ||
}; | ||
// If disconnect happens before `connect` the promise will be rejected. | ||
socket.once(disconnect, handleDisconnect); | ||
socket.once(connected, function () { | ||
debug('Socket connected'); | ||
debug('Removing ' + disconnect + ' listener'); | ||
socket.removeListener(disconnect, handleDisconnect); | ||
resolve(socket); | ||
} | ||
}); | ||
}); | ||
@@ -132,0 +141,0 @@ } |
{ | ||
"name": "feathers-authentication-client", | ||
"description": "The authentication plugin for feathers-client", | ||
"version": "0.1.2", | ||
"version": "0.1.3", | ||
"homepage": "https://github.com/feathersjs/feathers-authentication-client", | ||
@@ -6,0 +6,0 @@ "main": "lib/", |
@@ -18,2 +18,4 @@ # feathers-authentication-client | ||
**Note:** This is only compatibile with `feathers-authentication@1.x` and above. | ||
## Documentation | ||
@@ -20,0 +22,0 @@ |
603
132
168245