I don't know if you copy pasted your code from your editor to the question, but in your providers.chains.model App\Models\chain::class should be App\Models\Chain::class, it's case sensitive, maybe update it and test again?
Aug 18, 2022
3
Level 7
Auth Attempt with guard passes but doesnt login
I created a new model "Chains" to which I extend it as Authen . I created Guards and Providers. When I attempt to login it using Auth::guard('web_chain')->attempt($credentials) it passes but doesnt login the user?
Chains.php (model):
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Auth\User as Authenticatable;
class Chain extends Authenticatable
{
/**
* 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',
];
}
Auth (config):
'guards' => [
'web' => [
'driver' => 'session',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
'hash' => false,
],
'web_chain' => [
'driver' => 'session',
'provider' => 'chains',
],
],
'providers' => [
'users' => [
'driver' => 'eloquent',
'model' => App\Models\User::class,
],
'chains' => [
'driver' => 'eloquent',
'model' => App\Models\chain::class,
],
],
Login using a Livewire component:
public function login()
{
$credentials = $this->validate([
'email' => 'required|email',
'password' => 'required',
]);
if (Auth::guard('web_chain')->attempt($credentials)) {
return redirect('/wholesale/dashboard');
} else {
$this->dispatchBrowserEvent('notify', 'Email and password are wrong.');
}
}
I even tried loggin in a user like this, which works but also logs in the first user in the table Users as well?
if(Auth::guard('web_chain')->attempt($credentials)) {
$user = Chain::where('email', '=', $credentials['email'])->first();
Auth::login($user);
return redirect('/wholesale/dashboard');
}
Level 37
1 like
Please or to participate in this conversation.