nuxt-community / auth-module

Zero-boilerplate authentication support for Nuxt 2
https://auth.nuxtjs.org
MIT License
1.93k stars 924 forks source link

Redirect fail due to removal of trailing slash #1429

Open janhenkes opened 2 years ago

janhenkes commented 2 years ago

Version

module: nuxt:

Nuxt configuration

mode:

Nuxt configuration

  router: {
      trailingSlash: true,
  }, 
  auth: {
     strategies: {
            social: {
                scheme: "oauth2",
                endpoints: {
                    authorization: process.env.API_ADMIN_AJAX_URL + 'oauth/authorize',
                    token: process.env.API_ADMIN_AJAX_URL + 'oauth/token',
                    userInfo: false,
                    logout: false,
                },
                token: {
                    property: 'access_token',
                    type: 'Bearer',
                    maxAge: 1800
                },
                refreshToken: {
                    property: 'refresh_token',
                    maxAge: 60 * 60 * 24
                },
                responseType: 'code',
                grantType: 'authorization_code',
                clientId: process.env.WP_OAUTH_CLIENT_ID,
            }
        },
        redirect: {
            login: '/login/',
            logout: false,
            home: '/',
            callback: '/callback/',
        },
        rewriteRedirects: true,
        fullPathRedirect: true,
        localStorage: false,
        cookie: {
            options: {
                secure: process.env.ENV !== 'development'
            }
        },
  }

Additional information

We have the trailingSlash option in Nuxt set to true (https://nuxtjs.org/docs/configuration-glossary/configuration-router#trailingslash). We have searched through the code and found the normalizePath function which removes the trailing slash. This should not happen if Nuxt is configured to force trailing slashes. https://github.com/nuxt-community/auth-module/blob/5.0.0-dev/src/utils/index.ts#L67

What is expected?

I expect that after authorization the user is redirect tot /callback/?code=XXX.

What is actually happening?

The user is redirected to /callback?code=XXX.

Checklist

plusone-masaki commented 2 years ago

Try it as a temporary workaround this.

// middleware/trailing-slash.js
export default ({ route, redirect }) => {
  if (route.path.slice(-1) !== '/') redirect(301, route.path + '/')
}
// nuxt.config.js
{
  // ...
  router: {
    trailingSlash: true,
    middleware: ['trailing-slash'],
  },
  // ...
}