title: Laravel 5.6 - How to get auth()->user() or $response->user() in api controller?
body: <p>In <code>api.php</code> routes file below, there are public routes and private routes:</p>
  
  <pre><code>Route::group(['namespace' =&gt; 'API'], function() {
  
       // Public routes (auth not required)
       Route::group([], function() {
           Route::get('/testauth1', 'TestController@testauth1');
           // more public routes...
       });
  
       // Private routes (auth required)
       Route::group(['middleware' =&gt; 'auth:api'], function() {
           Route::get('/testauth2', 'TestController@testauth2');
           // more private routes...
       });
  
  });
  </code></pre>
  
  <p>In the <code>TestContoller</code> these are the 2 methods  called above:</p>
  
  <pre><code>class TestController extends Controller {
  
      public function testauth1(\Request $request) {
        // return auth()-&gt;user(); // does not return user
        return $request-&gt;user(); // does not return user
      }
  
      public function testauth2() {
        return auth()-&gt;user(); // returns user
      }
  
  }
  </code></pre>
  
  <p>Since the private route group has the <code>auth:api</code> middleware, we will ensure the user is authenticated by checking the token supplied in the <code>Authorization Bearer</code> header. Only if a valid token is present will the private routes be rendered to the authenticated user. This is why <code>TestController@testauth2</code> returns the auth user correctly.</p>
  
  <p>Now, anyone can access the public routes, with or without token. If there is no token supplied in the <code>Authorization Bearer</code> header, then we'll have no authenticated user, which makes sense. This is why <code>TestController@testauth1</code> does not return an auth user. However, when a logged in user accesses <code>/testauth1</code> public route, they provide their token in the <code>Authorization Bearer</code> header and therefore should be returned in <code>TestController@testauth1</code> if not with <code>auth()-&gt;user()</code> at least with the <code>$request-&gt;user()</code> but we can't seem to access the user with their supplied token in that method.</p>
  
  <p>Any idea how we can access the valid token user in all public route methods?</p>