Skip to content

Commit 6a534f0

Browse files
authored
fix(authentication): Fall back when req.app is not the application when emitting events (#1185)
1 parent bd99658 commit 6a534f0

File tree

5 files changed

+18
-5
lines changed

5 files changed

+18
-5
lines changed

packages/authentication-oauth1/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function init (options = {}) {
7979
auth.express.authenticate(name, oauth1Settings),
8080
handler,
8181
errorHandler,
82-
auth.express.emitEvents(authSettings),
82+
auth.express.emitEvents(authSettings, app),
8383
auth.express.setCookie(authSettings),
8484
auth.express.successRedirect(),
8585
auth.express.failureRedirect(authSettings),

packages/authentication-oauth2/lib/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ function init (options = {}) {
9191
auth.express.authenticate(name, omit(oauth2Settings, 'state')),
9292
handler,
9393
errorHandler,
94-
auth.express.emitEvents(authSettings),
94+
auth.express.emitEvents(authSettings, app),
9595
auth.express.setCookie(authSettings),
9696
auth.express.successRedirect(),
9797
auth.express.failureRedirect(authSettings),

packages/authentication/lib/express/emit-events.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ const Debug = require('debug');
22

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

5-
module.exports = function emitEvents () {
5+
module.exports = function emitEvents (settings, _app) {
66
return function (req, res, next) {
77
const method = res.hook && res.hook.method;
88

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

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

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

packages/authentication/lib/service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ module.exports = function init (options) {
6060
app.use(
6161
path,
6262
new Service(app, options),
63-
emitEvents(options),
63+
emitEvents(options, app),
6464
setCookie(options),
6565
successRedirect(),
6666
failureRedirect(options)

packages/authentication/test/express/emit-events.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ describe('express:emitEvents', () => {
6363
done();
6464
});
6565
});
66+
67+
it('works with app fallback', done => {
68+
const fallback = {
69+
emit: sinon.spy()
70+
};
71+
delete req.app.emit;
72+
emitEvents({}, fallback)(req, res, () => {
73+
expect(fallback.emit).to.have.been.calledOnce;
74+
expect(fallback.emit).to.have.been.calledWith('login', res.data, { provider: 'rest', req, res });
75+
req.app.emit = sinon.spy();
76+
done();
77+
});
78+
});
6679
});
6780

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

0 commit comments

Comments
 (0)