我已经阅读了关于认证的Laravel 5文档,但无法按照描述的方式登录。
我的用户表包含两个字段,用户名和密码(这是文本类型),我已经存储了一个散列密码,如$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:(在我看来,这和文档中描述的完全一样,有人知道哪里出错了吗?由于
你可能不需要自己做$pass = Hash::make($password)
调用。
试着删除这一行看看是否有效。
From the docs:
<?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');
}
}
}
尝试方法接受一个键/值对数组作为第一个论点。数组中的值将用于查找用户您的数据库表。因此,在上面的示例中,用户将是通过email列的值检索。如果用户被找到,存储在数据库中的散列密码将与通过数组传递给方法的散列密码值。
进一步参考查询:
http://laravel.com/docs/master/authentication用户身份验证
这个函数是在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');
}
}