Search code examples
phplaravelauthenticationlaravel-airlock

Laravel Airlock route for authenticated + unauthenticated


I'm using the new Laravel Airlock package for an app I'm building. I have a route that can be accessible for authenticated and unauthenticated users.

In the controller I check if the user is authenticated with Auth::check();. My route looks like this:

Route::get('post', [PostController::class, 'index'])->name('posts');

The problem is that if I make a request with an authenticated user (with a bearer token), I can't get the authenticated user in the controller (no idea why). And if I add the auth:airlock to the route, it's only accessible to authenticated users (not want I want).

How can I make sure I get the authenticated user in my controller if there is one ?


Solution

  • Try

    if (Auth::guard('airlock')->check()) {
        $user = Auth::guard('airlock')->user();
    }
    

    If it does not work at first, try to specify the airlock guard in your /config/auth.php file.