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

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

user10461 Published in September 21, 2018, 8:38 am

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']);
    return view('validate-email');
Route::get('/all', ['middleware'=>'guest','uses'=>'HomeController@index']);

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

            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'));
share|improve this question
  • as a note, if($user->isAdmin == '1') can just be if($user->isAdmin) and if($user->isAdmin == '0') if(!$user->isAdmin) – Ben Dubuisson Feb 12 at 1:08
  • HTTP 302 is not an error but a http code for redirection. What error are you getting exactly? – Ben Dubuisson Feb 12 at 1:14
  • @Ben Dubuisson the error I get says the page was redirected wrongly. In my browser devtool I see like 50 attempts to redirect to home and then it gives the error. ( Sorry if rhere are spelling mistakes I'm on mobile) – DevMan Feb 12 at 1:25
  • sounds like a redirect loop. – Ben Dubuisson Feb 12 at 1:26
  • can you walk me through the steps again? Login as an admin results in the loop? Login as a non admin does what? – Ben Dubuisson Feb 12 at 1:27

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO