Nextjs-auth0: Redirect based on the session

Created on 6 Jul 2020  路  3Comments  路  Source: auth0/nextjs-auth0

Please do not report security vulnerabilities here. The Responsible Disclosure Program details the procedure for disclosing security issues.

Thank you in advance for helping us to improve this library! Your attention to detail here is greatly appreciated and will help us respond as quickly as possible. For general support or usage questions, use the Auth0 Community or Auth0 Support. Finally, to avoid duplicates, please search existing Issues before submitting one here.

By submitting an Issue to this repository, you agree to the terms within the Auth0 Code of Conduct.

Describe the problem you'd like to have solved

The problem I would like to have solved is function to be able to redirect to a different route inside onUserLoaded function in handleCallback method. onUserLoaded is definitely useful for doing some database transactions before redirection but currently we cannot perform a redirection to different routes depending on the session. If we wanted to perform a progressive profiling but if some of the steps are absolutely required after signup, only users who have not yet filled those information should be redirected to the form whereas users who have fulfilled the requirements should be redirected to the home page.

Describe the ideal solution

A clear and concise description of what you want to happen.

I think it would be most ideal if we can include redirectTo: '/route'/ in the session return statement of onUserLoaded method.

Alternatives and current work-arounds

A clear and concise description of any alternatives you've considered or any work-arounds that are currently in place.

current work around would be to check the session every time in the client and redirect user to profiling page.

Additional context

Add any other context or screenshots about the feature request here.

enhancement

Most helpful comment

+1 on this, one of our integrations requires generating a token for the user during the login callback and then redirecting the user to that URL to log them in. The problem is that we won't know the URL to redirect to until after onUserLoaded has ran. I've locally patched the package to return extra options from onUserLoaded

return {
  session,
  options: {
    redirectTo: claimUrl,
  },
};

and patched handlers/callback

let callbackOptions;
if (options && options.onUserLoaded) {
    // session = yield options.onUserLoaded(req, res, session, decodedState);
    const { session: _session, options: _options } = yield options.onUserLoaded(req, res, session, decodedState);
    // Return the options here ^^
    session = _session
    callbackOptions = _options

}
// Create the session.
yield sessionStore.save(req, res, session);
// Redirect to the homepage or custom url.
const redirectTo = (callbackOptions && callbackOptions.redirectTo) || (options && options.redirectTo) || decodedState.redirectTo || '/';
// Then use them if they exist here      ^^^^^^^^^^^^^^^^^^^^^^^^

res.writeHead(302, {
    Location: redirectTo
});
res.end();

All 3 comments

+1 on this, one of our integrations requires generating a token for the user during the login callback and then redirecting the user to that URL to log them in. The problem is that we won't know the URL to redirect to until after onUserLoaded has ran. I've locally patched the package to return extra options from onUserLoaded

return {
  session,
  options: {
    redirectTo: claimUrl,
  },
};

and patched handlers/callback

let callbackOptions;
if (options && options.onUserLoaded) {
    // session = yield options.onUserLoaded(req, res, session, decodedState);
    const { session: _session, options: _options } = yield options.onUserLoaded(req, res, session, decodedState);
    // Return the options here ^^
    session = _session
    callbackOptions = _options

}
// Create the session.
yield sessionStore.save(req, res, session);
// Redirect to the homepage or custom url.
const redirectTo = (callbackOptions && callbackOptions.redirectTo) || (options && options.redirectTo) || decodedState.redirectTo || '/';
// Then use them if they exist here      ^^^^^^^^^^^^^^^^^^^^^^^^

res.writeHead(302, {
    Location: redirectTo
});
res.end();

Would be nice to have a PR for something like this, @mcky.

This is possible in the Beta using the afterCallback hook

See https://github.com/auth0/nextjs-auth0/pull/202#issuecomment-763588328

Was this page helpful?
0 / 5 - 0 ratings