微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。