Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 Auth redirect always to login page? /login

Tags:

php

laravel-4

I am using laravel for my web application and in routes.php I have:

// admin routes
Route::group(array('before' => 'auth'), function()
{
Route::controller('admin', 'UsersController');
});

I want to protect and check if the person is logged in , but this code always redirect to "/login" i want it to redirect to "admin/login" , can this be done?

like image 887
user3150060 Avatar asked Apr 08 '14 21:04

user3150060


1 Answers

There is a default auth filter in the filters.php file, it should be like this:

Route::filter('auth', function($route, $request)
{
    if (Auth::guest()) return Redirect::guest('login'); // /login url
});

This filter (given above) will check if the user is not logged in then a redirect will occur and user will be sent to /login url and otherwise nothing will happen, user will be sent to the requested page.

Also, following filter is available by default and this filter just checks if the user is already logged in then (s)he will be redirected to / (home page) by default:

Route::filter('guest', function($route)
{
    if (Auth::check()) return Redirect::to('/'); // you may change it to /admin or so
});

This (guest) filter is used with /login as given below, so if a logged in user intended to log in then the user will be redirected to home page by default:

Route::get('login', array('before' => 'guest', 'uses' => 'UsersController@getLogin'));

Now, in your routes.php file you have following route declared:

Route::group(array('before' => 'auth'), function()
{
    Route::controller('admin', 'UsersController');
});

If everything is fine then this setup should work. If a logged out user tries to visit admin then user will be sent to login and these are by default available and should work.

like image 173
The Alpha Avatar answered Sep 20 '22 10:09

The Alpha