Laravel: Argument 2 passed to Illuminate\Auth\SessionGuard::__construct()
Asked Answered
G

2

15

I have a application where I have created a new guard called residents which is the exact copy of the default User class that comes with the laravel. But when i change the guard it start showing this error:
Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in ...\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 125

I tried googling for the solution and read many but nothing worked.

Laravel version is 5.8

auth.php

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

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

    'api' => [
        'driver' => 'token',
        'provider' => 'residents',
        'hash' => false,
    ],
],

'providers' => [
    'residents' => [
        'driver' => 'eloquent',
        'model' => App\ApiModels\Resident::class,
    ],
],

'passwords' => [
    'users' => [
        'provider' => 'users',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],

App\ApiModels\Resident.php

namespace App\ApiModels;

use \Illuminate\Notifications\Notifiable;
use \Illuminate\Contracts\Auth\MustVerifyEmail;
use \Illuminate\Foundation\Auth\User as Authenticatable;

class Resident extends Authenticatable
{
    use Notifiable;

    /**
     * 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',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * Authenticate the user
     *
     * @param   object  $request 
     * @return  array
     */
    public function _login($request)
    {
        if(\Auth::attempt([
            'email'    => $request->email,
            'password' => $request->password
        ]))
        {
            return [
                'success' => true
            ];
        }
        else
        {
            return [
                'success' => false
            ];
        }
    }
}

route\api.php

Route::get('/login', 'Api\ResidentsController@login');

Api\ResidentsController.php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

/**
 * Models
 */
use App\ApiModels\Resident;

class ResidentsController extends Controller
{
    public function __construct()
    {
        /**
         * Middleware(s)
         */
        $this->middleware('auth:api', [
            'except' => [
                'login',
                'logout'
            ]
        ]);
    }
    
    public function login(Request $request)
    {
        ...

        return response()->json([
            'login_id' => $request->login_id,
            'password' => $request->password
        ]);
    }
}
Gianina answered 11/9, 2019 at 11:53 Comment(0)
P
30

The problem occurs in your config/auth.php. Your web guard is using the users provider which is not declared in your providers array. You have 2 options to fix this (choose one):

  1. Add a users provider
  2. If you're planning NOT to use the web guard, remove it instead then make your default guard to api:
'defaults' => [
    'guard' => 'api',
    'passwords' => 'residents',
],

'guards' => [
    'api' => [
        'driver' => 'token',
        'provider' => 'residents',
        'hash' => false,
    ],
],

'providers' => [
    'residents' => [
        'driver' => 'eloquent',
        'model' => App\ApiModels\Resident::class,
    ],
],

'passwords' => [
    'residents' => [
        'provider' => 'residents',
        'table' => 'password_resets',
        'expire' => 60,
    ],
],
Polyhedron answered 11/9, 2019 at 12:39 Comment(0)
A
0

You Must use this for web guard:

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

Or

you can remove web guard:

Anabranch answered 5/6, 2022 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.