Amazon Cognito + next-auth -> checks.state argument is missing
See original GitHub issueProvider type
Cognito
Environment
System:
OS: macOS 12.3.1
CPU: (8) x64 Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
Memory: 2.07 GB / 16.00 GB
Shell: 5.8 - /bin/zsh
Binaries:
Node: 16.13.0 - ~/.nvm/versions/node/v16.13.0/bin/node
Yarn: 1.22.18 - /usr/local/bin/yarn
npm: 8.1.0 - ~/.nvm/versions/node/v16.13.0/bin/npm
Browsers:
Chrome: 101.0.4951.64
Firefox: 100.0
Safari: 15.4
npmPackages:
next: latest => 12.1.2
next-auth: 4.0.6 => 4.0.5
react: ^17.0.2 => 17.0.2
Reproduction URL
https://github.com/nextauthjs/next-auth-example
Describe the issue
I’m running an issue while creating a new user with Cognito, after the verification code, the website crashed with the following error screen.
2022-05-16T20:08:28.437Z 5d144ce4-3a52-421c-80ec-89ebcc694439 ERROR [next-auth][error][OAUTH_CALLBACK_ERROR]
https://next-auth.js.org/errors#oauth_callback_error checks.state argument is missing {
error: {
message: 'checks.state argument is missing',
stack: 'TypeError: checks.state argument is missing\n' +
' at Client.callback (/var/task/chunks/117.js:12099:13)\n' +
' at oAuthCallback (/var/task/chunks/117.js:7600:29)\n' +
' at processTicksAndRejections (internal/process/task_queues.js:95:5)\n' +
' at async Object.callback (/var/task/chunks/117.js:8562:11)\n' +
' at async NextAuthHandler (/var/task/chunks/117.js:6424:28)\n' +
' at async NextAuthNextHandler (/var/task/chunks/117.js:9841:19)\n' +
' at async /var/task/chunks/117.js:9877:32\n' +
' at async Object.apiResolver (/var/task/chunks/458.js:6620:9)\n' +
' at async Module.<anonymous> (/var/task/chunks/458.js:5552:13)',
name: 'TypeError'
},
providerId: 'cognito',
message: 'checks.state argument is missing'
}
2022-05-16T20:08:28.439Z 5d144ce4-3a52-421c-80ec-89ebcc694439 ERROR [next-auth][error][CALLBACK_OAUTH_ERROR]
https://next-auth.js.org/errors#callback_oauth_error checks.state argument is missing TypeError: checks.state argument is missing
at Client.callback (/var/task/chunks/117.js:12099:13)
at oAuthCallback (/var/task/chunks/117.js:7600:29)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async Object.callback (/var/task/chunks/117.js:8562:11)
at async NextAuthHandler (/var/task/chunks/117.js:6424:28)
at async NextAuthNextHandler (/var/task/chunks/117.js:9841:19)
at async /var/task/chunks/117.js:9877:32
at async Object.apiResolver (/var/task/chunks/458.js:6620:9)
at async Module.<anonymous> (/var/task/chunks/458.js:5552:13) {
name: 'OAuthCallbackError',
code: undefined
Here is the pages/api/auth/[...nextauth].js
file:
import NextAuth from "next-auth";
import CognitoProvider from "next-auth/providers/cognito";
export default NextAuth({
providers: [
CognitoProvider({
clientId: process.env.COGNITO_CLIENT_ID,
clientSecret: process.env.COGNITO_CLIENT_SECRET,
issuer: process.env.COGNITO_ISSUER,
idToken: true,
}),
],
callbacks: {
// By default NextAuth will redirect to an url of the same hostname
// this callback allows to change that behavior and specify a custom url
redirect({ url }) {
return url;
},
async session({ session, token }) {
// Send properties to the client, like an access_token from a provider.
session.customerID = token.sub
return session
}
},
secret: process.env.SECRET,
debug: process.env.NODE_ENV === "development" ? true : false
});
How to reproduce
Sign up using Cognito and next-auth successfully.
Expected behavior
Register to Cognito without the following error message.
Issue Analytics
- State:
- Created a year ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
Checks.state argument is missing in next.js - Auth0 Community
I am getting an error when trying to login using auth0 while in production. This seems as a common problem other users have...
Read more >Amazon Cognito | NextAuth.js
You need to select your AWS region to go the the Cognito dashboard. Options. The Amazon Cognito Provider comes with a set of...
Read more >Cognito logout does not work as documented - Stack Overflow
This redirect happens whenever logout_uri parameter doesn't match exactly what's listed among Sign out URL(s) in AWS Cognito User Pools App ...
Read more >Managing error responses - Amazon Cognito
You can simulate a generic response with user name and password authentication if you are using verification-based alias attributes, and the immutable user...
Read more >get session next auth | The AI Search Engine You Control
How can i get next-auth session in nextjs 12 middlware ... a loading state on the initial check and every page transition afterward...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, I have answered my own question. Changing my Cloudfront config made it work.
@ThangHuuVu Is there any place in the documentation where all Cookies used by NextAuth are listed? It’s always trial-and-error to see if it works and would be great if there just was a list of cookie names you have to whitelist.
@z0ph I set up a dedicated cache behaviour for
api/*
allowing all HTTP methods and the following cookies.I’ve tried different Cloudfront settings without success yet.
What I see so far:
You can see in the screenshot below that the cookie is set starting with the highlighted: Cognito line
Ok, writing these lines below makes me realize my mistake. I was not using first the app URL to allow it to set the state information, but instead directly a sign-up URL from Cognito with a false/previous state id. My mistake.
It’s now solved, thanks 👍🏼