A community member has associated this post with a similar question:
How to fix invalid_grant (AADSTS54005) error with next-auth?
Only moderators can edit this content.
How to fix invalid_grant (AADSTS54005) error with next-auth?
I am experiencing an issue with Azure AD login in my Next.js 14 application using next-auth
. The error message I receive is:
| [next-auth][error][OAUTH_CALLBACK_ERROR]
| https://next-auth.js.org/errors#oauth_callback_error invalid_grant (AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token. Trace ID: 436a2a67-5721-45ab-b57f-96cd6f700e00 Correlation ID: bdd8a262-3185-4836-afb1-613da56f8f36 Timestamp: 2024-07-29 02:15:08Z) {
| error: i: invalid_grant (AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token. Trace ID: 436a2a67-5721-45ab-b57f-96cd6f700e00 Correlation ID: bdd8a262-3185-4836-afb1-613da56f8f36 Timestamp: 2024-07-29 02:15:08Z)
| at e.exports (/home/ubuntu/projects/myProjectName/.next/server/app/api/auth/[...nextauth]/route.js:52:35672)
| at Y.grant (/home/ubuntu/projects/myProjectName/.next/server/app/api/auth/[...nextauth]/route.js:52:15036)
| at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
| at async Y.callback (/home/ubuntu/projects/myProjectName/.next/server/app/api/auth/[...nextauth]/route.js:52:2813)
| at async l (/home/ubuntu/projects/myProjectName/.next/server/app/api/auth/[...nextauth]/route.js:19:145828)
| at async Object.c (/home/ubuntu/projects/myProjectName/.next/server/app/api/auth/[...nextauth]/route.js:43:780)
| at async _ (/home/ubuntu/projects/myProjectName/.next/server/app/api/auth/[...nextauth]/route.js:19:128167)
| at async a (/home/ubuntu/projects/myProjectName/.next/server/app/api/auth/[...nextauth]/route.js:43:19595)
| at async e.length.t (/home/ubuntu/projects/myProjectName/.next/server/app/api/auth/[...nextauth]/route.js:43:21005)
| at async /home/ubuntu/projects/myProjectName/node_modules/next/dist/compiled/next-server/app-route.runtime.prod.js:6:42484 {
| name: 'OAuthCallbackError',
| code: undefined
| },
| providerId: 'azure-ad',
| message: 'invalid_grant (AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token. Trace ID: 436a2a67-5721-45ab-b57f-96cd6f700e00 Correlation ID: bdd8a262-3185-4836-afb1-613da56f8f36 Timestamp: 2024-07-29 02:15:08Z)'
| }
This issue only occurs for specific users on the deployed page, while the login process works correctly in the local development environment.
I suspect the problem may be related to the authorization code process. Could you help me understand why this error occurs only on the deployed page and for specific users? Any guidance on resolving this issue would be greatly appreciated.
Thank you!