Skip to content

Commit

Permalink
fix: reduce authentication connection hook complexity and remove unne…
Browse files Browse the repository at this point in the history
…cessary checks
  • Loading branch information
daffl committed Mar 10, 2019
1 parent 65b43bd commit fa94b2f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
22 changes: 10 additions & 12 deletions packages/authentication/lib/hooks/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ module.exports = (strategy = 'jwt') => {
const {
method,
result: { accessToken },
params: { provider, connection }
params: { connection }
} = context;

if (!connection && (provider === 'socketio' || provider === 'primus')) {
throw new Error(`No connection object found. Please make sure you are using the latest version of '@feathersjs/${provider}' and params.connection is set.`);
if (!connection) {
return context;
}

if (connection) {
const { authentication: { accessToken: currentToken } = {} } = connection;
const { authentication = {} } = connection;

if (method === 'remove' && accessToken === currentToken) {
debug('Removing authentication information from real-time connection');
delete connection.authentication;
} else if (method === 'create' && accessToken) {
debug('Adding authentication information to real-time connection');
connection.authentication = { strategy, accessToken };
}
if (method === 'remove' && accessToken === authentication.accessToken) {
debug('Removing authentication information from real-time connection');
delete connection.authentication;
} else if (method === 'create' && accessToken) {
debug('Adding authentication information to real-time connection');
connection.authentication = { strategy, accessToken };
}

return context;
Expand Down
10 changes: 0 additions & 10 deletions packages/authentication/test/hooks/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,6 @@ describe('authentication/hooks/connection', () => {
});
});

it('throws error if connection is not set for expected provider', () => {
return service.create({}, { provider: 'socketio' })
.then(() => assert.fail('Should never get here'))
.catch(error => {
assert.strictEqual(error.message, `No connection object found. ` +
`Please make sure you are using the latest version of '@feathersjs/socketio' ` +
`and params.connection is set.`);
});
});

it('create does nothing when there is no connection', () => {
return service.create({}, {}).then(result => {
assert.deepStrictEqual(result, {
Expand Down

0 comments on commit fa94b2f

Please sign in to comment.