# NextJS on Auth0 fails with "Missing state cookie from login request"

**URL:** <https://community.auth0.com/t/nextjs-on-auth0-fails-with-missing-state-cookie-from-login-request/102859>\
**Category:** Get Help\
**Tags:** authentication-sessions, sessions\
**Created:** [March 19, 2023, 2:03am UTC](https://community.auth0.com/t/nextjs-on-auth0-fails-with-missing-state-cookie-from-login-request/102859 "2023-03-19T02:03:48Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cameronlund](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@cameronlund](https://community.auth0.com/u/cameronlund)\
**Post date:** [March 19, 2023, 2:03am UTC](https://community.auth0.com/t/nextjs-on-auth0-fails-with-missing-state-cookie-from-login-request/102859/1 "2023-03-19T02:03:48Z")

</div>

I am new to Auth0 and am looking to add authentication to a NextJS application being hosted on AWS Amplify. I followed the guide [here](https://auth0.com/docs/quickstart/webapp/nextjs/01-login).

**The issue**

Everything works great locally. When running on Amplify, I get the following error:

```auto
CallbackHandlerError: Callback handler failed. CAUSE: Missing state cookie from login request (check login URL, callback URL and cookie config).

```

If anyone has ideas on what I should do to debug this further, it would be greatly appreciated. I am currently out of ideas.

**Context**

This happens after I hit “log in,” sign in through the UI and get returned to my site.

In the logs from Amplify, before the error, I see the log, which appears to suggest the cookie is being set:

```auto
[x-amplify-log][INFO] app response status_code=302 Found headers=set-cookie: redacted (134), set-cookie: redacted (120), set-cookie: redacted (170), set-cookie: redacted (156), set-cookie: redacted (142), set-cookie: redacted (128), location: redacted (454), date: Sun, 19 Mar 2023 01:35:05 GMT, connection: keep-alive, keep-alive: redacted (9), transfer-encoding: chunked, x-amplify-internal-request-id: 48xxx

```

My amplify domain is “[https://staging.xxx.amplifyapp.com/](https://staging.xxx.amplifyapp.com/)”, and I have a callback URL configured for “[https://staging.xxx.amplifyapp.com/api/auth/callback](https://staging.xxx.amplifyapp.com/api/auth/callback)”, so that should be correct. That is in the same format as my localhost callback, which works correctly.

Following other threads I found, I set “AUTH0\_COOKIE\_SAME\_SITE” env var to “none” to no avail.

In Chrome dev tools, I can see that there are no cookies set when I am returned to my website.

---

<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:** [August 30, 2024, 2:17am UTC](https://community.auth0.com/t/nextjs-on-auth0-fails-with-missing-state-cookie-from-login-request/102859/3 "2024-08-30T02:17:28Z")

</div>

Hi @cameronlund,

The “Missing state cookie from login request” error happens because the Next.js SDK can’t find any query parameters in the callback request (`/api/auth/callback`). This usually occurs when middleware between the app and the browser removes these parameters. See [this knowledge solution](https://community.auth0.com/t/next-js-400-error-callback-handler-failed-cause-missing-state-parameter-in-authorization-response/130579), which mentions this.

With that, I’d suggest reviewing your deployment configurations for any middleware or network components that may interfere with the callback URL’s state parameter. Specifically, make sure to preserve the state parameter in the query string of the callback URL (e.g., `/api/auth/callback?state=XXXXX`).

Here’s what you can do:

1. **Review Middleware Configurations:** Check for any reverse proxies, load balancers, or security tools in your deployment setup that might strip off URL parameters.
2. **Inspect Cookie Configurations:** Ensure that cookies are correctly set and sent during the authentication process. Misconfigurations here could prevent the state cookie from being present. You might need to add the `AUTH0_TRANSACTION_COOKIE_SAME_SITE=none` in the auth0-config.
3. **Verify Callback URLs:** Double-check that the login URL, callback URL, and cookie configurations are set correctly in your Auth0 and Next.js settings.

References:

- [https://github.com/auth0/nextjs-auth0/issues/1392](https://github.com/auth0/nextjs-auth0/issues/1392)

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:** [September 13, 2024, 2:19am UTC](https://community.auth0.com/t/nextjs-on-auth0-fails-with-missing-state-cookie-from-login-request/102859/6 "2024-09-13T02:19:19Z")

</div>

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