Skip to content

Commit

Permalink
fix(authentication): Fall back when req.app is not the application wh…
Browse files Browse the repository at this point in the history
…en emitting events (#1185)
  • Loading branch information
daffl authored Jan 26, 2019
1 parent bd99658 commit 6a534f0
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/authentication-oauth1/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function init (options = {}) {
auth.express.authenticate(name, oauth1Settings),
handler,
errorHandler,
auth.express.emitEvents(authSettings),
auth.express.emitEvents(authSettings, app),
auth.express.setCookie(authSettings),
auth.express.successRedirect(),
auth.express.failureRedirect(authSettings),
Expand Down
2 changes: 1 addition & 1 deletion packages/authentication-oauth2/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ function init (options = {}) {
auth.express.authenticate(name, omit(oauth2Settings, 'state')),
handler,
errorHandler,
auth.express.emitEvents(authSettings),
auth.express.emitEvents(authSettings, app),
auth.express.setCookie(authSettings),
auth.express.successRedirect(),
auth.express.failureRedirect(authSettings),
Expand Down
4 changes: 2 additions & 2 deletions packages/authentication/lib/express/emit-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const Debug = require('debug');

const debug = Debug('feathers-authentication:express:emit-events');

module.exports = function emitEvents () {
module.exports = function emitEvents (settings, _app) {
return function (req, res, next) {
const method = res.hook && res.hook.method;

Expand All @@ -15,7 +15,7 @@ module.exports = function emitEvents () {
}

if (res.data && res.data.accessToken && event) {
const { app } = req;
const app = req.app && typeof req.app.emit === 'function' ? req.app : _app;

debug(`Sending '${event}' event for REST provider. Token is`, res.data.accessToken);

Expand Down
2 changes: 1 addition & 1 deletion packages/authentication/lib/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ module.exports = function init (options) {
app.use(
path,
new Service(app, options),
emitEvents(options),
emitEvents(options, app),
setCookie(options),
successRedirect(),
failureRedirect(options)
Expand Down
13 changes: 13 additions & 0 deletions packages/authentication/test/express/emit-events.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,19 @@ describe('express:emitEvents', () => {
done();
});
});

it('works with app fallback', done => {
const fallback = {
emit: sinon.spy()
};
delete req.app.emit;
emitEvents({}, fallback)(req, res, () => {
expect(fallback.emit).to.have.been.calledOnce;
expect(fallback.emit).to.have.been.calledWith('login', res.data, { provider: 'rest', req, res });
req.app.emit = sinon.spy();
done();
});
});
});

describe('when remove method was called', () => {
Expand Down

0 comments on commit 6a534f0

Please sign in to comment.