[英]Laravel5: Auth::attempt not working as expected
我已經閱讀了有關身份驗證的Laravel 5文檔,但無法按照說明進行登錄。
我的用戶表同時包含用戶名和密碼(這是TEXT類型)字段,我存儲了一個哈希密碼,例如$2y$10$XCyEOyvC6Yp/O6HaeemPheO4KV1I8aEMUytZZt77Yjw9hp/j6uivWnope
這是我的代碼:
public function validateLogin()
{
$email = Input::get('email');
$pass = Input::get('password');
$pass = Hash::make($pass);
$credentials = [
'username' => $email,
'password' => $pass
];
if (Auth::attempt($credentials)) {
// Authentication passed...
return "passed";
}else{
return "nope";
}
}
我嘗試了任何操作, Auth::attempt
總是返回false :( Imo,這與文檔中所述完全一樣,有人知道這是怎么回事嗎?謝謝
您可能不需要自己進行$pass = Hash::make($password)
調用。
嘗試刪除該行,看看是否可行。
從文檔:
<?php
namespace App\Http\Controllers;
use Auth;
use Illuminate\Routing\Controller;
class AuthController extends Controller
{
/**
* Handle an authentication attempt.
*
* @return Response
*/
public function authenticate()
{
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('dashboard');
}
}
}
try方法接受鍵/值對的數組作為其第一個參數。 數組中的值將用於在數據庫表中查找用戶。 因此,在上面的示例中,將通過email列的值來檢索用戶。 如果找到了用戶,則將存儲在數據庫中的哈希密碼與通過array傳遞給方法的哈希密碼值進行比較 。
有關更多參考,請檢查:
http://laravel.com/docs/master/authentication#authenticating-users
您是否在AuthController中編寫了函數? 或任何新的/其他控制器中?
如果您在其他控制器中編寫了該函數,請確保已在頂部添加了此代碼
use App\User;
use Validator;
use App\Http\Controllers\Controller;
use Illuminate\Foundation\Auth\ThrottlesLogins;
use Illuminate\Foundation\Auth\AuthenticatesAndRegistersUsers;
public function login()
{
$email = Input::get('email');
$password = Input::get('password');
if (Auth::attempt(['email' => $email, 'password' => $password])) {
// Authentication passed...
return redirect()->intended('dashboard');
}
else{
return redirect()->intended('admin/login');
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.