Skip to content
Advertisement

Laravel Auth::attempt after writing credentials, redirects me to login

After typing email and password it redirects to the login page and do not proceeds further. I have used ‘Authenticated’ middleware, a middleware that I customized.

Web.php

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

    Route::get('/', [AppHttpControllersAuthenticationController::class, 'dashboard'])->name('dashboard');

}

Controller

    $email = $request->email;
        $password = $request->password;

        $user = User::select('id', 'email', 'password')->where('email', '=', $email)->first();

        if ($user) {
            if (!Hash::check($password, $user->password)) {
                return redirect()->back()->withInput()->with('error', 'Email and password do not match');
            } else {
                $credentials = $request->only('email', 'password');
                if (Auth::attempt($credentials)) {

                    return redirect()->intended(route('dashboard'));
                }
            }
        } else {
            return redirect()->back()->with('error', 'Email does not exists');
        }

    }

Middleware

public function handle($request, Closure $next)
{

    if (!Auth::check()) {
        return redirect()->guest('login');
    }
    else{
        return $next($request);
    }

}

Advertisement

Answer

Answer:

In laravel 8 this line be implemented in the :

$request->session()->regenerate()

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement