Rainieren's avatar

Laravel - Prevent Automatic redirect to /login

Hello, When a user accesses my website they immediately get redirected to /login but I want to prevent that. Instead, I want them to see the home page which in this case is a blade file with nothing in it but eventually will be a page filled with content. I had this working in my previous project but now I can't get the same result.

My web.php looks like this:

Route::get('/', 'HomeController@index')->name('home');

Route::get('logout', function() {
    Auth::logout();
    return redirect('/');
});

Auth::routes();

In the meanwhile, I changed every AuthController's protected $redirectTo from /home to /. But that does not fix it either.

The homeController looks like this

class HomeController extends Controller
{
  
    public function __construct()
    {
        $this->middleware('auth');
    }

   
    public function index()
    {
        return view('home');
    }
}

What is causing this? Thanks in advance!

0 likes
5 replies
RamjithAp's avatar

Your home controller is protected with auth middleware thats why it getting redirected to login page now you have two options.

Remove auth middleware from homecontroller

class HomeController extends Controller
{
  
   // public function __construct()
  //  {
     //   $this->middleware('auth');
    // }

   
    public function index()
    {
        return view('home');
    }
}

Or Show your home page directly from route file

Route::get('/', function(){
return view('home');
}
Route::get('logout', function() {
    Auth::logout();
    return redirect('/');
});
Auth::routes();
1 like
Rainieren's avatar

@RamjithAp That's odd because In my previous project I did not have to remove that what so ever and it still works perfectly fine. What are the consequences of removing that middleware? And is there maybe another cause?

RamjithAp's avatar

Including middleware in constructor function on any controller tells that all the methods in that controller should pass the specified middleware conditions. In our case, you are including Auth middleware so all the unauthenticated request to that controller will be redirected to login page to get authenticated.

mushood's avatar

@Rainieren

You should take a look at the documentation: https://laravel.com/docs/5.5/controllers#controller-middleware

When you assign a middleware in a controller's constructor, all actions in that controller are checked against the auth middleware in your case. If the user is not logged in, it redirects to the front page.

However, instead of removing it, you can use only() and except()

Here are the two examples from the documentation:

        $this->middleware('log')->only('index');

        $this->middleware('subscribed')->except('store');

For your case

class HomeController extends Controller
{
  
    public function __construct()
    {
        $this->middleware('auth')->except('index');
    }

   
    public function index()
    {
        return view('home');
    }
}
1 like
Rainieren's avatar

I got it working already, The problem was the controller. I changed it from HomeController to PostController and then it worked, Thanks for helping though!

Please or to participate in this conversation.