Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

skeith22's avatar

Using custom guard and manual attempt to login causes `user` attribute to be null

I current have a 2nd guard called web-players, and when I manually attempt it to login and check for the user attribute it's blank

BUT notice how original attribute has the correct user values

Also #guard is wrong, it shouldn't be web but web-players.

App\Models\Player {#1609 ▼ // app/Http/Controllers/Players/AuthController.php:36
  #connection: "mysql"
  #table: "players"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  +preventsLazyLoading: false
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #escapeWhenCastingToString: false
  #attributes: array:11 [▶]
  #original: array:11 [▼
    "id" => 10
    "name" => "Kathleen Schuppe"
    "email" => "[email protected]"
    "email_verified_at" => "2022-11-04 06:58:05"
    "mobile_number" => "+1 (727) 648-7222"
    "password" => "yIXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi"
    "should_reset_password" => 1
    "remember_token" => "vlZyGWPncxuZojjS3Prsm6anPuQnwQZKg1Ojt1jr3yi7Wnmua0W9O8AFAbZR"
    "created_at" => "2022-11-04 06:58:05"
    "updated_at" => "2022-11-04 06:58:05"
    "deleted_at" => null
  ]
  #changes: []
  #casts: array:2 [▶]
  #classCastCache: []
  #attributeCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: []
  #touches: []
  +timestamps: true
  #hidden: array:2 [▶]
  #visible: []
  #fillable: array:4 [▶]
  #guarded: array:1 [▶]
  #rememberTokenName: "remember_token"
  -checkRoles: false
  -checkPermissions: false
  #accessToken: null
  #guard: "web"                  <<<<<<<<<<<<<<< This should also be web-players guard
  #user: null                     <<<<<<<<<<<<<<<  REFERRING TO THIS
  #forceDeleting: false

Code to authenticate

$credentials = $request->validate([
    'email' => ['required', 'email'],
    'password' => ['required'],
]);

if (Auth::guard('web-players')->attempt($credentials)) {
    $player = Player::where('email', $request->email)->first();

    Auth::guard('web-players')->login($player);

    dd(Auth::guard('web-players')->user())
}

GUARDS

'guards' => [
    'web-admins' => [
        'driver' => 'session',
        'provider' => 'admins',
    ],

    'web-players' => [
        'driver' => 'session',
        'provider' => 'players',
    ],
}

Should the guard and user have a different value or this is expected?

0 likes
3 replies
skeith22's avatar

@bobbybouwmann these are the providers in config/auth.php

    'providers' => [
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Models\Admin::class,
        ],

        'players' => [
            'driver' => 'eloquent',
            'model' => App\Models\Player::class,
        ],
    ],

    'passwords' => [
        'admins' => [
            'provider' => 'admins',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],

        'players' => [
            'provider' => 'players',
            'table' => 'password_resets',
            'expire' => 60,
            'throttle' => 60,
        ],
    ],

Please or to participate in this conversation.