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 returnTo on Login #95

Merged
merged 2 commits into from
May 4, 2020
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
11 changes: 9 additions & 2 deletions lib/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,17 @@ class ResponseContext {
const config = this._config;
const client = req.openid.client;

// Set default returnTo value, allow passed-in options to override.
// Set default returnTo value, allow passed-in options to override or use originalUrl on GET
let returnTo = this._config.baseURL;
if (options.returnTo) {
returnTo = options.returnTo;
} else if (req.method === 'GET' && req.originalUrl) {
returnTo = req.originalUrl;
}

options = {
returnTo: this._config.baseURL,
authorizationParams: {},
returnTo,
...options
};

Expand Down
6 changes: 6 additions & 0 deletions test/requiresAuth.tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ describe('requiresAuth middleware', function() {
it('should contain a location header to the issuer', function() {
assert.include(response.headers.location, 'https://test.auth0.com');
});
it('should contain a location header with state containing return url', function() {
const state = (new URL(response.headers.location)).searchParams.get('state');
const decoded = Buffer.from(state, 'base64');
const parsed = JSON.parse(decoded);
assert.equal(parsed.returnTo, '/protected');
});
});

describe('when removing the auth middleware', function() {
Expand Down