簡體   English   中英

Laravel Auth::attempt 寫入憑據后,將我重定向到登錄

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

在輸入 email 和密碼后,它會重定向到登錄頁面並且不再繼續。 我使用了“Authenticated”中間件,這是我定制的中間件。

Web.php

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

    Route::get('/', [App\Http\Controllers\AuthenticationController::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');
        }

    }

中間件

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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM