Laravel Auth::attempt 写入凭据后,将我重定向到登录

问题描述

输入电子邮件和密码后,它会重定向登录页面,不再继续。 我使用了“Authenticated”中间件,这是我自定义的中间件。

Web.PHP

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

    Route::get('/',[App\Http\Controllers\AuthenticationController::class,'dashboard'])->name('dashboard');

}

控制器

    $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');
        }

    }

中间件

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

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

}

解决方法

您的登录逻辑似乎不太好。

这个怎么样:

$credentials = $request->only('email','password');

if (Auth::attempt($credentials)) {
   return redirect()->intended(route('dashboard'));
}else{
    return redirect()
      ->back()
      ->withInput()
      ->with('error','Email and password do not match');
}
,

您是否检查过您的浏览器有关 Session/Cookie 的任何警告?

我记得有一个类似的问题,总是让我回到登录。 原来我在 .env 上有错误的域

SESSION_DOMAIN=other.domain.com

如果您使用 HTTP,也值得检查 SESSION_SECURE_COOKIE 的值

,

答案:

在 Laravel 8 中,这一行在 :

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