sidebase / nuxt-auth

Authentication built for Nuxt 3! Easily add authentication via OAuth providers, credentials or Email Magic URLs!
https://auth.sidebase.io
MIT License
1.25k stars 162 forks source link

OAUTH_CALLBACK_ERROR : expected 200 OK, got: 301 Moved Permanently with Custom provider #835

Closed flim closed 1 month ago

flim commented 2 months ago

Environment

- Operating System: Darwin
- Node Version:     v20.12.2
- Nuxt Version:     3.12.4
- CLI Version:      3.12.0
- Nitro Version:    2.9.7
- Package Manager:  yarn@1.22.19
- Builder:          -
- User Config:      runtimeConfig, colorMode, tailwindcss, app, compatibilityDate, ssr, devtools, modules, hooks, auth
- Runtime Modules:  @nuxt/ui@2.18.2, @nuxtjs/google-fonts@3.2.0, @nuxtjs/tailwindcss@6.12.1, @sidebase/nuxt-auth@0.8.1
- Build Modules:    -

Reproduction

nuxt.config.ts

  auth: {
    globalAppMiddleware: true,
    disableServerSideAuth: false,
    provider: {
      type: 'authjs',
      defaultProvider: 'wp',
      addDefaultCallbackUrl: true
    }
  }

server/api/auth[...].ts

{
      id: "wp",
      name: "wp",
      type: "oauth",
      authorization: {
        url: "https://mywp.local/oauth/authorize/",
        params: {
          scope: [ "basic" ]
        }
      },
      token: "https://mywp.local/oauth/token",
      userinfo: "https://mywp.local/oauth/me",
      clientId: "veDKK7K9vRS9D87S5kqcc3tfIwezuD5XqHmqaQbF",
      checks: ['state'],
      clientSecret: "1yvJZ4SGlqWDGM6ZhpS1D6e4fc6k9mX8JJ5vmruxj",
      profile(profile) {
        return {
          ...profile
        }
      },
    }

Describe the bug

I have a nuxt application that I would like to authenticate using oauth to my wordpress oauth Server. Im using the authorization code grant type :

For this, I created a custom provider but I constantly get a OAuthCallbackError. But I can't get more info about the real cause of this problem.

Additional context

Here is the callback request:

https://app.mywp.local/api/auth/callback/wp?code=exhwgu0nueado5lvc8srgw1zscv3fsybwrkuetah&state=5abthZSJDSAOxagJwnYu3sBYQnIk3Nk5kERBrlJ7uzs&iframe=break

Logs

nuxt-1  |  ERROR  [next-auth][error][OAUTH_CALLBACK_ERROR] 
nuxt-1  | https://next-auth.js.org/errors#oauth_callback_error expected 200 OK, got: 301 Moved Permanently { error:
nuxt-1  |    { OPError: expected 200 OK, got: 301 Moved Permanently
nuxt-1  |        at processResponse (/srv/app/node_modules/openid-client/lib/helpers/process_response.js:41:11)
nuxt-1  |        at Client.userinfo (/srv/app/node_modules/openid-client/lib/client.js:1264:18)
nuxt-1  |        at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
nuxt-1  |        at async oAuthCallback (/srv/app/node_modules/next-auth/core/lib/oauth/callback.js:131:17)
nuxt-1  |        at async Object.callback (/srv/app/node_modules/next-auth/core/routes/callback.js:52:11)
nuxt-1  |        at async AuthHandler (/srv/app/node_modules/next-auth/core/index.js:201:28)
nuxt-1  |        at Object.handler (/srv/app/node_modules/@sidebase/nuxt-auth/dist/runtime/server/services/authjs/nuxtAuthHandler.mjs:88:24)
nuxt-1  |        at async file:///srv/app/node_modules/h3/dist/index.mjs:1975:19
nuxt-1  |        at async Object.callAsync (file:///srv/app/node_modules/unctx/dist/index.mjs:72:16)
nuxt-1  |        at async Server.toNodeHandle (file:///srv/app/node_modules/h3/dist/index.mjs:2266:7)
nuxt-1  |      name: 'OAuthCallbackError',
nuxt-1  |      code: undefined },
nuxt-1  |   providerId: 'wp',
nuxt-1  |   message: 'expected 200 OK, got: 301 Moved Permanently' }
flim commented 1 month ago

Ok its WordPress Oauth plugin issue. Had to add trailing slash at the end of the token endpoint « https://mywp.local/oauth/token/« , otherwise it will redirect to the correct endpoint with trailing slash.