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

Add support for customizing returnTo in middleware #1342

Merged
merged 4 commits into from
Aug 3, 2023

Conversation

adamjmcgrath
Copy link
Contributor

📋 Changes

Adding support for customizing the returnTo in middleware:

import { withMiddlewareAuthRequired, getSession } from '@auth0/nextjs-auth0/edge';

// Existing
export default withMiddlewareAuthRequired(async function middleware(req) {
  return NextResponse.next();
});

// To provide a custom `returnTo` url to login:
export default withMiddlewareAuthRequired({
  returnTo: '/foo',
  async middleware(req) { return NextResponse.next(); } // Custom middleware can be here
});

// You can also provide a method for `returnTo` that takes the req as an argument.
// Useful for adding i18n or basePath
export default withMiddlewareAuthRequired({
  returnTo(req) { return `${req.nextURL.basePath}${req.nextURL.pathname}`};
});

📎 References

fixes #1322 #1329

@adamjmcgrath adamjmcgrath requested a review from a team as a code owner August 3, 2023 12:03
@vercel
Copy link

vercel bot commented Aug 3, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Ignored Deployment
Name Status Preview Comments Updated (UTC)
nextjs-auth0 ⬜️ Ignored (Inspect) Visit Preview Aug 3, 2023 0:16am

const { pathname, origin, search } = req.nextUrl;
let returnTo = `${pathname}${search}`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we include the basePath here? As requested in #1329

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or maybe in another PR.

Copy link
Contributor Author

@adamjmcgrath adamjmcgrath Aug 3, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason I didn't add it was because I was worried it might be a breaking change.

If you were to work around this limitation currently, you might write something that prepended the base path to the returnTo in a custom login or callback handler and I didn't want to break that usage.

@vercel
Copy link

vercel bot commented Aug 3, 2023

Deployment failed with the following error:

Please accept Vercel's request to update the GitHub App permissions. Otherwise GitHub will not permit Vercel to create or access a new Git repository for you.

@adamjmcgrath adamjmcgrath merged commit a57193e into main Aug 3, 2023
2 checks passed
@adamjmcgrath adamjmcgrath deleted the customize-returnto-in-mw branch August 3, 2023 15:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support returnTo in withMiddlewareAuthRequiredFactory
2 participants