3
votes

I'm trying to create multi login in laravel 5.6 and this error appeared, can anyone help me?

Symfony \ Component \ Debug \ Exception \ FatalThrowableError (E_RECOVERABLE_ERROR) Type error: Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must be an instance of Illuminate\Contracts\Auth\UserProvider, null given, called in C:\wamp64\www\Laravel\Sistema\oficial\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 123

<?php
namespace App\Http\Controllers\Auth;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Route;
use Illuminate\Support\Facades\Auth;

class EmployeeLoginController extends Controller
{

    public function __construct()
    {
         $this->middleware('auth:employee');
    }
    
    public function login(Request $request)
    {
      $credentials = $request->only('email', 'password');
      if (Auth::guard('employee')->attempt($credentials)) {
        return redirect()->intended(route('admin.dashboard'));
      }
      return redirect()->back()->withInput($request->only('email', 'remember'));
    }
}

guard:

<?php

return [

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


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

        'employee' => [
            'driver' => 'session',
            'provider' => 'employees',
        ],

        'api' => [
            'driver' => 'token',
            'provider' => 'users',
        ],
    ],



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

        'employee' => [
            'driver' => 'eloquent',
            'model' => App\Employee::class,
        ],


        // 'users' => [
        //     'driver' => 'database',
        //     'table' => 'users',
        // ],
    ],


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

];

ReditectIfAuthenticated

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\Auth;

class RedirectIfAuthenticated
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @param  string|null  $guard
     * @return mixed
     */
    public function handle($request, Closure $next, $guard = null)
    {
        switch ($guard) {
        case 'employee':
            if (Auth::guard($guard)->check()) {
              return redirect('/dashboardemployee');
          }

          break;
        default:
            if (Auth::guard($guard)->check()) {
            return redirect('/home');
          }

          break;
      }

        return $next($request);
    }
}

model:

<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;

class Employee extends Authenticatable
{
    protected $guard = 'employee';

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'photo',
        'status',
        'connect_email',
        'connect_senha',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];
   }
1
Show us the code from where the authentication actually happens (probably some controller) - Felippe Duarte
@FelippeDuarte added in post - João Leandro L. Santos
You created a custom employee guard, did you made the proper configurations? laravel.com/docs/5.6/authentication - Felippe Duarte
@FelippeDuarte added in post - João Leandro L. Santos

1 Answers

17
votes

The provider is named wrong:

'employee' => [
        'driver' => 'eloquent',
        'model' => App\Employee::class,
    ],

should be

'employees' => [
    'driver' => 'eloquent',
    'model' => App\Employee::class,
],