Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel authentication with new model & guard fails: Undefined index: model

I'm trying to authenticate my Laravel application (5.8) with an additional model & guard. The problem, I receive a "Undefined index: model" error during the following login approach. Any ideas what i'm doing wrong? I've used this integration in an 5.7 Version of Laravel and it worked there without any problems.

 auth()->guard('partner')->login($partner);

enter image description here

CodeSnippets:

Partner Model (additional Settings)

class Partner extends Authenticatable  {

protected $guard = 'partner';

    public function getRouteKeyName()
    {
        return 'uuid';
    }

}

Guards (config.auth.php)

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

        'partner' => [
            'driver' => 'session',
            'provider' => 'partners',
        ],
],

Providers (config.auth.php)

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

    'partners' => [
        'driver' => 'eloquent',
        'table' => \App\Models\Partner::class,
    ],
],

Middleware Gorup (kernel.php)

protected $middlewareGroups = [

        'partner' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            //\Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
        ],

];

RouteServiceProvider

  protected function mapPartnerRoutes()
    {
        Route::prefix('partner')
            ->middleware(['partner'])
            ->namespace($this->namespace)
            ->group(base_path('routes/partner.php'));

    }

Application frames Error

enter image description here

like image 724
Stan Barrows Avatar asked Mar 22 '19 23:03

Stan Barrows


People also ask

How do I change my auth model in Laravel?

Because we need to change the Auth config file (you can find it in config/auth. php), and change the users provider model from “User::class” to “YourNewModel::class”, in this case “Administrator::class”. Using the Administrator model. * Get a validator for an incoming registration request.

How do I add authentication to Laravel?

Just run php artisan make:auth and php artisan migrate in a fresh Laravel application. Then, navigate your browser to http://your-app.test/register or any other URL that is assigned to your application. These two commands will take care of scaffolding your entire authentication system!


1 Answers

I think you miss to configure a model in your partners auth provider, i.e.:

'partners' => [
    'driver' => 'eloquent',
    //'table' => \App\Models\Partner::class,
    'model' => \App\Models\Partner::class,
],
like image 180
dparoli Avatar answered Nov 15 '22 07:11

dparoli