11
votes

In my LoginController under Auth, I have used the following codes:

namespace App\Http\Controllers\Auth;

use App\Model\Admin;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\AuthenticatesUsers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\Redirect;
use Hash;
use Auth;
use DB;
use App\Model\UserAdmin;

class LoginController extends Controller {
use AuthenticatesUsers;
public function __construct() {
        $this->middleware('guest')->except('logout');
    }

public function doLogin(Request $request) {
$userdata = array(
            'email' => Input::get('email'),
            'password' => Input::get('password'),
            'status' => '1',
        );
if (Auth::guard('admin')->attempt($userdata)) {
  return Redirect::intended('/administrator/dashboard')->with('successMessage', 'You have successfully logged in.');
}
}
}

And in UserAdmin (model) under app/Model is as follows:

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Config;
class UserAdmin extends Authenticatable {
protected $table = 'adminusers';
    public $timestamps = false;
    protected $fillable = ['firstName', 'lastName', 'email', 'company', 'website'];

    public function __construct() {
        parent::__construct(); // Don't forget this, you'll never know what's being done in the constructor of the parent class you extended
    }

}

After submitting the login details, it shows me the error:

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

7
@Vishal - I did exactly the same as the link you mentioned above, but nothing happen. I dd('XX'); after Auth::guard('admin')->attempt($userdata) but dd is not working.Niladri Banerjee - Uttarpara

7 Answers

31
votes

I suppose that you required to add implements \Illuminate\Contracts\Auth\Authenticatable to your UserAdmin model class definition.

    class UserAdmin extends Model implements 
    \Illuminate\Contracts\Auth\Authenticatable
7
votes

You must use Authenticatable in User model

for example:

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
  //your code
}
2
votes

You must declared use AuthenticableTrait for Authenticatable interface.

For example :

use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Auth\Authenticatable as AuthenticableTrait;
use Illuminate\Database\Eloquent\Model;

class Company extends Model implements Authenticatable
{
use AuthenticableTrait;
1
votes

Try and run 'composer dump-autoload' to check for "ambiguous User class resolution". It is likely you have two classes Defined as User Class.

1
votes

use Illuminate\Foundation\Auth\AuthenticatesUsers;

Then in your model class, extends AuthenticatesUsers instead of Model.

0
votes

You must extends Authenticatable class and implements JWTSubject in User model

For example :

class User extends Authenticatable implements JWTSubject {
0
votes

Go to your Model and instead of extending Model, extend User

<?php

namespace App;


class Staff extends \Illuminate\Foundation\Auth\User
{
}