Skip to content

Commit

Permalink
Add redirectTo support (#81)
Browse files Browse the repository at this point in the history
* Add redirectTo support

* Always set a0:redirectTo
  • Loading branch information
nicogarcia authored Mar 31, 2020
1 parent 7d7932a commit d34b319
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ You can optionally send extra parameters to Auth0 to influence the transaction,
- Filling in the user's email address
- Exposing information to the custom login page (eg: to show the signup tab)
- Using a custom `state`
- Redirecting the user to a `redirectTo` url after the transaction is finished

```js
import auth0 from '../../utils/auth0';
Expand All @@ -148,7 +149,8 @@ export default async function login(req, res) {
scope: 'some other scope',
state: 'a custom state',
foo: 'bar'
}
},
redirectTo: '/custom-url'
});
} catch (error) {
console.error(error);
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ export default function callbackHandler(
// Create the session.
await sessionStore.save(req, res, session);

// Redirect to the homepage.
const redirectTo = (options && options.redirectTo) || '/';
// Redirect to the homepage or custom url.
const redirectTo = (options && options.redirectTo) || cookies['a0:redirectTo'] || '/';
res.writeHead(302, {
Location: redirectTo
});
Expand Down
7 changes: 7 additions & 0 deletions src/handlers/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export interface AuthorizationParameters {

export interface LoginOptions {
authParams?: AuthorizationParameters;
redirectTo?: string;
}

export default function loginHandler(settings: IAuth0Settings, clientProvider: IOidcClientFactory) {
Expand All @@ -51,6 +52,7 @@ export default function loginHandler(settings: IAuth0Settings, clientProvider: I

const opt = options || {};
const { state = base64url(randomBytes(48)), ...authParams } = (opt && opt.authParams) || {};
const { redirectTo } = opt;

// Create the authorization url.
const client = await clientProvider();
Expand All @@ -70,6 +72,11 @@ export default function loginHandler(settings: IAuth0Settings, clientProvider: I
name: 'a0:state',
value: state,
maxAge: 60 * 60
},
{
name: 'a0:redirectTo',
value: redirectTo || '/',
maxAge: 60 * 60
}
]);

Expand Down
7 changes: 6 additions & 1 deletion tests/handlers/callback.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ describe('callback handler', () => {
url: `${httpServer.getUrl()}?state=foo&code=bar`,
followRedirect: false,
headers: {
cookie: 'a0:state=foo;'
cookie: 'a0:state=foo;a0:redirectTo=/custom-url;'
}
});

Expand Down Expand Up @@ -260,6 +260,11 @@ describe('callback handler', () => {
expect(cookie['Max-Age']).toBe('3600');
expect(cookie.Expires).toBe(new Date(time.getTime() + 3600 * 1000).toUTCString());
});

test('should redirect to cookie url', async () => {
expect(responseStatus).toBe(302);
expect(responseHeaders.location).toBe('/custom-url');
});
});
});

Expand Down
14 changes: 13 additions & 1 deletion tests/handlers/login.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('login handler', () => {

beforeEach(done => {
discovery(withoutApi);
loginOptions = null;
loginOptions = { redirectTo: '/custom-url' };
loginHandler = login(withoutApi, getClient(withoutApi));
httpServer = new HttpServer((req, res) => loginHandler(req, res, loginOptions));
httpServer.start(done);
Expand All @@ -37,6 +37,18 @@ describe('login handler', () => {
expect(state).toBeTruthy();
});

test('should create a redirectTo cookie', async () => {
const { headers } = await getAsync({
url: httpServer.getUrl(),
followRedirect: false
});

const state = parse(headers['set-cookie'][0]);
const redirectTo = parse(headers['set-cookie'][1]);
expect(state).toBeTruthy();
expect(redirectTo['a0:redirectTo']).toEqual('/custom-url');
});

test('should redirect to the identity provider', async () => {
const { statusCode, headers } = await getAsync({
url: httpServer.getUrl(),
Expand Down

0 comments on commit d34b319

Please sign in to comment.