Skip to content

Commit

Permalink
Merge pull request #95 from auth0/fix-returnTo
Browse files Browse the repository at this point in the history
Fix returnTo on Login
  • Loading branch information
davidpatrick authored May 4, 2020
2 parents 172ee8c + 67daff1 commit dda2d3f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
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

0 comments on commit dda2d3f

Please sign in to comment.