auth-module: Login redirect not working, $auth.state.loggedIn false
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.
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Comments: 28 (5 by maintainers)
I think I see the issue: the endpoint doesn’t return a “user” object, rather the user itself is the payload:
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:
This fetches the user object as-is. Looks like all working well, thanks!
Encountered this today too and burned an hour debugging things. IMHO it is misleading/wrong for the propertyName to default to
usereven when specifying a custom endpoint config, including url. ThispropertyName: "user"default value seems based on odd assumptions of what is normal/typical.This is also in conflict with the documentation, which states:
This does not work - omitting
propertyName(thus making it undefined) or explicitly setting it as such (propertyName: undefined) will not work.same on 4.9.1, really frustrating
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
Motivated by the laughable amount of hours spent while dealing with this library, especially this problem, I should comment here and open another issue afterwards, using the template for new issues.
The problem I was having
User accesses the login screen, types correct username/password combination, Auth module is configured with
localstrategy, fetches the endpoint for auth, gets a JWT token, fetches the user and nothing else happens. The user is stuck at/login.Files
nuxt.config.jspages/login.vue(only the JS)I just followed the demos present in the source. My
@nuxtjs/authcurrent version is 4.9.0.Debugging the Vuex state, I found some very interesting things:
return testis executed atsubmitLogin(),$auth.loggedInisfalseand$auth.userisnull. Curiously, if I manually go to an address where my user is authorized by the business rules, after the login attempt, the screen opens normally and all the states are normal ($auth.loggedInistrueand$auth.useris defined and valid);await this.$auth.logout();, the screen doesn’t redirect to/loginafterwards. In truth “nothing” happens. However, if I navigate to any route that requireauthmiddleware (the user logged in), I see the/loginscreen again;I dug into the code and I’ve found this and this (that basically use
Promises) and the strategy’sloginimplementation (that usesawait). So now it’s clear that:localstrategy, if it is slow enough, can’t update the user state before the library tries the redirect, which checks forloggedIn == false, which in turn just keeps the user in the same login screen (the feeling that nothing happened);In order to make it work, the only thing I can do is to test the return from
this.$auth.loginWith('local'), to verify if I got, for instance, a 2XX status code, and then redirect the user manually.So I modified my
submitLoginmethod as follows:This solves half of my problem. For the logout, I had to do something like this:
But refreshing the current page to cause a redirect to login has a problem: some of my pages have things executing on
asyncData, which executes before the browser starts to redirect to the/loginroute, and flashes 401 messages to my user. To guarantee my user will not see these 401’s, I had to create a global middleware (registered innuxt.config.js) with the following:Finally, I had to implement a redirect plugin (
/plugins/auth.js) to get the redirections, since the redirect supported implemented in this library doesn’t work properly:plugins/auth.jsFunny fact, this code is implemented here.
Final
nuxt.config.jsMy 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?
Me too, just simple middleware
guest.js
But in ssr it’s always false like this