Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(authentication): Fall back when req.app is not the application … #1185

Merged
merged 1 commit into from
Jan 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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