[Vuejs]-How to implement Auth::check() in laravel and vuejs

3👍

Protect your route with the authentication middleware:

Route::get('comments', function () {
    // Only authenticated users may enter...
})->middleware('auth');

or as a group:

    Route::group(['middleware' => ['auth']], function () {
        Route::post('comments', 'CommentController@store');
    });

Then laravel will automatically redirect unauthenticated users to your ‘login’ named route. (or specify a custom route in the redirectTo() method of app/Http/Middleware/Authenticate.php)

From your ajax call in vue, you can listen for a 403 response code and then redirect the user to the login url. But if you put your main app’s route in the auth middleware, you will never have to worry about this part, since laravel will redirect users automatically.

1👍

This really depends on how you built your application if it is an spa, you should use something like laravel airlock or passport instead of the default login logic.

If this is a multipage application, you should include a simple auth middleware.

With that you could simply tell in your blade file which elements shall be included or not like this:

@auth
  <h1>Logged in</h1>
@endauth

You could have a simple route which checks wether you are logged in or not, depending on the result you can do some magic.

Your route:

use Auth;

Route::get('is-auth', function () {
    $auth = Auth::user();
    return $auth;
});

Now you save the user to the global window object.

In generel you could do something like this, but I do not recommened the ideas below.

You could then sent a request to the server, which I do not recommend and get the result if you are logged in or not.

axios.get('is-auth')
   .then(response => {
     if(response.data) {
        console.log("not logged in");
     }
     console.log("logged in");
   });
   .catch(error => console.log("error");
👤utdev

Leave a comment