Skip to content

Commit fa94b2f

Browse files
committed
fix: reduce authentication connection hook complexity and remove unnecessary checks
1 parent 65b43bd commit fa94b2f

File tree

2 files changed

+10
-22
lines changed

2 files changed

+10
-22
lines changed

packages/authentication/lib/hooks/connection.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@ module.exports = (strategy = 'jwt') => {
55
const {
66
method,
77
result: { accessToken },
8-
params: { provider, connection }
8+
params: { connection }
99
} = context;
1010

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

15-
if (connection) {
16-
const { authentication: { accessToken: currentToken } = {} } = connection;
15+
const { authentication = {} } = connection;
1716

18-
if (method === 'remove' && accessToken === currentToken) {
19-
debug('Removing authentication information from real-time connection');
20-
delete connection.authentication;
21-
} else if (method === 'create' && accessToken) {
22-
debug('Adding authentication information to real-time connection');
23-
connection.authentication = { strategy, accessToken };
24-
}
17+
if (method === 'remove' && accessToken === authentication.accessToken) {
18+
debug('Removing authentication information from real-time connection');
19+
delete connection.authentication;
20+
} else if (method === 'create' && accessToken) {
21+
debug('Adding authentication information to real-time connection');
22+
connection.authentication = { strategy, accessToken };
2523
}
2624

2725
return context;

packages/authentication/test/hooks/connection.test.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,6 @@ describe('authentication/hooks/connection', () => {
2929
});
3030
});
3131

32-
it('throws error if connection is not set for expected provider', () => {
33-
return service.create({}, { provider: 'socketio' })
34-
.then(() => assert.fail('Should never get here'))
35-
.catch(error => {
36-
assert.strictEqual(error.message, `No connection object found. ` +
37-
`Please make sure you are using the latest version of '@feathersjs/socketio' ` +
38-
`and params.connection is set.`);
39-
});
40-
});
41-
4232
it('create does nothing when there is no connection', () => {
4333
return service.create({}, {}).then(result => {
4434
assert.deepStrictEqual(result, {

0 commit comments

Comments
 (0)