# Failing to redirect to a custom returnTo

**URL:** <https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006>\
**Category:** Get Help\
**Tags:** new-universal-login-experience, login-experience\
**Created:** [April 15, 2024, 12:01pm UTC](https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006 "2024-04-15T12:01:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![possad](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/possad/32/56410_2.png) [@possad](https://community.auth0.com/u/possad)\
**Post date:** [April 15, 2024, 12:01pm UTC](https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006/1 "2024-04-15T12:01:48Z")

</div>

Hi

Following the documentation and a few suggestions here, I’ve tried to modify the returnTo parameter to change the redirected URL after authorisation but I’m not able to do so.

After following the simple guide on setting auth0 in nextjs, I’m able to go to /login, click on a simple button that calls /api/auth/login and manage to authenticate. After authentication, I’m redirected to / URL.

To change that behaviour, I’ve changed the handleAuth from

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

export const GET = handleAuth();

```

to

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

export default handleAuth({
  async login(req, res) {
    await handleLogin(req, res, {
      returnTo: "/custom",
    });
  },
});

```

But now, when I click on the button in /login page, I get a 405 method not allowed. I see that in the browser URL it’s trying to redirect to localhost:/api/auth/login

If I change back he handleauth then everything works again.

What am I doing wrong?

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 15, 2024, 10:25pm UTC](https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006/3 "2024-04-15T22:25:29Z")

</div>

Hi @possad,

Welcome to the Auth0 Community!

After reviewing your code snippet, I noticed that you are passing an incomplete URL for the `returnTo` parameter.

In this situation, please make sure to pass the full URL that you want the user to redirect to after authentication.

For example:

```auto
export default handleAuth({
  async login(req, res) {
    await handleLogin(req, res, {
      returnTo: "https://www.yourapp.com/callback",
    });
  },
});

```

Let me know how this goes for you.

Cheers,  
Rueben

---

<div class="post-metadata">

**Author:** ![possad](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/possad/32/56410_2.png) [@possad](https://community.auth0.com/u/possad)\
**Post date:** [April 16, 2024, 12:02am UTC](https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006/4 "2024-04-16T00:02:14Z")

</div>

That doesn’t solve the problem, unfortunately.

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 16, 2024, 10:41pm UTC](https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006/5 "2024-04-16T22:41:29Z")

</div>

Hi @possad,

Can you provide more details about what happened after trying to pass the complete URL to the `returnTo` parameter?

---

<div class="post-metadata">

**Author:** ![possad](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/possad/32/56410_2.png) [@possad](https://community.auth0.com/u/possad)\
**Post date:** [April 17, 2024, 9:56am UTC](https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006/6 "2024-04-17T09:56:41Z")

</div>

Nothing changes.

Here is what I have in my application setting:

 ![Screenshot 2024-04-17 at 10.59.24](https://global.discourse-cdn.com/auth0/original/3X/c/d/cde2cfe483c11d91291e5731c8881fe6e4b7a09a.png)

When I have

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

export const GET = handleAuth();

```

After I click on login button, it redirects correctly to auth0 website letting the user to log in/sign up. After a successful login, it redirect back to localhost:3000/ but I want it to redirect to localhost:3000/main

So I when I change it to:

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

export default handleAuth({
  async login(req, res) {
    await handleLogin(req, res, {
      returnTo: "http://localhost:3000/main",
    });
  },
});

```

After clicking on login, the browser goes to [http://localhost:3000/api/auth/login](http://localhost:3000/api/auth/login) and I get a 405!

 ![Screenshot 2024-04-17 at 11.01.51](https://global.discourse-cdn.com/auth0/original/3X/f/9/f9b36db2147375b4fe5036b211212c864f6be46b.png)

Here is also my login button

 ![Screenshot 2024-04-17 at 10.56.24](https://global.discourse-cdn.com/auth0/original/3X/a/e/ae5af5a3b53256e12e401b9a6f9fbb3ad7adc25d.png)

---

<div class="post-metadata">

**Author:** ![rueben.tiow](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/rueben.tiow/32/11646_2.png) [@rueben.tiow](https://community.auth0.com/u/rueben.tiow)\
**Post date:** [April 29, 2024, 4:42pm UTC](https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006/7 "2024-04-29T16:42:37Z")

</div>

Hi @possad,

Thanks for your update.

I see that your **Allowed Callback URLs** do not have “[https://localhost:3000/main](https://localhost:3000/main)” as a callback URL for your app.

If you want to redirect your users to “[https://localhost:3000/main](https://localhost:3000/main)”, you must include it as a callback URL and make sure that it’s referenced in your `returnTo` parameter.

Furthermore, the HTTP `405` error code you experienced indicates that you are trying to use an HTTP method that the server doesn’t support. In this case, please make sure you are performing a GET request to your login endpoint (`/api/auth/login`).

Keep me posted on how this goes for you.

Thanks,  
Rueben

---

<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:** [May 13, 2024, 4:43pm UTC](https://community.auth0.com/t/failing-to-redirect-to-a-custom-returnto/132006/8 "2024-05-13T16:43:26Z")

</div>

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