Search code examples
phplaravelauthenticationlaravel-5.3guard

Laravel 5.3 get attempt true but Auth::User return null?


As the title said, in the VendorController postLogin method dd(Auth::user()) get the value null, but the $attempt get the result true, I can't get the data that has been attempt.

My routes/web.php

Route::group(['prefix' => 'vendor','as' => 'Vendor.'], function () {
        Route::get('login', 'VendorController@login')->name('Login');
        Route::post('login', 'VendorController@postLogin')->name('Post.Login');
});

My VendorController:

  public function postLogin(Request $request)
  {
    $attempt = Auth::guard('vendor')->attempt([
      'acctId'   => $request->acctId,
      'password' => $request->password
    ]);

    if($attempt){
      dd(Auth::user());
    }
  }

My Vendor Model

namespace ACME\Entities;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
class Vendor extends Model implements Authenticatable
{
    use AuthenticableTrait;

    protected $table = 'vendor';
    protected $primaryKey = 'id';
    public $timestamps = true;
    protected $fillable = [
        'acctId',
        'password',
        'active',
        'remark'
    ];
    protected $guarded = ['password'];
}

My config/auth.php

return [

    'defaults' => [
        // 'guard' => 'web',
        // 'passwords' => 'users',
        'guard' => 'user',
        'passwords' => 'users',
    ],

    'guards' => [
        // 'web' => [
        //     'driver' => 'session',
        //     'provider' => 'users',
        // ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],

        'user' => [
            'driver' => 'session',
            'provider' => 'users',
        ],

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],
        'vendor' => [
            'driver' => 'session',
            'provider' => 'vendors',
        ],
    ],
    'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => ACME\Entities\User::class,
        ],
        'admins' => [
            'driver' => 'eloquent',
            'model' => ACME\Entities\Admin::class,
        ],
        'vendors' => [
            'driver' => 'eloquent',
            'model' => ACME\Entities\Vendor::class,
        ],
    ],
    'passwords' => [
        'users' => [
            'provider' => 'users',
            'email' => 'auth.emails.password',
            'table' => 'user',
            'expire' => 60,
        ],
        'admins' => [
            'provider' => 'admins',
            'email' => 'auth.emails.password',
            'table' => 'admin',
            'expire' => 60,
        ],
        'vendors' => [
            'provider' => 'vendors',
            'email' => 'auth.emails.password',
            'table' => 'vendor',
            'expire' => 60,
        ],
    ],

];

Solution

  • Because you are using a custom driver and you are not using the Auth::shouldUse() to define which driver the method needs to use, you need to call authenticated user like this:

    Auth::guard('vendor')->user()
    

    In another solution you could use the Auth::shouldUse() method.

    public function postLogin(Request $request)
    {
        Auth::shouldUse('vendor');
    
        if(! $attempt = Auth::attempt([
          'acctId'   => $request->acctId,
          'password' => $request->password
        ])){
          //handle error if there is not login
        }
    
        dd(Auth::user());
    }
    

    By doing this you set the driver to use vendor.