Search code examples
vue.jsvuejs2nuxt.js

How $auth.loggedIn works in nuxt.js?


$auth.loggedin returns true if a user is authenticated and false otherwise but what does Nuxt exactly check to set this flag to true or false?
How can Nuxt tell if a user is authenticated or not?
As far as I understood it checks the response coming from the backend after sending a login request.
But if the response only contains user information and a token how could Nuxt tell that this user is authenticated?


Solution

  • This answer may maybe help: https://stackoverflow.com/a/68081536/8816585

    Essentially, you have various ways of authenticating with the auth module. It all depends of your scheme configuration: https://auth.nuxtjs.org/guide/scheme

    You can either POST on the backend with your credentials and see if it returns a positive access granted response, then update Vuex with those email + password combo. Or you could also fetch those directly, if it's contained into the backend response.

    So, basically the module looks if you have a successful response from the backend, if it does, $auth.loggedin is true and the middleware let you pass.