# Redirecting back to starting page dynamically with nextjs-auth0

**URL:** <https://community.auth0.com/t/redirecting-back-to-starting-page-dynamically-with-nextjs-auth0/71460>\
**Category:** Get Help\
**Tags:** redirect, nextjs\
**Created:** [October 17, 2021, 3:27pm UTC](https://community.auth0.com/t/redirecting-back-to-starting-page-dynamically-with-nextjs-auth0/71460 "2021-10-17T15:27:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![prnkdslv](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/prnkdslv/32/13390_2.png) [@prnkdslv](https://community.auth0.com/u/prnkdslv)\
**Post date:** [October 17, 2021, 3:27pm UTC](https://community.auth0.com/t/redirecting-back-to-starting-page-dynamically-with-nextjs-auth0/71460/1 "2021-10-17T15:27:19Z")

</div>

Hello, I’m trying to figure out how to redirect users dynamically after login based on the page they accessed the login form from.

In the app I’m currently trying to build, users can log in from “/” which is the home page, and from the navbar element in “/browse”. However, after logging in, it always redirects back to “/”, while I would like to redirect users to “/browse” or "/browse/[id] if that is where they began the login process from.

I’ve tried using [https://community.auth0.com/t/redirecting-to-another-page-other-than-using-nextjs-auth0/66920](https://community.auth0.com/t/redirecting-to-another-page-other-than-using-nextjs-auth0/66920) as a guide but this method only allows me to redirect to a pre-defined route. I would like to know how I could make the redirect URL dynamic.

SDK: nextjs-auth0  
Version: “^1.6.1”  
“next”: “11.1.2”

**Edit** : I’ve managed to find a solution for now by digging in to the req object and setting the returnTo value to “referer”.

```auto
import { handleAuth, handleLogin } from '@auth0/nextjs-auth0';

const getLoginState = (req, loginOptions) => {
    return {
        returnTo: req.headers.referer
    };
  };

export default handleAuth({
    async login(req, res) {
        try {
          await handleLogin(req, res, { getLoginState });
        } catch (err) {
            res.status(err.status ?? 500).end(err.message)
        }
      }
  });

```

I’m not seeing any obvious problems so far but I’m not entirely sure if this method has any drawbacks, so I would appreciate any feedback.

---

<div class="post-metadata">

**Author:** ![dtuckernet](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dtuckernet/32/44076_2.png) [@dtuckernet](https://community.auth0.com/u/dtuckernet)\
**Post date:** [March 4, 2023, 5:47pm UTC](https://community.auth0.com/t/redirecting-back-to-starting-page-dynamically-with-nextjs-auth0/71460/2 "2023-03-04T17:47:27Z")

</div>

I believe from what I’m seeing in the code, if you add a `returnTo` value to the login link, that will be respected once the user logs in.

```auto
export default function SamplePage({ loggedIn }: Props) {

  const returnLink = `/api/auth/login?returnTo=${encodeURIComponent('/sample')}`;

  return (
    <>
      <h1>Server-side rendered page</h1>
      { loggedIn && 
      <p>
        This page is server-side rendered. The user is logged in.
      </p> }
      { !loggedIn &&
      <p>
        Please <a href={returnLink}>Login</a>.
      </p>
      }
    </>
  );
}

```

---

<div class="post-metadata">

**Author:** ![konrad.sopala](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/konrad.sopala/32/13516_2.png) [@konrad.sopala](https://community.auth0.com/u/konrad.sopala)\
**Post date:** [March 6, 2023, 9:11am UTC](https://community.auth0.com/t/redirecting-back-to-starting-page-dynamically-with-nextjs-auth0/71460/3 "2023-03-06T09:11:40Z")

</div>

Thanks for sharing that with the rest of community!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [March 20, 2023, 9:12am UTC](https://community.auth0.com/t/redirecting-back-to-starting-page-dynamically-with-nextjs-auth0/71460/4 "2023-03-20T09:12:33Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
