Skip to content

Commit

Permalink
Add authenticated and logout events (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
daffl committed Aug 29, 2018
1 parent 5a1f6e7 commit 3741629
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
26 changes: 16 additions & 10 deletions packages/authentication-client/src/passport.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ export default class Passport {
const emit = app.io ? 'emit' : 'send';
return this.authenticateSocket(credentials, socket, emit).then(this.setJWT);
});
}).then(payload => {
app.emit('authenticated', payload);
return payload;
});
}

Expand Down Expand Up @@ -213,16 +216,19 @@ export default class Passport {
this.clearCookie(this.options.cookie);

// remove the accessToken from localStorage
return Promise.resolve(app.get('storage')
.removeItem(this.options.storageKey)).then(() => {
// If using sockets de-authenticate the socket
if (app.io || app.primus) {
const method = app.io ? 'emit' : 'send';
const socket = app.io ? app.io : app.primus;

return this.logoutSocket(socket, method);
}
});
return Promise.resolve(app.get('storage').removeItem(this.options.storageKey)).then(() => {
// If using sockets de-authenticate the socket
if (app.io || app.primus) {
const method = app.io ? 'emit' : 'send';
const socket = app.io ? app.io : app.primus;

return this.logoutSocket(socket, method);
}
}).then(result => {
app.emit('logout', result);

return result;
});
}

setJWT (data) {
Expand Down
23 changes: 23 additions & 0 deletions packages/authentication-client/test/integration/primus.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,20 @@ describe('Primus client authentication', function () {
});
});

it('`authenticated` event', done => {
client.once('authenticated', response => {
try {
expect(response.accessToken).to.not.equal(undefined);
expect(client.get('accessToken')).to.deep.equal(response.accessToken);
done();
} catch (e) {
done(e);
}
});

client.authenticate(options);
});

it('local username password authentication and access to protected service', () => {
return client.authenticate(options).then(response => {
expect(response.accessToken).to.not.equal(undefined);
Expand Down Expand Up @@ -151,6 +165,15 @@ describe('Primus client authentication', function () {
});
});

it('`logout` event', done => {
client.once('logout', () => done());

client.authenticate(options).then(response => {
expect(response.accessToken).to.not.equal(undefined);
return client.logout();
});
});

it('authenticates automatically after reconnection', done => {
client.authenticate(options).then(response => {
app.primus.end({ reconnect: true });
Expand Down
23 changes: 23 additions & 0 deletions packages/authentication-client/test/integration/rest.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,20 @@ describe('REST client authentication', () => {
});
});

it('`authenticated` event', done => {
client.once('authenticated', response => {
try {
expect(response.accessToken).to.not.equal(undefined);
expect(client.get('accessToken')).to.deep.equal(response.accessToken);
done();
} catch (e) {
done(e);
}
});

client.authenticate(options);
});

it('local username password authentication and access to protected service', () => {
return client.authenticate(options).then(response => {
expect(response.accessToken).to.not.equal(undefined);
Expand Down Expand Up @@ -129,4 +143,13 @@ describe('REST client authentication', () => {
});
});
});

it('`logout` event', done => {
client.once('logout', () => done());

client.authenticate(options).then(response => {
expect(response.accessToken).to.not.equal(undefined);
return client.logout();
});
});
});
23 changes: 23 additions & 0 deletions packages/authentication-client/test/integration/socketio.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ describe('Socket.io client authentication', function () {
});
});

it('`authenticated` event', done => {
client.once('authenticated', response => {
try {
expect(response.accessToken).to.not.equal(undefined);
expect(client.get('accessToken')).to.deep.equal(response.accessToken);
done();
} catch (e) {
done(e);
}
});

client.authenticate(options);
});

it('local username password authentication and access to protected service', () => {
return client.authenticate(options).then(response => {
expect(response.accessToken).to.not.equal(undefined);
Expand Down Expand Up @@ -144,6 +158,15 @@ describe('Socket.io client authentication', function () {
});
});

it('`logout` event', done => {
client.once('logout', () => done());

client.authenticate(options).then(response => {
expect(response.accessToken).to.not.equal(undefined);
return client.logout();
});
});

it('authenticates automatically after reconnection', done => {
client.authenticate(options).then(response => {
app.io.close();
Expand Down

0 comments on commit 3741629

Please sign in to comment.