nextauthjs / next-auth

Authentication for the Web.
https://authjs.dev
ISC License
25.04k stars 3.54k forks source link

Cognito get error redirect_mismatch on sign in #12125

Closed zluo01 closed 3 weeks ago

zluo01 commented 1 month ago

Provider type

Cognito

Environment

  System:
    OS: Linux 6.11 Fedora Linux 40 (KDE Plasma)
    CPU: (24) x64 AMD Ryzen 9 5900X 12-Core Processor
    Memory: 49.20 GB / 62.70 GB
    Container: Yes
    Shell: 5.2.26 - /bin/bash
  Binaries:
    Node: 20.16.0 - ~/.nvm/versions/node/v20.16.0/bin/node
    npm: 10.8.1 - ~/.nvm/versions/node/v20.16.0/bin/npm
  Browsers:
    Chrome: 130.0.6723.69
  npmPackages:
    @auth/unstorage-adapter: ^2.0.0 => 2.7.2 
    next: 14.2.16 => 14.2.16 
    next-auth: beta => 5.0.0-beta.2

Reproduction URL

https://github.com/zluo01/next-auth-cognito

Describe the issue

Login with cognito will get following error error=redirect_mismatch from the cognito url. Cognito page shows An error was encountered with the requested page..

Only change I have made is create .env.local and clean up auth.js with one provider compare to the original next-auth-example.

Callback on cognito end is http://localhost:3000/api/auth/callback/cognito as suggested in https://authjs.dev/getting-started/authentication/oauth.

How to reproduce

Fill in the blank in .env.local then simply try to login with cognito.

Expected behavior

Successfully authenticate with cognito like V4.

asionesjia commented 4 weeks ago

See here, this can solve the error of authjs signin redirect failure in server form action.

export const signInWithGithubAction = validatedAction(
    signInWithGithubSchema,
        async (data, formData) => {
        const { redirect: redirectParma } = data
        const res = await signIn('github', {
            redirect: !!redirectParma,
            redirectTo: redirectParma ? redirectParma : undefined,
        })
    redirect(res)
  },
)

Key Points: redirect(res) at the fuction last.

fucking authjs , get up and work , I spent 3 hours on this,