Login redirect not working, $auth.state.loggedIn false

danjac
5
danjac
commented 2 years ago

In my login.vue submit method I have:

this.$auth.login({ data: this.form })
The correct endpoint is reached with the right values, a token is returned and saved correctly to localStorage. A follow up request to /user endpoint also executes successfully. All subsequent requests include the correct Authorization header.

However:

  • there is no redirect to the home page after login
  • $auth.state.loggedIn and $auth.state.user are both false/null, all secure pages (using middleware['auth']) redirect to login
    It appears that the only thing working is the authorization header, vuex state is not being updated correctly.
bug
0
danjac
5
danjac
commented 2 years ago

To confirm, there is no change to vuex state after a successful login. Store index.js is just empty:

export default {
  state: () => ({

  })
}
0
breakingrobot
305
breakingrobot
commented 2 years ago

Hi @danjac, thank you for contributing to auth-module ๐Ÿ˜„

For future reference, maybe related to #55 @pi0

Could you provide some essential informations :

  • Which operating system are you using ?
  • Which version of auth-module are you using ?
  • How did you manage to get this error ? (code reproduction)
  • How did you run your application ? (SSR / SPA / โ€ฆ)
  • Which Nuxt variant are you using ? (Vanilla, using class-component through Babel, using Typescript ?)

I tried to reproduce on our examples, but couldn't do you have a custom Vuex store, if yes, what is its contents.

Thank you very much ๐Ÿ‘

0
danjac
5
danjac
commented 2 years ago

Hi, thanks for fast response!

Full client code here:

https://github.com/danjac/spacenews-ui

Version: latest install (npm install @nuxtjs/auth)
OS: Linux/Fedora 27
Running on SSR

0
breakingrobot
305
breakingrobot
commented 2 years ago

Thanks @danjac !

We are going to investigate the issue asap with @pi0

0
breakingrobot
305
breakingrobot
commented 2 years ago

@danjac

You set user fetch endpoint as this:

user: { url: '/api/auth/me/' },

By default, the module is using these options:

user: { method: 'get', propertyName: 'user' }

Could you please enforce your propertyName to your user object path/name and tell me how it reacts?

0
danjac
5
danjac
commented 2 years ago

I think I see the issue: the endpoint doesn't return a "user" object, rather the user itself is the payload:

{
    "email": "******@gmail.com",
    "id": 1,
    "username": "danjac"
}

This might explain why the user is not being set correctly, however loggedIn is not being set either (and here are no error messages).

EDIT: for the above issue I found this to be the solution:

    endpoints: {
     ....
      user: { url: '/api/auth/me/', propertyName: false },
    },

This fetches the user object as-is. Looks like all working well, thanks!

0
breakingrobot
305
breakingrobot
commented 2 years ago

@danjac Still it should be reported to the user anyway :)

0
doublemarked
5
doublemarked
commented 2 years ago

Encountered this today too and burned an hour debugging things. IMHO it is misleading/wrong for the propertyName to default to user even when specifying a custom endpoint config, including url. This propertyName: "user" default value seems based on odd assumptions of what is normal/typical.

This is also in conflict with the documentation, which states:

propertyName can be used to specify which field of the response to be used for value. It can be undefined to directly use API response or being more complicated like auth.user.

This does not work - omitting propertyName (thus making it undefined) or explicitly setting it as such (propertyName: undefined) will not work.

1
barrachri
0
barrachri
commented 2 years ago

Same problem with redirecting after positive login.

If I try to connect to protected pages, like /, redirect me to /users/login but after I logged in, I don't get redirected.

this.$auth.token has the right value and this.$auth.loggedIn is true.

login () {
      this.$auth.login({
        data: this.data,
      })
        .then((res) => {
          console.log('Auth Success')
          console.log(this.$auth.token)
          console.log(this.$auth.state.loggedIn)
        })
        .catch((err) => {
          console.log(err)
          this.$notify({
            title: 'Important message',
            text: err
            })

        })
      }
auth: {
    endpoints: {
      login: { url: '/auth/login', method: 'post', propertyName: 'token' },
      logout: { url: '/auth/logout', method: 'post' },
      user: { url: '/auth/user', method: 'get', propertyName: 'user' }
    },

    redirect: {
      login: '/users/login',
      home: '/'
    },

    cookie: false,
  },

  router: {
    middleware: ['auth']
  },
0
breakingrobot
305
breakingrobot
commented 2 years ago

Fixed as of 4.0 ๐Ÿ‘

Please create a new issue if it happens again in 4.0 :)

0
ErwanTouba
0
ErwanTouba
commented 2 years ago

Having the same issue here, using "@nuxtjs/auth": "^4.5.1"
@danjac your workaround did the job, thx

0
webslesar
0
webslesar
commented 2 years ago

It is reproduces using mode: 'universal' instead of spa.

0
amdp
0
amdp
commented a year ago

still in 4.5.3
setting user: { url: '/api/auth/me/', propertyName: false }, worked thanks @danjac

0
siberiadev
5
siberiadev
commented a year ago

My code is working fine on my desktop ubuntu. But when I put code into remote server (ubuntu too) it didn't set loggedIn and user just like write above.

I set my user propertyName to false, but it still didn't work.

Any suggestions?

0
PTiCA1
0
PTiCA1
commented a year ago

After switching the mode from "Universal" to "SPA", login and redirection work correctly in @nuxtjs/auth: 4.5.3

0
imsidz
0
imsidz
commented 9 months ago

getting same issue in @nuxtjs/auth: 4.8.1
after login it redirect /dashboard which is protected by auth when i refresh page it redirect to /login page but i am already logged when i go to / page appear user name on

0
paulvonber
0
paulvonber
commented 7 months ago

@imsidz Same here on v4.8.2

0
Informations
Bug Report โ€ข Open
#c44 - Created 2 years ago