nuxt-community / auth-module

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

v5 Auth module doesn't redirect the user automatically to defined redirect routes #1675

Closed sts-ryan-holton closed 1 year ago

sts-ryan-holton commented 2 years ago

Version

module: 5.0.0-1643791578.532b3d6 nuxt: 2.15.8

Nuxt configuration

Nuxt configuration

// Auth module configuration: https://auth.nuxtjs.org/
auth: {
  redirect: {
    login: '/login/',
    logout: '/login/',
    callback: '/login/',
    home: '/'
  },
  strategies: {
    'laravelSanctum': {
      provider: 'laravel/sanctum',
      url: process.env.MAILER_API_DOMAIN,
      endpoints: {
        login: { url: '/api/login', method: 'post' },
        logout: { url: '/api/account/logout', method: 'post' },
        user: { url: '/api/account', method: 'get', propertyName: 'user' }
      },
    },
  }
},

What is expected?

When using the auth middleware and a user isn't logged in, they should be redirected to the login route specified in the redirect object, other redirect routes such as when a user logs in should also function too.

What is actually happening?

When loading up a route where middleware is set to auth the user isn't being redirected to the specified redirects in the config.

Additional information

I came across this in the issues, I tried downgrading to an earliser version and that doesn't help, it still doesn't redirect, redirects used to happen just fine when my strategy was set to scheme.

Checklist

barnabas-szekeres commented 2 years ago

I'm facing a very similar issue: after success login the $auth not set on SSR side, only on client side. :(

sts-ryan-holton commented 2 years ago

@barnabas-szekeres Hopefully more people are experiencing this same error, do you have any nice workarounds?

barnabas-szekeres commented 2 years ago

@sts-ryan-holton I guess the issue connected to the allowed SESSION_DOMAINS and SANCTUM_STATEFUL_DOMAINS environment variables on the Laravel side. As I see your API served from a different domain, so maybe you might want to check these settings.

Furthermore: If you will experience CORS issue on client side then use proxy in your nuxt.config.js. Here is an example:

  modules: [
    '@nuxtjs/axios',
    '@nuxtjs/auth-next',
  ],
  auth: {
    strategies: {
      laravelSanctum: {
        provider: 'laravel/sanctum',
        url: 'api', // note this
        endpoints: {
          login: {
            url: '/auth/login', // align this to your AuthController
          },
          user: {
            url: '/user',  // align this to your AuthController
          },
        },
      },
    },
  },

  axios: {
    proxy: true,
    credentials: true, // note this
  },

  proxy: {
    '/api': {
      target: process.env.API_URL, // in your .env check the url. It should be only the domain, example: https://domain.com without end sufix
    },
  },

I hope I could help :)

sts-ryan-holton commented 2 years ago

@barnabas-szekeres I'm not getting any CORS errors, I'm not sure this is an issue with SESSION_DOMAINS or SANCTUM_STATEFUL_DOMAINS as I'm able to log in, and cookies are correctly being set. The redirect object is (from past experience) is simply a way of redirecting Nuxt JS to some page at some route, and behind the scenes it's doing this based on the $store state.

Right now, that's not working, none of the redirect options are working for me, but they used to, back when I were using the local scheme, it seems that ever since using Sanctum they've stopped working, any I think this is a bug.

rikusen0335 commented 1 year ago

I have similar issue. I also represent the redirect object, but only redirect.home wouldn't work, so it doesn't redirect after logged in.

rikusen0335 commented 1 year ago

...according to reading the code, setting user.autoFetch to false causes the redirect issue. nvm

rikusen0335 commented 1 year ago

I forgot one thing to say, if there's no api to fetch the user, this problem occurs. Like, if it's configured to use /api/user/me, and backend has no route for that, this will occur.