title: How to optionally authenticate with Laravel Sanctum?
body: <p>I'm using <a href="https://laravel.com/docs/8.x/sanctum" rel="nofollow noreferrer">Laravel Sanctum</a> to authenticate users. I'd like to have a route that can be accessed by guests and logged in users. Logged in users send an API Token in the Authorization header.</p>
  <p>I've tried making a route without authentication, but that way I can't see the logged in user.</p>
  <pre><code>Route::get('noauth', function() {
    return Auth::check();
  });
  </code></pre>
  <p><code>GET /noauth with auth header returns false, user is not logged in</code><br>
  <code>GET /noauth without auth header returns false, user is not logged in</code></p>
  <p>I've also tried using <code>auth:sanctum</code> middleware, but that way guests can't access the page.</p>
  <pre><code>Route::get('yesauth', function() {
    return Auth::check();
  })-&gt;middleware('auth:sanctum');
  </code></pre>
  <p><code>GET /yesauth with auth header returns true, the user is logged in</code><br>
  <code>GET /yesauth withouth auth header returns 401, unauthorized</code></p>
  <p>The solution should return true with auth headers, and false without auth headers.</p>