Search code examples
laravel-5.7laravel-authentication

Trying Multiple User Auth, it keep saying wrong instance of argument passed


I'm getting this error while trying to log in multiple users with guards and unable to understand what instance it needs to be passed:

Argument 1 passed to 
Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an 
instance of Illuminate\Contracts\Auth\Authenticatable, instance of 
App\Employs given, called in /var/www/html/crmproject/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 379

This is my Auth Controller:

<?php 
namespace App\Http\Controllers\Auth;

use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;

class EmploysLoginController  extends Controller
{
 use AuthenticatesUsers;

 protected $guard = 'Employs';
/**

 * Where to redirect users after login.

 *

 * @var string

 */

protected $redirectTo = '/Employs';

/**

 * Create a new controller instance.

 *

 * @return void

 */

public function __construct()
{
    $this->middleware('guest')->except('logout');
}

public function showLoginForm()
{
    return view('auth.employe-login');
}

public function login(Request $request)
{

    if (auth()->guard('Employs')->attempt(['email' => $request->email, 'password' => $request->password])) {

        dd(auth()->guard('Employs')->user());
    }

    return back()->withErrors(['email' => 'Email or password are wrong.']);
 }
}

This is my Model:

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Authenticatable;
// use Illuminate\Contracts\Auth\Authenticatable as 
AuthenticatableContract;

class Employs extends Model// implements AuthenticatableContract 
{
  protected $primaryKey = 'employ_id';
}

i tried many solution provided online/stackoverflow but i'm constantly getting this error, and if you find this question has ambiguity please ask before doing down vote i'm trying this out last time here.


Solution

  • You should create a model like this:

    Model

        <?php
    
            namespace App;
    
            use Illuminate\Notifications\Notifiable;
            use Illuminate\Foundation\Auth\User as Authenticatable;
    
            class Employs extends Authenticatable
            {
                use Notifiable;
    
                protected $guard = 'Employs';
    
                /**
                 * The attributes that are mass assignable.
                 *
                 * @var array
                 */
                protected $fillable = [
                    'name', 'email', 'password',
                ];
    
                /**
                 * The attributes that should be hidden for arrays.
                 *
                 * @var array
                 */
                protected $hidden = [
                    'password', 'remember_token',
                ];
            }
    

    I hope this work for you.