0
votes

Im facing problem using filters in laravel. Here is the piece of code

Route::get('admin/login', function()
{
return View::make('admin.login');
});

Route::get('admin/logout', function()
{
    Auth::logout();
    return Redirect::to('admin/login');
});

Route::post('admin/login', function()
{
$userdata = array('username' => Input::get('username'),
                  'password' => Input::get('password'));

if (Auth::attempt($userdata))
{
    return Redirect::to('admin');
}
else
{
    return Redirect::to('admin/login')->with('login_errors',true);
}
});

Route::get('admin', array('before' => 'auth', function() {
    return Redirect::to('admin');
}));

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

The problem is when im opening /admin, it is redirecting me to the admin/login (which is good). Now if I enter the correct username and password, it should take me to /admin page, but it again redirect me back to admin/login page. I think whenever I want it to redirect me to /admin, the (before auth) filters run and it takes me back to admin/login.

Please Help

1
Didn't you consider that your auth attempt was failed? - Vitaly Dyatlov
no, it has not failed. It is not going in the else part. I checked it - Ankur Goyal
then add some results output to the filter to check if it is actual problem. In general your code should work, so it is something on your side. - Vitaly Dyatlov
I returned "Hello" in the filter and it worked that is i got the "hello" on the /admin view - Ankur Goyal

1 Answers

0
votes

According to me you have a redirect loop here:

Route::get('admin', array('before' => 'auth', function() {
    return Redirect::to('admin');
}));

You are trying to redirect to the admin page if the auth filter passes but this is the same route.