# How to retrieve the referrer URL in an Auth0 Post Login Action?

**URL:** <https://community.auth0.com/t/how-to-retrieve-the-referrer-url-in-an-auth0-post-login-action/151169>\
**Category:** Get Help\
**Tags:** extensibility, actions-extensibility\
**Created:** [November 7, 2024, 10:45pm UTC](https://community.auth0.com/t/how-to-retrieve-the-referrer-url-in-an-auth0-post-login-action/151169 "2024-11-07T22:45:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![b.neugebauer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/b.neugebauer/32/58893_2.png) [@b.neugebauer](https://community.auth0.com/u/b.neugebauer)\
**Post date:** [November 7, 2024, 10:45pm UTC](https://community.auth0.com/t/how-to-retrieve-the-referrer-url-in-an-auth0-post-login-action/151169/1 "2024-11-07T22:45:10Z")

</div>

Hi, I’m looking to create a post-login action that depends on the URL the user originally visited.

I’m securing a Google AppSheet URL with Auth0, and here’s my use case:

1. User navigates to `https://www.appsheet.com/start/123`
2. The Auth0 login form appears
3. User logs in
4. The `onExecutePostLogin` action is triggered

I’d like to access the original referrer URL (`https://www.appsheet.com/start/123`) within the `onExecutePostLogin` function. However, `event.transaction.redirect_uri` only shows `https://www.appsheet.com/Account/ELC`, not the initial URL.

How can I retrieve the original URL that prompted the Auth0 login screen?

Best,  
Benny

---

<div class="post-metadata">

**Author:** ![marybeth.hunter](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marybeth.hunter/32/61955_2.png) [@marybeth.hunter](https://community.auth0.com/u/marybeth.hunter)\
**Post date:** [November 12, 2024, 6:28pm UTC](https://community.auth0.com/t/how-to-retrieve-the-referrer-url-in-an-auth0-post-login-action/151169/3 "2024-11-12T18:28:04Z")

</div>

Hi @b.neugebauer,

There is not an out-of-the-box way to access the URL the user originally visited in an Action. I did a bit of testing and was able to access the URL in this (somewhat hacky) way:

In my React app, I am initializing Auth0 with the `acr_values` set to `window.location.origin`. Example:

```auto
const providerConfig = {
  domain: config.domain,
  clientId: config.clientId,
  cacheLocation: 'localstorage',
  authorizationParams: {
    redirect_uri: config.callbackURL,
    acr_values: `${window.location.origin}`
  },
};

```

In my Action, I can access `event.transaction.acr_values` and see an array with the initial `window.location.origin` URL. This is the result of console.logging the `event` object in my action:

![Screenshot 2024-11-12 at 12.27.28 PM](https://global.discourse-cdn.com/auth0/original/3X/0/6/06542f475f5040a1ba8046a76b8d56b8604d55ea.png)

Note that the above code is an example and should be tested in a development environment first before adding to production. Additionally, this may vary depending on the SDK you are using. I just used our React SDK as an example.

Please let me know if you have any additional questions or if you run into any issues!

Best,

Mary Beth

---

<div class="post-metadata">

**Author:** ![b.neugebauer](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/b.neugebauer/32/58893_2.png) [@b.neugebauer](https://community.auth0.com/u/b.neugebauer)\
**Post date:** [November 12, 2024, 6:48pm UTC](https://community.auth0.com/t/how-to-retrieve-the-referrer-url-in-an-auth0-post-login-action/151169/4 "2024-11-12T18:48:55Z")

</div>

Hi Mary, thanks for testing!

Unfortunately, the website displaying the login page isn’t under my control, so I can’t add an authorization parameter to it.

I’ve configured my [AppSheet Access using OpenID Connect](https://support.google.com/appsheet/answer/10105083).

When I visit [https://www.appsheet.com/start/123](https://www.appsheet.com/start/123), it redirects to [Login - AppSheet](https://www.appsheet.com/Account/Login), featuring an OpenID Connect button linked to the **Auth endpoint** of my Auth0 application. Although the [Login - AppSheet](https://www.appsheet.com/Account/Login) page includes a “returnUrl” parameter with the original address, this parameter isn’t forwarded to Auth0.

I’m exploring now alternative methods to secure my AppSheet app using Auth0. Do you have any suggestions? I’ve come across middleware like this: [GitHub - auth0/express-openid-connect: An Express.js middleware to protect OpenID Connect web applications.](https://github.com/auth0/express-openid-connect)

Best,  
Benny

---

<div class="post-metadata">

**Author:** ![marybeth.hunter](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/marybeth.hunter/32/61955_2.png) [@marybeth.hunter](https://community.auth0.com/u/marybeth.hunter)\
**Post date:** [November 15, 2024, 9:29pm UTC](https://community.auth0.com/t/how-to-retrieve-the-referrer-url-in-an-auth0-post-login-action/151169/5 "2024-11-15T21:29:15Z")

</div>

Hi @b.neugebauer,

I did some research on this and this is what I found that I think could be helpful. I’m using Next.js as an example here:

1. Modify the `/authorize` URL to Include the `returnUrl`

You need to pass this `returnUrl` parameter to Auth0 by including it in the authorization request. Typically, you can do this by modifying the `authorizationParams` when initiating the login request through your API (e.g., the login endpoint in Next.js).

For example, if you’re using the `@auth0/nextjs-auth0` SDK, you can forward this parameter like so:

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

export default function login(req, res) {
  const returnUrl = req.query.returnUrl || '/'; // Use the returnUrl if available

  handleLogin(req, res, {
    authorizationParams: {
      redirect_uri: process.env.AUTH0_REDIRECT_URI,
      // Pass the returnUrl to Auth0 as a query parameter
      returnTo: returnUrl,
    },
  });
}

```

1. Use `returnTo` Parameter on Auth0

Auth0 supports a `returnTo` parameter that you can pass during the authentication process. This tells Auth0 where to redirect the user after the authentication process is complete.

- In the `handleLogin` function, you pass `returnTo` as part of the `authorizationParams`.
- When the user is redirected back after logging in, Auth0 will forward the user to that URL, preserving the original destination (the AppSheet URL in this case).

1. Callback Handling to Capture the Redirect URL

In the callback handler (where Auth0 sends the user back after authentication), ensure that you process the `returnTo` query parameter properly:

```auto
import { handleCallback } from '@auth0/nextjs-auth0';

export default async function callback(req, res) {
  try {
    // Handles the callback and stores the session
    await handleCallback(req, res);

    // After successful login, redirect the user to the `returnTo` URL
    const { returnTo } = req.query; // Capture the returnTo URL from the query string
    res.redirect(returnTo || '/'); // Fallback to '/' if returnTo is not present
  } catch (error) {
    console.error(error);
    res.status(error.status || 500).end(error.message);
  }
}

```

1. Ensure Your Auth0 Settings are Correct

Make sure your Auth0 application settings allow the `returnTo` URL in your Allowed Callback URLs:

- Go to your Auth0 dashboard
- Navigate to Applications \> Your Application \> Settings.
- Update the Allowed Callback URLs to include the return URL (e.g., `https://your-app.com/callback`), and make sure it aligns with the one that AppSheet sends.

Please let me know how that goes!

Thanks,

Mary Beth

---

<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:** [November 29, 2024, 9:30pm UTC](https://community.auth0.com/t/how-to-retrieve-the-referrer-url-in-an-auth0-post-login-action/151169/6 "2024-11-29T21:30:14Z")

</div>

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