title: Laravel 5.4 redirect to specific page if user is not authenticated using middleware
body: <p>I want to redirect user, if not authenticated, to my index page (which is the login page)</p>
  
  <p>Can't seem to make it work and i really got confused with the routing.</p>
  
  <blockquote>
    <p>HomeController</p>
  </blockquote>
  
  <pre><code>class HomeController extends Controller
  {
  
      /**
       * Show the application dashboard.
       *
       * @return \Illuminate\Http\Response
       */
      public function index()
      {
          return redirect()-&gt;guest('/');
      }
  }
  </code></pre>
  
  <blockquote>
    <p>Routing</p>
  </blockquote>
  
  <pre><code>// Index
  Route::get('/', [
      'as' =&gt; 'index',
      'uses' =&gt; 'UserController@index'
  ]);
  </code></pre>
  
  <blockquote>
    <p>UserController</p>
  </blockquote>
  
  <p>The routing as you see redirects to a <strong>User Controller</strong> at <strong>index</strong> function, which is the below :</p>
  
  <p>*has <strong>__construct()</strong> so it uses the middleware 'auth'.</p>
  
  <pre><code>public function __construct()
  {
      $this-&gt;middleware('auth');
  }
  
  public function index(){
  
      // If user is logged
      if(Auth::check()) {
  
          // If user has NOT submitted information form redirect there, otherwise to categories
          if(!Auth::user()-&gt;submitted_information)
              return redirect()-&gt;route('information');
          else
              return redirect()-&gt;route('categories');
      }
      else
          return view('index', ['body_class' =&gt; 'template-home']);
  
  }
  </code></pre>
  
  <blockquote>
    <p>Handler.php</p>
  </blockquote>
  
  <p>And the <strong>unauthenticated function</strong> inside middleware of auth (Exceptions/Handler.php)</p>
  
  <pre><code> protected function unauthenticated($request, AuthenticationException $exception)
      {
          if ($request-&gt;expectsJson()) {
              return response()-&gt;json(['error' =&gt; 'Unauthenticated.'], 401);
          }
  
          return redirect()-&gt;route('index');
      }
  </code></pre>
  
  <p>The error i get right now is the below :</p>
  
  <pre><code>InvalidArgumentException in UrlGenerator.php line 304:
  Route [index] not defined.
  </code></pre>
  
  <p>This error happens because of the line of </p>
  
  <p><code>return redirect()-&gt;route('index');</code> in the above <strong>unauthenticated</strong> function.</p>
  
  <p>What am i missing here? If you need any more information please feel free to ask.</p>
  
  <p><strong>EDIT</strong> : Until now, if i remove from <strong>UserController</strong> the <code>__construct()</code> method, and insert in <code>web.php</code> to all the routes what <code>middleware</code> to use, it works.</p>
  
  <p>For example </p>
  
  <pre><code>Route::get('/categories', [
      'as' =&gt; 'categories',
      'uses' =&gt; 'UserController@showCategories'
  ])-&gt;middleware('auth');
  </code></pre>
  
  <p>But i am trying to find, without specifying there what middleware to use, to use it automatically.</p>