0

how to check user login? if the user is not logged in, the user cannot submit comments, and redirect to the login page for login first.

I've try, but didn't work

7
  • is it in vuejs .? Commented Jan 29, 2020 at 10:06
  • @KamleshPaul yes check in vuejs. but for the controller, i used laravel Commented Jan 29, 2020 at 10:08
  • and which auth you are using for vuejs login.? Commented Jan 29, 2020 at 10:09
  • @KamleshPaul no, for login i'm using laravel, but for form comment i'm using component vue Commented Jan 29, 2020 at 10:13
  • then when user enter comment let them enter but when comment hits to server you can check. if user is not login you can send response false and inside vuejs you can do if response is false then window.location.href="login" Commented Jan 29, 2020 at 10:16

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.