How to set `returnTo` for SSR for a dynamic route?

Please include the following information in your post:

  • Which SDK this is regarding: @auth0/nextjs-auth0
  • SDK Version: 1.7.0
  • Platform Version: Node v17.1.0 (on macOS 12.2)
  • Code Snippets/Error Messages/Supporting Details/Screenshots:

Is this a feature request or bug report? No.

I am following the instructions referenced here for an SSR protected page.

I am using this snippet with a fresh/clean install of NextJS (version 12.0.10):

export const getServerSideProps = withPageAuthRequired({
  async getServerSideProps(ctx) {
    console.log("query", ctx.query);
    // This was in the documentation but doesn't seem to be needed:
    // https://auth0.github.io/nextjs-auth0/modules/helpers_with_page_auth_required.html#withpageauthrequiredoptions
    //
    // const session = getSession(ctx.req, ctx.res);
    return { props: { customProp: "bar" } };
  },
});

I am trying to link to a route /test/[id].tsx

This functions correctly when I omit returnTo but it changes the URL to: http://localhost:3000/test/something?id=something instead of http://localhost:3000/test/something

If I set the returnTo value to /test/something the URL is fine.

Is there a way to set the returnTo dynamically? Or am I doing something incorrect? Or is it just something I will have to live with?

Thanks a lot!
Marc

Based on the README for nextjs-auth0 repository, it seems like it’s possible to do something like this, although I’m not sure it is recommended (just to get a prettier URL):

const getReturnTo = (
  ctx: GetServerSidePropsContext<ParsedUrlQuery, PreviewData>
) => {
  // Lots of better/different ways to do this
  const [path] = ctx.resolvedUrl.split("?");

  return path;
};

export const getServerSideProps: GetServerSideProps = (ctx) => {
  const returnTo = getReturnTo(ctx);

  return withPageAuthRequired<{ customProp: string }>({
    returnTo,
    async getServerSideProps(ctx) {
      return { props: { customProp: "bar" } };
    },
  })(ctx);
};

Hey there marclemagne!

I guess in this case it will be worth discussing with the SDK maintainer through a GitHub issue so they can share their thoughts. Would you be able to create one and share it here so we can ping them? Thanks!

Created this issue. Thanks.

Perfect! I’ll ping them in a minute!