mercredi 27 juin 2018

FatalThrowableError when using custom guard for api authentication in laravel

I keep on getting a FatalThrowableError exception each time i try to authenticate via token or passport drivers in my custom guard (api-users) however with a session driver it works just fine. I have defined the guards as per the docs but still to no avail, I don't understand where I'm going wrong

Code

Config\Auth.php

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

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

    'api' => [ <== this is not being used as far as i know
        'driver' => 'token',
        'provider' => 'users',
    ],

    'api-users' => [  //<== I am trying to use this guard 
        'driver' => 'session', //<== session works, token or passport doesn't
        'provider' => 'api-users',
    ],
],

'providers' => [
    'users' => [
        'driver' => 'eloquent',
        'model' => App\Models\User::class,
    ],

    'api-users' => [
        'driver' => 'eloquent',
        'model' => App\Models\ApiUser::class,
    ],
],

AuthController.php

class AuthController extends Controller
{
    protected function guard()
    {
        return Auth::guard('api');
    }

    protected function attemptLogin(Request $request)
    {
        return $this->guard()->attempt($this->credentials($request), false);
    }

    protected function credentials(Request $request)
    {
        return $request->only('email', 'password');
    }

    public function login(Request $request)
    {
        if ($this->attemptLogin($request)) { //<== this doesn't work with token/passport driver but with a session driver
            echo 'success';
        } else {
            echo "fail";
        }
    }
}

Models\ApiUser.php

class ApiUser extends Authenticatable
{
    use SoftDeletes;
    use Notifiable;
    use HasApiTokens;

    protected $guard = 'api-users';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'mobile', 'password', 'active'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = ['password', 'remember_token', 'created_at', 'updated_at', 'deleted_at'];

    /**
     * Type cast the active field
     */
    protected $casts = [
        'active' => 'boolean',
    ];
}



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2KpxvYC
via IFTTT

Aucun commentaire:

Enregistrer un commentaire