Home Laravel 5.5 Getting 302 redirect error when redirecting user to homepage after login
Reply: 1

Laravel 5.5 Getting 302 redirect error when redirecting user to homepage after login

DevMan
1#
DevMan Published in 2018-02-11 20:33:56Z

I have searched a lot of forums and didn't find my mistake here. I'm not very good in laravel so I'm sure I'm doing a mistake which would be obvious to more advanced developers. My page works like this. When new user first comes to the website there will be no need to login and user can view the home page. Then User registers and will be redirected to an email validation page with a link to the home page but the user isn't logged in automatically. After user confirms her email, she can go and log in. After login I want the user to be redirected to the home page if the user is a normal user and to the dashboard if the user is an Admin. Here is where the problem comes. After login I get the 302 redirection error. Below you can see my routes and different controllers I've edited. Hope I've been able to explain the problem. Thanks for any help in advance.

My web.php file

Route::get('/', ['middleware'=>'web','uses'=>'HomeController@index']);
Route::get('/home', ['middleware'=>'auth','uses'=>'HomeController@after_login']);
Route::get('/search',['middleware'=>'web','uses'=>'HomeController@search']);
Route::get('/search-all-category',['middleware'=>'web','uses'=>'HomeController@search_all_category']);
Route::get('/register/register-search-cities',['middleware'=>'web','uses'=>'HomeController@register_search_cities']);
Route::post('/company-results',['middleware'=>'web','uses'=>'HomeController@search_companies']);
Route::get('/validate-email',function(){
    return view('validate-email');
});
Route::get('/all', ['middleware'=>'guest','uses'=>'HomeController@index']);
Auth::routes();

My LoginController.php controller, where I've overwritten the authenticated function from the AuthenticatesUsers.php :

//   protected $redirectTo = '/home';


protected function authenticated( Request $request, $user ) {
        if($user->isAdmin == '0'){

            return redirect()->intended('/home');
        }else if($user->isAdmin == '1'){
            return redirect()->intended('dashboard.dashhome');
        }else{

            return redirect()->intended('/home');
        }
}

And here is my HomeController.php which handles the routes:

public function index() {
    $country = Country::all();
    return view('/home',compact('country'));
}

public function after_login(){
    $country = Country::all();
    return view('/home',compact('country'));
}
Ben Dubuisson
2#
Ben Dubuisson Reply to 2018-02-12 01:40:09Z

Your code

 if($user->isAdmin == '0'){

            return redirect()->intended('/home');
        }else if($user->isAdmin == '1'){
            return redirect()->intended('dashboard.dashhome');
        }else{

            return redirect()->intended('/home');
        }

can be simplified like so (and after your comments, redirect non admins to / because otherwise they'll be stuck in a loop

 if ($user->isAdmin){
     return redirect()->intended('dashboard.dashhome');
 } else {
     return redirect()->intended('/'); // return to a route not behind auth middleware to avoid loop
 }
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.354655 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO