I have an application where I want to have authentication but not use User models. The model is called Karyawan. But when I change the auth.php it starts showing this error:
Illuminate\Auth\EloquentUserProvider::validateCredentials(): Argument #1 ($user) must be of type Illuminate\Contracts\Auth\Authenticatable, App\Models\Karyawan given, called in C:\KKP\Project_KKP\vendor\laravel\framework\src\Illuminate\Auth\SessionGuard.php on line 426
Laravel version is 8
auth.php
'defaults' => [
'guard' => 'web',
'passwords' => 'users',
],
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\Karyawan::class,
],
],
'passwords' => [
'users' => [
'provider' => 'users',
'table' => 'password_resets',
'expire' => 60,
'throttle' => 60,
],
],
App\Models\Karyawan.php
namespace App\Models;
use App\Models\Cuti;
use App\Models\Role;
use App\Models\Divisi;
use App\Models\Jabatan;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Karyawan extends Model
{
use HasFactory;
protected $guarded = ['karyawan_id'];
protected $table = 'karyawans';
public function scopeSearch($query, array $searchs) {
$query->when($searchs['search'] ?? false, function($query, $search) {
return $query->where('nama', 'like', '%' . $search . '%')
->orWhere('divisi', 'like', '%' . $search . '%')
->orWhere('jabatan', 'like', '%' . $search . '%')
->orWhere('agama', 'like', '%' . $search . '%')
->orWhere('nik', 'like', '%' . $search . '%');
});
}
public function role_id()
{
return $this->belongsTo(Role::class);
}
public function divisi()
{
return $this->belongsTo(Divisi::class);
}
public function jabatan()
{
return $this->belongsTo(Jabatan::class);
}
public function cuti()
{
return $this->hasMany(Cuti::class);
}
}
web.php
Route::get('/', [BasicLoginController::class, 'login']);
Route::post('/', [BasicLoginController::class, 'authenticate']);
BasicLoginController.php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class BasicLoginController extends Controller
{
public function login()
{
return view ('login.login', [
'title' => 'Login'
]);
}
public function authenticate(Request $request) {
$credentials = $request->validate([
'email' => 'required|email:dns',
'password' => 'required'
]);
if(Auth::attempt($credentials)) {
$request->session()->regenerate();
return redirect()->intended('/dashboard');
}
return back()->with('loginError', 'Email atau Password salah');
}
}
CodePudding user response:
as the error says, your class need to implement the interface Illuminate\Contracts\Auth\Authenticatable
which the common user model do by extending the class Illuminate\Foundation\Auth\User as Authenticatable;
So either implement the contract in your class and declare all needed method functionality in it.
OR
Just extend the existing Illuminate\Foundation\Auth\User
namespace App\Models;
//...
use Illuminate\Foundation\Auth\User as Authenticatable;
class Karyawan extends Authenticatable
{
The class Illuminate\Foundation\Auth\User
extends the model class Illuminate\Database\Eloquent\Model
so the model functionalities will still be available.
CodePudding user response:
Class Karyawan needs to implement Illuminate\Contracts\Auth\Authenticatableclass
interface.
Or you can extend Illuminate\Foundation\Auth\User in your Karyawan Class.