Nvan
Nvan

Reputation: 1146

Auth::check() not persisting Laravel 5.2

I'm facing a problem with laravel 5.2,

 public function authenticate()
    {
        $user = Auth::attempt(['email' => Input::get('email'), 'password' => Input::get('password')]);
        if (Auth::check()) {
            return redirect()->intended('default');
        } else {
            return 'wrong password or mail';
        }
    }

In the authenticate() function, Auth::check() is true. But then in my Autthenticate Middleware it return false...

public function handle($request, Closure $next, $guard = null)
{
    if (!Auth::check()) { //Here Auth::check() is false
        return redirect()->guest('auth/login');
    }
    return $next($request);
}

I can't figure what the problem is or what i am doing wrong... I tried using Sessions but neither data doesn't persist.

Thanks

Upvotes: 2

Views: 316

Answers (3)

komi2
komi2

Reputation: 1

If you set your middleware to $middleware in Kernel.php, you should try set to $middlewareGroups.

Upvotes: 0

Parvez Rahaman
Parvez Rahaman

Reputation: 25

In your middleware instead of Auth::check() use Auth::guard()

public function handle($request, Closure $next, $guard = null)
{
    if(Auth::guard($guard)->guest()) {
        return redirect()->guest('auth/login');
    }
    return $next($request);
}

Please let me know if it doesn't work.

Upvotes: 1

Pankaj
Pankaj

Reputation: 10095

I did some changes in Kernel.php.

Original Code.

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];

Modified To

protected $middleware = [
    \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
];

Upvotes: 2

Related Questions