Skip to content

Guard possible not being correctly loaded from config file #1616

@rickgoemans

Description

@rickgoemans
  • Passport Version: 11.5.0
  • Laravel Version: 9.47.0
  • PHP Version: 8.1.10
  • Database Driver & Version: MySQL 5.7.34

Description:

While using a different guard than the default version in config/auth.php, the request cannot resolve the user during authorization.

Steps To Reproduce:

We are using multiple guards and try to achieve a Authorization Code Grant with PKCE.

Here's out auth config (simplified):

<?php

return [
  'defaults' => [
    'guard'     => 'web',
    'passwords' => 'users',
  ],
  
  'guards' => [
    'web' => [
      'driver'   => 'session',
      'provider' => 'users',
    ],
    
    'web_new' => [
      'driver'   => 'session',
      'provider' => 'outsmart_users',
    ],
    
    'api' => [
      'driver'   => 'passport',
      'provider' => 'outsmart_users',
    ],
  ],
  'providers' => [
    'users' => [
      'driver' => 'eloquent',
      'model'  => Customer::class,
    ],
    'outsmart_users' => [
      'driver' => 'outsmart_users',
      'model'  => User::class,
    ],
  ],
];

We have web as default but we use web_new for the Laravel Passport. So we defined the guard in config/passport.php to be web_new, but we keep getting an error on the Laravel\Passport\Http\Controllers\AuthorizationController on line 102 which states:

return $this->approveRequest($authRequest, $user);

Which is caused by line 97:

$user = $request->user();

Because that ends up null.

Therefore I've tried to add some the following logging:

info('Current driver: ', [Auth::getDefaultDriver()]); // Current driver: ['web']
info('Current user provider: ', [Auth::getDefaultUserProvider()]); // Current user provider: [null]

info('(G) User: ', [$this->guard->user()?->toArray()]); // (G) User: [{id: 1, ...}] 
info('(R) User: ', [$request->user()?->toArray()]); // (R) user: [null]

As you can see the $this->guard->user() resolves correctly but the $request->user() does not. Also the current driver states web instead of web_new.

This is our (simplified) controller processing the login request (after being redirected to the view page with a form due to an unauthenticated exception) does this:

<?php

class AuthController extends Controller {
  public function login(Request $request): RedirectResponse
  {
    $guardName = 'web_new';
    
    if (!Auth::guard($guardName)->attempt($request->only('email', 'password'))) {
      return back()
        ->withInput()
        ->withErrors([
          '_general' => 'Invalid credentials',
        ]);
    }
    
    return redirect()
      ->intended();
  }
}

Am I misconfiguring something or is there something broken related to multiple guards?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions