New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loggedIn set to false after refresh #53

Closed
fanckush opened this Issue Feb 4, 2018 · 9 comments

Comments

Projects
None yet
5 participants
@fanckush
Copy link

fanckush commented Feb 4, 2018

loggedIn is stored in the store (vuex) and so when i refresh the page i am logged out because i suppose loggedIn is set to false by default unless set to true after a successful login

Shouldn't LoggedIn be stored the same way the token is (cookie, localStorage)? or maybe set to true if token exists on load automatically?

If not then what is the recommended method for persistent session

This question is available on Nuxt.js community (#c38)

@pi0 pi0 closed this Feb 4, 2018

@pi0 pi0 reopened this Feb 4, 2018

@fanckush

This comment has been minimized.

Copy link
Author

fanckush commented Feb 4, 2018

so for now i added the following:

initloggedIn () {
if (! process.server) return
if (this.getState('token')) {
this.setState('loggedIn', true)
}
}

to auch.class.js and then call it inside init, if you believe this the right aproach then i will try to make a pull request

@pi0

This comment has been minimized.

Copy link
Member

pi0 commented Feb 4, 2018

Hi sorry for late answering. The behavior is intended indeed. loggedIn is set to true as soon as fetchUser is done not when just token is available. (https://github.com/nuxt-community/auth-module/blob/master/lib/templates/auth.class.js#L252) You can directly use token as a loggedIn validator if user availability is not important.

@fanckush

This comment has been minimized.

Copy link
Author

fanckush commented Feb 4, 2018

oh i see i missed this line:

return this.state.user ? Promise.resolve() : this.fetchUser()

i thought that fetchUser was called only once only inside login().

So if i had an api endpoint for user then loggedIn will remain true after page refresh?

@pi0

This comment has been minimized.

Copy link
Member

pi0 commented Feb 4, 2018

@fanckush Yes it should remain true (expected behaviour) :)

@fanckush

This comment has been minimized.

Copy link
Author

fanckush commented Feb 4, 2018

a n0ob question: you said

You can directly use token as a loggedIn validator if user availability is not important.

where would i set that?

@fanckush

This comment has been minimized.

Copy link
Author

fanckush commented Feb 4, 2018

never mind i will close this issue now. Thanks a lot!

@fanckush fanckush closed this Feb 4, 2018

@breakingrobot

This comment has been minimized.

Copy link
Member

breakingrobot commented Feb 4, 2018

@fanckush You can use a custom watchState like this :

const customValue = 'loggedIn' // could be anything
this.$auth.watchState(customValue, newValue => {
  // do you logic here.
})

Then, you should edit you configuration watchLoggedIn to false

@alexdilley

This comment has been minimized.

Copy link

alexdilley commented Feb 15, 2018

This functionality means that given mode: spa, a page refresh will $auth.redirect('home') instead of rendering the requested route (since the default watchState implementation is set up on init() – once the user is fetched [following a page refresh], it is considered a login action and a redirect occurs).

A workaround:

{
  auth: {
    watchLoggedIn: false,
  },
},

and use a custom watchState (which is seemingly set up after the user is fetched [following a page refresh])

// plugins/auth.js
export default ({ app }) => {
  // Only _actual_ login/outs (including resets) will be watched here.
  app.$auth.watchState('loggedIn', (isLoggedIn) => {
    if (isLoggedIn) {
      // Follow @nuxtjs/auth workflow.
      app.$auth.redirect('home');
    }
  });
};
@kamaln7

This comment has been minimized.

Copy link

kamaln7 commented Oct 4, 2018

In case anyone comes across this, watchState is now available under $auth.$storage, not $auth.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment