简体   繁体   中英

Laravel 5.2 Auth always redirect to login page (maybe session not persisting)

Edit: Problem "solved", I downgraded to 5.1.

I meet a problem with Laravel 5.2 since a moment:

  • Laravel's sessions (_token) work but mine not,
  • Auth always redirect to login page.

I looked for a solution on the Internet and I tried several but none works.

Here is my code:

adminController@login:

$userdata = array(
    'username' => $request->get('username'),
    'password' => $request->get('password'),
);

if(Auth::validate($userdata))
{
    if(Auth::attempt($userdata)) {
        return redirect()->intended('admin-dashboard');
    }else{
        return redirect('admin-login')->with('error', 'Nom d\'utilisateur ou mot de passe incorrect !');
    }
}else{
    return redirect('admin-login')->with('error', 'Nom d\'utilisateur ou mot de passe incorrect !');
}

Routes.php:

Route::group(['middleware' => 'web'], function () {

    Route::post('postAdminLogin', ['as' => 'postAdminLogin', 'uses' => 'adminController@login']);

    Route::group(['middleware' => 'auth'], function() {
        Route::get('admin-dashboard', ['as' => 'admindashboard', 'uses' => 'adminController@index']);
    });

});

MiddlewareGroups:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,

        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,

        \Illuminate\Session\Middleware\StartSession::class,

        \Illuminate\View\Middleware\ShareErrorsFromSession::class,

        \App\Http\Middleware\VerifyCsrfToken::class,

    ],

    'auth' => [
        \App\Http\Middleware\Authenticate::class,
    ],

    'api' => [
        'throttle:60,1',
    ],
];

adminController@index:

public function index(){
  if (Auth::check()) {
    return "ok";
  }
}

I would be grateful if one of you could help me.

I've had a similar problem where my permissions weren't set correctly for storate/framework/sessions/ (ie the application wasn't able to create the session file).

A general workaround for anyone that has similar issues could be changing your driver to database . The artisan automatically creates sessions schema when you run php artisan make:auth anyway.

Simply change config/session.php

'driver' => env('SESSION_DRIVER', 'driver'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM