Laravel JWT Auth fetch user if token
<p>I'm using the <a href="https://github.com/tymondesigns/jwt-auth" rel="nofollow">jwt-auth</a> package for Laravel. It's working great, but it seems that a user has to be authenticated or not.</p>

<p>For instance some routes do not require authentication, but if the token is present it should still authenticate the user. The parameters I display to user from API can vary based on the type of users access. So admins will get some additional parameters.</p>

<p>Right now it will always just throw token absent. But it should go through as normal and "IF" token is present, process it.</p>

<p>Not sure if I need to create a custom middleware for this.</p>

<pre><code>class JWTAuthIfPresent
{
    public function handle($request, Closure $next)
    {
        if (JWTAuth::getToken()) {
            JWTAuth::parseToken()-&gt;authenticate();
        }

        return $next($request);
    }
}
</code></pre>

<p>This seems to work, but not sure if there is a better way or something already in the existing package.</p>

<p><strong>EDIT:</strong></p>

<p>Also this will not deal with any token issues, like <code>token=</code> or some invalid or expired token. If set it should still process the token normally.</p>