How to check if the user is logged in VueJS + Laravel 5.5?

1

I am following a course in Laravel 5.5 + VueJS, and putting into practice what is being taught. However, it does not address authentication at the moment, and since I'm designing the entire project to load your content with VueJS, it's getting a little 'sad' on the page refresh in the login system.

So, temporarily, I did a small 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 of the user. I've been thinking of the best way to solve this, so I saw the possibility of entering a definite field in all the answers of requests with a boolean indicating if it is or not authenticated. But like the first, I think it's a beautiful POG.

So I ask, how to check if the user is authenticated in VueJS?

    
asked by anonymous 05.11.2017 / 23:14

1 answer

1

On the systems I do, I work as if the front-end were separate, and in this case, when I log in, I return the user's basic data including a token, saving it in the localstorage. At a glance at the documentation, laravel already has a schematic ready.

In this case, in every request that the user needs to be logged in I send this token and if the token is invalid the laravel will return an HTTP status 401 (if I am not mistaken) telling me that the user is not logged in, this way I treat as I need.

I hope that I have understood the doubt and, if so, that I have been able to clarify something.

    
06.11.2017 / 01:42