0

I have this problem, my application should redirect users to admin area whose routes are protected (have middle auth). When I login it redirects to login page again but when place the dashboard route outside the route group, it behaves well. What may be the problem? This is my code:

Code for protected route (does not work) after login

Route::group(['middleware'=>'auth'], function(){
    Route::get('backend/dashboard', array('as'=>'dashboard', 'uses'=>'BackendDashboardController@getDashboard'));
});

Code for dashboard route placed outside the route group (Works well after login)

Route::get('backend/dashboard', array('as'=>'dashboard', 'uses'=>'BackendDashboardController@getDashboard'));

Auth controller - Post login function

protected function postLogin() {
        $request = Input::all();
        $user = array(
            'email' => $request['email'],
            'password' => $request['password']
        );
        if ($this->auth->attempt($user)) {
            return redirect(route('dashboard'));

        }else{
            return redirect(route('login'));
        }
    }

I really want to protect my admin routes and place all of them under auth middleware. Kindly avice

Ashutosh Sharma
  • 426
  • 1
  • 9
  • 20
Jack Zollo
  • 13
  • 2

1 Answers1

0

It only redirects, if you are not authorize/loggedin, otherwise it works fine. and I think you are missing something in Route::group() you also need to mention the prefix eg

Route::group(['prefix'=>'backend', 'middleware'=>'auth'], function(){ 
   Route::get('dashboard', array('as'=>'dashboard', 'uses'=>'BackendDashboardController@getDashboard')); 
});

Edited

Also try to update your attempt method, then try it, eg:

    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication passed...
        return redirect()->intended('dashboard');
    }
Qazi
  • 4,815
  • 7
  • 38
  • 61
  • Even after adding 'prefix'=>'backend' still redirects to login page. Note, if I use Route::get('dashboard', array('as'=>'dashboard', 'uses'=>'BackendDashboardController@getDashboard')); without placing it under Route::group(['prefix'=>'backend', 'middleware'=>'auth'], function(){ }); it works fine and redirects to dashboard after login – Jack Zollo Feb 19 '16 at 08:05
  • in your dashboard controller method, do this, show me result. `print_r(\Session::all());` – Qazi Feb 19 '16 at 08:53
  • Qazi, I think the problem is with laravel not setting session after authentication because print_r(\Session::all()); outputs Array() which shows there is no saved session. What could make laravel not save sessions? – Jack Zollo Feb 19 '16 at 09:15
  • Its printing empty array.. Its mean that your authentication/login not working.. Because whenever login works its sets values into session – Qazi Feb 19 '16 at 16:52