简体   繁体   中英

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

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'));
}

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
 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM