0

I am trying to use Laravel 5 User Authentication. In this regard my URL is http://localhost/lara_project/public/auth/login before login. I would like to redirect user to http://localhost/lara_project/public/home after successful login. But it is redirect to http://localhost/lara_project/public/. I tried with routes like below

Route::get('home',['middleware' => 'auth', 'uses' => 'WelcomeController@index']);

Route::get('/', 'HomeController@index');

Route::controllers(['auth' => 'Auth\AuthController','password' => 'Auth\PasswordController']);

which are resided in routes.php

My Problem is afrer successful login user redirected to http://localhost/lara_project/public/ and if I would like to browse http://localhost/lara_project/public/home after successfull login it is showing error

This webpage has a redirect loop
ERR_TOO_MANY_REDIRECTS

I tried by editing RedirectIfAuthenticated.php,compiled.php and AuthenticatesAndRegistersUsers files. But could not get any satisfactory result.

Could anyone help me in this regard ??

3 Answers 3

2

Go to Auth\AuthController and add a variable $redirectTo = 'home';

This with tell laravel where to redirect to after login.

1
  • Thanks @Carthagian Traveler for your reply. Your solution is not working. Thanks
    – abu abu
    Commented Jul 16, 2015 at 7:57
1

Modify RedirectIfAuthenticated.php as below

public function handle($request, Closure $next)
{
    if ($this->auth->check() && !$request->is('/home'))
    {
        return redirect('/home');
    }

    return $next($request);
}

Please also have a look at here. I am using some kind of code as below.

public function handle($request, Closure $next)
{
    if (!$request->is('authenticate') && !$request->is('authenticate/login') && !Auth::check()) {

        return $request->ajax() ? response(array("status"=>'404')) : redirect('authenticate/login');
    }
    if(Auth::check()) {
        if( ! $request->user()->isAdmin() ) {
            return $request->ajax() ? response(array("status"=>'404')) : redirect('authenticate/login');
        }
        return $next($request);
    }
    return $next($request);
}
5
  • Thanks @Pratik Soni. Your solution is not working. Thanks
    – abu abu
    Commented Jul 14, 2015 at 11:40
  • can you try !$request->is('home') and let me know? Commented Jul 14, 2015 at 12:16
  • I tried !$request->is('home'). Now the error This webpage has a redirect loop ERR_TOO_MANY_REDIRECTS is not coming, I can see the home page. But after successful logged in it is redirecting to http://localhost/lara_project/public/. I need redirection to http://localhost/lara_project/public/home.
    – abu abu
    Commented Jul 14, 2015 at 13:30
  • I have edited the code. Please see the code and let me know Commented Jul 14, 2015 at 16:27
  • Thanks @Pratik Soni. Your solution is not working. Thanks
    – abu abu
    Commented Jul 16, 2015 at 7:57
1

Go to /app/Http/Controllers/Auth/LoginController.php

There you can change protected $redirectTo = RouteServiceProvider::HOME; that to what you want.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.