How to verify that the user is logged in to Vuejs + Laravel 5.5?

Asked

Viewed 710 times

1

I am following a course of Laravel 5.5 + Vuejs, and putting into practice what is being taught. However, it doesn’t address authentication at the moment, and as I’m structuring the whole project to load its content with Vuejs, it’s getting a little 'sad' the page refresh in the login system.

So, temporarily, I did a little function in the beforeCreate method to send a request. However, in this way, all requests made by the user, make 2 requests: One to know if it is connected and the other from the user himself. I’ve been thinking about the best way to solve this, so I saw the possibility of inserting a definitive field in all the request responses with a boolean indicating whether or not it’s authenticated. But just like the first, I believe I’m a beautiful POG.

So I ask, how to verify if the user is authenticated in Vuejs?

1 answer

1


In the systems I do I work as if the front end is separate, and in this case, when logging in I return basic user data including a token, saving this in localstorage. From a look at the documentation, Aravel already has a scheme ready.

That done, in any request that the user needs to be logged in I send this token and if the token is invalid the Laravel will return me a status http 401 (if I am not mistaken) telling me that the user is not logged in, so I treat as I need.

I hope I’ve understood the doubt and if so, that I’ve cleared up something.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.