title: How to programmatically login then redirect with Laravel 5.5?
body: <p>I am using Laravel 5.5 and PHP 7.1
  I am using the standard built-in Auth and middleware, I have not customized anything.</p>
  
  <p>I am wanting to create a route in the api.php routes file that would be something like this:</p>
  
  <pre><code>Route::group(array('prefix' =&gt; 'mobile'), function () {
        Route::get('requestaccess', ['uses' =&gt; 'MobileController@RequestAccess']);
  });
  </code></pre>
  
  <p>Then in my MobileController I would have this code:</p>
  
  <pre><code>public function RequestAccess() {
      try {
          $input = request()-&gt;all();
          $token = $input['token'];
          //$page = $input['page'];
  
          $user = User::where('mobile_access_token', $token)-&gt;first();
          if (!$user)
              return view('public.error', ['errorCode' =&gt; 901]);
  
          if ($user-&gt;mobile_access_expires &lt; Carbon::now())
              return view('public.error', ['errorCode' =&gt; 902]);
  
           // I tried with and without the second param (true)
          if (!Auth::loginUsingId($user-&gt;id, true))
              return view('public.error', ['errorCode' =&gt; 903]);
  
          // My code makes it to here just fine which means the token
          // was valid and I 
          // successfully logged in.
          // Now I want to send them to a page where the user
          // can browse and remain logged in. 
  
          // I tried the following line but it does not work
          // It just immediately redirects me to the login page
          return redirect(url('dashboard'));
  
          // The following line works but if user clicks a link
          // to go to any other page it redirects them to the login page 
          return view('console.dashboard.dashboard');
  
      } catch (\Exception $e) {
          return view('public.error', ['errorCode' =&gt; 900, 'errorMessage' =&gt; $e-&gt;getMessage()]);
      }
  }
  </code></pre>
  
  <p>and in the web.php route file I have the following:</p>
  
  <pre><code>Route::group(['middleware' =&gt; 'auth'], function() {
      Route::get('dashboard', ['uses' =&gt; 'DashboardController@getDashboard']);
  });
  </code></pre>
  
  <p>and of course for testing I am typing something like this in my browser:</p>
  
  <pre><code>example.com/api/mobile/requestaccess?token=abc
  </code></pre>
  
  <p>Everything is all in one domain I am just wanting to log the user in and have them stay logged in while browsing the site.</p>
  
  <p>Any help will be greatly appreciated.</p>