Laravel 4 身份验证不起作用 [英] Laravel 4 authentication not working
问题描述
我有一个 Laravel 4 应用程序,我在其中设置了一个用户.在我的登录路线中,我使用电子邮件和密码调用 Auth::attempt
但它总是返回为假.我绝对有正确的密码和数据库中正确的哈希,因为 Hash::check
返回 true.
I have a Laravel 4 app in which I have set up one user. In my login route I'm calling Auth::attempt
with the email and password but it always comes back as false. I definitely have the password correct and the correct hash in the database as Hash::check
returns true.
我认为这可能是由于使用 email
作为登录字段而不是 username
,但我看不到任何设置.这个问题暗示你可以添加一个选项到 config/auth.php
但它没有用.This question 说使用 username
作为数组键,但随后我收到 SQL 错误,因为它试图在数据库中的用户名字段上进行选择.
I think it may be due to using email
as the login field instead of username
, but I can't see any setting for that. This question implied you could add an option to config/auth.php
but it didn't work. This question says to use username
as the array key, but then I get SQL error because it tries to select on a username field in the database.
我需要在 User 模型中添加一些东西来指定用户名字段吗?这是我的登录路径:
Do I need to add something to the User model to specify the username field? Here is my login route:
Route::post('login', function() {
// data from login form
$credentials = array(
'email' => Input::get('email'),
'password' => Input::get('password')
);
$auth = Hash::check(Input::get('password'), Hash::make('mypass'));
var_dump($auth); // this is TRUE
// login was good
$auth = Auth::attempt($credentials);
var_dump($auth); // this is FALSE
});
推荐答案
我发现了问题.正如 Jason 在上面的评论中所建议的,我已经修改了 models/User.php
文件并删除了一些我没有意识到是必要的功能.
I found the problem. As suggested by Jason in the comment above, I had modified the models/User.php
file and removed some functions that I didn't realise were necessary.
getAuthIdentifier()
和 getAuthPassword()
方法必须包含在 User 模型中进行身份验证!
The getAuthIdentifier()
and getAuthPassword()
methods must be included in the User model for authentication!
这篇关于Laravel 4 身份验证不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!