How to redirect to Login if user not logged in Laravel

0 votes

I have code in my controller's __construct like

if(Auth::check())
{
    return View::make('view_page');
}

return Redirect::route('login')->withInput()->with('errmessage', 'Please Login to access restricted area.');

its working fine, but what i wants is. its really annoying to put these coding in each and every controller, so i wish to put this Verify Auth and redirect to login page in one place, may be in router.php or filters.php.

I have read some posts in forum as well as in stackoverflow, and added code in filters.php like below but that's too not working.

Route::filter('auth', function() {
    if (Auth::guest())
    return Redirect::guest('login');
});

Please help me to resolve this issue.

Dec 4, 2020 in Laravel by kartik
• 37,490 points
847 views

1 answer to this question.

0 votes

Hello @kartik,

You can use it in middleware. If you are using the Laravel build in Auth, You can directly use the auth middleware as given,

Route::group(['middleware' => ['auth']], function() {
   // define your route, route groups here
});

or, for a single route,

Route::get('profile', function () {

})->middleware('auth');

Hope it helps!!

Thank you!!

answered Dec 4, 2020 by Niroj
• 82,560 points

Related Questions In Laravel

0 votes
1 answer

How to get all the users except current logged in user in laravel eloquent?

Hello @kartik, You can get the current user's ...READ MORE

answered Dec 8, 2020 in Laravel by Niroj
• 82,560 points
209 views
0 votes
1 answer

How can we check the logged-in user info in Laravel?

Hey @kartik, yes we can keep track of ...READ MORE

answered Mar 20, 2020 in Laravel by Niroj
• 82,560 points
148 views
0 votes
1 answer

How to check request is ajax or not in Laravel?

Hello, Laravel allow use of their library method that ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
454 views
0 votes
1 answer

Error:login in Laravel. How to solve?

Hey, First check if your model login has a field password in ...READ MORE

answered Mar 26, 2020 in Laravel by Niroj
• 82,560 points
1,361 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,560 points
5,459 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,560 points
635 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
487 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,560 points
6,058 views
0 votes
1 answer

How to redirect to login if user is not authenticated in laravel?

Hello @kartik, Use middleware for this purpose and ...READ MORE

answered Oct 28, 2020 in Laravel by Niroj
• 82,560 points
980 views
0 votes
1 answer

How to validate an input field if value is not null in Laravel?

Hello @kartik, try using nullable as a rule 'password' ...READ MORE

answered Sep 25, 2020 in Laravel by Niroj
• 82,560 points
2,099 views