Repository auth-module

Redirect not working after login or logout

Geminii
0
Geminii
commented 5 months ago

Version
v4.5.1

Reproduction link
https://github.com/nuxt-community/auth-module

Configuration

auth: {
    // Options
    strategies: {
      local: {
        endpoints: {
          login: { url: process.env.BASE_URL + '/auth', method: 'post', propertyName: 'token' },
          logout: false,
          user: { url: process.env.BASE_URL + '/users/auth', method: 'get', propertyName: false }
        }
      }
    },
    redirect: {
      login: '/signin',
      logout: '/signin',
      home: '/home'
    },
    watchLoggedIn: true,
    rewriteRedirects: false,
    resetOnError: true
  }

Action to login :

this.$auth.loginWith('local', {
            data: {
              username: this.username,
              password: this.password
            }
          });

Action to logout :
await this.$auth.logout();

Steps to reproduce
I use local strategy with a token. After the token expires if i reload the page, the user is logged out because i have resetOnError=True. I'm redirect to my signin page. Then i try to reconnect, i get new token and when i check into VueX, i got this result :

auth: {
busy:false,
loggedIn:true,
strategy:"local",
user: { ... }
 }

But i'm not redirect to home page … If i refresh, i'm connected into the home page.

Where i'm doing wrong ?
Thanks for your answers.

Regards,

0
redijedi
0
redijedi
commented 3 months ago

@Geminii I'm running into the same issue. Did you ever find the cause?

0
NoahCardoza
0
NoahCardoza
commented 2 months ago

Me too, I have spent so much time with this I could have implemented my own auth system by now… I was hoping this would be more intuitive.

0
NoahCardoza
0
NoahCardoza
commented 2 months ago

I came up with this which seems to work as I would have expected the redirects to work out of the box.

export default function ({ app }) {
  const oldLogout = app.$auth.logout.bind(app.$auth)
  const oldLogin = app.$auth.login.bind(app.$auth)

  app.$auth.logout = (...args) => {
    const _ = oldLogout(...args)
    _.then(() => app.$auth.redirect('logout'))
    return _
  }

  app.$auth.login = (...args) => {
    // sometimes doesn't work when the user tries to get to the admin page
    // before being logged in.

    const _ = oldLogin(...args)
    _.then(() => {
      app.$auth.redirect('home')
    })
    return _
  }
}
0
sayx
0
sayx
commented 2 months ago

I came up with this which seems to work as I would have expected the redirects to work out of the box.

export default function ({ app }) {
  const oldLogout = app.$auth.logout.bind(app.$auth)
  const oldLogin = app.$auth.login.bind(app.$auth)

  app.$auth.logout = (...args) => {
    const _ = oldLogout(...args)
    _.then(() => app.$auth.redirect('logout'))
    return _
  }

  app.$auth.login = (...args) => {
    // sometimes doesn't work when the user tries to get to the admin page
    // before being logged in.

    const _ = oldLogin(...args)
    _.then(() => {
      app.$auth.redirect('home')
    })
    return _
  }
}

@NoahCardoza Where did you make these changes in the configuration?

0
ConsoleTVs
11
ConsoleTVs
commented 2 months ago

This lirbary seems dead. it's faster to create a few methods and states on vuex and manage the stuff alone…

0
NoahCardoza
0
NoahCardoza
commented 2 months ago

@sayx I added this as an auth-module plugin.

nuxt.config.js

...
  auth: {
    plugins: ['~/plugins/auth.js'],
    strategies: {
      local: {
        endpoints: {
          login: { url: '/api/login', method: 'post' },
          logout: { url: '/api/logout', method: 'get' },
          user: { url: '/api/user', method: 'get', propertyName: 'username' }
        },
        tokenRequired: false,
        tokenType: false,
      }
    },
    redirect: {
      login: '/login',
      logout: '/login',
      home: '/admin',
      user: '/admin',
    },
  }
...

~/plugins/auth.js being what I shared earlier.

@ConsoleTVs I feel like the library is useful, it just didn't seem to function the way I thought it would.

0
dzsessona
0
dzsessona
commented a month ago

I am having the same problem,

  auth: {
    rewriteRedirects: true,
    strategies: {
      local: {
        endpoints: {
          login: { url: 'user/login', method: 'post', propertyName: 'data.token' },
          user: { url: 'user/me', method: 'get', propertyName: 'data' },
          logout: false
        }
      },
      facebook: {
        client_id: process.env.FB_APP_ID,
        userinfo_endpoint: 'https://graph.facebook.com/v2.12/me?fields=about,name,picture{url},email',
        scope: ['public_profile', 'email'],
        redirect_uri: process.env.FB_REDIRECT_URI
      },
      google: {
        client_id: process.env.GOOGLE_APP_ID,
        scope: ['profile', 'email'],
        redirect_uri: process.env.GOOGLE_REDIRECT_URI
      },
    },
    redirect: {
      login: '/login',
      logout: '/',
      home: '/privacy',
    }
  },

A guarded route profile, when trying to access it withought being logged in it redirects to the login page, after login (on any strategy… local or oauth) it always redirects to /privacy instead of the original guarded route as per documentation.

Please note that even by default rewriteRedirects should be true i am making sure of it in the config options.

0
sfgn
0
sfgn
commented a month ago

I can't actually get the auth module to redirect after login. It just never emits the routeChange event after I log in it seems. I can get it to redirect on logout, or from an authed page when not logged in.

auth: {
    strategies: {
      local: {
        endpoints: {
          login: {
            url: '/login',
            method: 'get',
            propertyName: 'token'
          },
          logout: {
            url: '/logout',
            method: 'delete'
          },
          user: {
            url: '/me',
            method: 'get',
            propertyName: 'data'
          }
        }
      }
    },
    redirect: {
      login: '/auth/login',
      logout: '/',
      home: '/',
      callback: '/'
    },
    watchLoggedIn: true,
    rewriteRedirects: true
  },

I'm still not sure what callback is for in redirects, but I've changed it to every possible value I can think of, as well as omitting it, with no difference.

I've tried running as SSR and SPA, with no difference as well.

0
shawnlauzon
0
shawnlauzon
commented 4 days ago

Duplicate of #134. See workaround in that thread which resolved the problem for me.

0
Informations
QuestionUnresolved
#c155 - Created 5 months ago