vendredi 4 septembre 2020

Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() laravel 5.8

I create a project whit tow type of user one for admin one for the student using this link

Everything it's ok but when I want to make a request to my postLoging() method the just redirect to my getLogin() method(page refreshed) and I think it because of the auth protection

my web.php file:

Route::group(['prefix' => 'admin'], function (){
    Route::group(['middleware' => 'guest:admin'], function (){
        Route::get('/login', [
            'uses' => 'AdminController@getLogin',
            'as' => 'admin.login'
        ]);

        Route::post('/login', [
            'uses' => 'AdminController@postLogin',
            'as' => 'admin.login'
        ]);
    });

    Route::group(['middleware' => 'auth:admin'], function (){
        Route::get('/', [
            'uses' => 'AdminController@getIndex',
            'as' => 'admin.index'
        ]);
    });
});

and my AdminController file:

class AdminController extends Controller
{
    public function __construct(){
        $this->middleware('auth:admin')->except('getLogin');
    }

    public function getLogout(){
        Auth::logout();

        return redirect()->route('admin.login');
    }

    public function getLogin(){
        return view('admin.login');
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     * @throws \Illuminate\Validation\ValidationException
     */
    public function postLogin(Request $request){
        return $request;
        $this->validate($request, [
            'email' => 'required',
            'password' => 'required',
        ]);

        if(Auth::attempt(['email' => $request->input('email'), 'password' => $request->input('password')])){
            return redirect()->route('admin.index');
        }

        return redirect()->back()->with('merror', 'ایمیل یا پسورد وارد شده اشتباه است.');
    }

    public function getIndex(){
        return view('admin.index');
    }
}

in construct function, I tried:

when I set tow middleware like this

$this->middleware('auth:admin')->except('getLogin'); $this->middleware('auth:admin')->except('postLogin');

the app will break and the login page doesn't load.

when I try $args = array('getLogin', 'postLogin'); $this->middleware('auth:admin')->except($args ); and $this->middleware('auth:admin')->except('getLogin', 'postLogin'); I get this error

Argument 2 passed to Illuminate\Auth\SessionGuard::__construct() must implement interface Illuminate\Contracts\Auth\UserProvider, null given, called in C:\xampp\htdocs\admin\vendor\laravel\framework\src\Illuminate\Auth\AuthManager.php on line 125

config/auth.php file

    return [

    /*
    |--------------------------------------------------------------------------
    | Authentication Defaults
    |--------------------------------------------------------------------------
    |
    | This option controls the default authentication "guard" and password
    | reset options for your application. You may change these defaults
    | as required, but they're a perfect start for most applications.
    |
    */

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

    /*
    |--------------------------------------------------------------------------
    | Authentication Guards
    |--------------------------------------------------------------------------
    |
    | Next, you may define every authentication guard for your application.
    | Of course, a great default configuration has been defined for you
    | here which uses session storage and the Eloquent user provider.
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | Supported: "session", "token"
    |
    */

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

        'admin' => [
            'driver' => 'session',
            'provider' => 'admins',
        ],

        'student' => [
            'driver' => 'session',
            'provider' => 'students',
        ],

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

    /*
    |--------------------------------------------------------------------------
    | User Providers
    |--------------------------------------------------------------------------
    |
    | All authentication drivers have a user provider. This defines how the
    | users are actually retrieved out of your database or other storage
    | mechanisms used by this application to persist your user's data.
    |
    | If you have multiple user tables or models you may configure multiple
    | sources which represent each model / table. These sources may then
    | be assigned to any extra authentication guards you have defined.
    |
    | Supported: "database", "eloquent"
    |
    */

    'providers' => [
        'admins' => [
            'driver' => 'eloquent',
            'model' => App\Admin::class,
        ],

        'students' => [
            'driver' => 'eloquent',
            'model' => App\Student::class,
        ],

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

    /*
    |--------------------------------------------------------------------------
    | Resetting Passwords
    |--------------------------------------------------------------------------
    |
    | You may specify multiple password reset configurations if you have more
    | than one user table or model in the application and you want to have
    | separate password reset settings based on the specific user types.
    |
    | The expire time is the number of minutes that the reset token should be
    | considered valid. This security feature keeps tokens short-lived so
    | they have less time to be guessed. You may change this as needed.
    |
    */

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

];


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire