Laravel 身份验证尝试失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23711521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Auth attempt failing
提问by desidarling
I really try to debug my issues on my own before I bring them here, but I seriously cannot find a solution to my laravel auth problem, though it seems to be a common issue.
在我将它们带到这里之前,我真的尝试自己调试我的问题,但我真的找不到解决 laravel 身份验证问题的方法,尽管这似乎是一个常见问题。
My authentication will not login. It always returns false and I don't understand why.
我的身份验证不会登录。它总是返回 false,我不明白为什么。
I've read through some other questions here, and their solutions haven't solved my particular situation.
我在这里阅读了一些其他问题,他们的解决方案并没有解决我的特殊情况。
- My User model implements UserInterface and Remindable Interface.
- My password is hashed upon creating it to the database.
- My password field in my database is varchar 100, which should be more than enough to hash the password.
- The user I'm logging is has been created and activated in the database.
- 我的用户模型实现了 UserInterface 和 Remindable Interface。
- 我的密码在创建到数据库时被散列。
- 我的数据库中的密码字段是 varchar 100,它应该足以散列密码。
- 我正在登录的用户已在数据库中创建并激活。
Thank you so much for any insight.
非常感谢您的任何见解。
User Model
用户模型
<?php
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $fillable = array('email', 'username', 'password', 'password_temp', 'code', 'active');
public $timestamps = false;
protected $softDelete = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'Users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = 'password';
/**
* Get the unique identifier for the user.
*
* @return mixed
*/
public function getAuthIdentifier()
{
return $this->getKey();
}
/**
* Get the password for the user.
*
* @return string
*/
public function getAuthPassword()
{
return $this->password;
}
/**
* Get the e-mail address where password reminders are sent.
*
* @return string
*/
public function getReminderEmail()
{
return $this->email;
}
}
Account Controller
帐户控制器
class AccountController extends BaseController {
public function getLogin() {
return View::make('account.login');
}
public function postLogin() {
$validator = Validator::make(Input::all(),
array(
'email' => 'required',
'password' => 'required'
)
);
if($validator->fails()) {
return Redirect::route('login')
->withErrors($validator);
} else {
$auth = Auth::attempt(array(
'email' => Input::get('email'),
'password' => Input::get('password'),
'active' => 1
));
if($auth) {
return Redirect::route('Create-Account');
}
}
return Redirect::route('login')
->with('global', 'There was a problem logging you in. Please check your credentials and try again.');
}
public function getCreate() {
return View::make('account.create');
}
public function getviewReturn() {
return View::make('account.return');
}
public function postCreate() {
$validator = Validator::make(Input::all(),
array(
'email' => 'required|max:50|email|unique:Users',
'username' => 'required|max:15|min:4|unique:Users',
'password' => 'required|min:6',
'password2' => 'required|same:password'
)
);
if ($validator->fails()) {
return Redirect::route('Post-Create-Account')
->withErrors($validator)
->withInput();
}
else {
$email = Input::get('email');
$username = Input::get('username');
$password = Input::get('email');
$code = str_random(60);
$user = User::create(array(
'email' => $email,
'username' => $username,
'password' => Hash::make($password),
'code' => $code,
'active' => 0));
});
return Redirect::to('account/return')
Routes
路线
Route::group(array('before' => 'guest'), function() {
Route::group(array('before' => 'csrf'), function() {
Route::post('/account/create', array(
'as' => 'Post-Create-Account',
'uses' => 'AccountController@postCreate'
));
Route::post('/account/login', array(
'as' => 'postlogin',
'uses' => 'AccountController@postLogin'
));
});
Route::get('/account/login', array(
'as' => 'login',
'uses' => 'AccountController@getLogin'
));
Route::get('/account/create', array(
'as' => 'Create-Account',
'uses' => 'AccountController@getCreate'
));
Route::get('/account/activate/{code}', array(
'as' => 'Activate-Account',
'uses' => 'AccountController@getActivate'
回答by Laurence
When creating the user you've done
创建用户时,您已完成
$password = Input::get('email');
It should be
它应该是
$password = Input::get('password');
so if you try and login with the "email" as the password - it will work! :)
因此,如果您尝试使用“电子邮件”作为密码登录 - 它将起作用!:)
So if you change this
所以如果你改变这个
else {
$email = Input::get('email');
$username = Input::get('username');
$password = Input::get('email');
$code = str_random(60);
$user = User::create(array(
'email' => $email,
'username' => $username,
'password' => Hash::make($password),
'code' => $code,
'active' => 0));
});
to this
对此
else {
$user = User::create(array(
'email' => Input::get('email'),
'username' => Input::get('username'),
'password' => Hash::make(Input::get('password');),
'code' => str_random(60),
'active' => 0));
});
that cleans up your code and fixes the issue.
清理您的代码并解决问题。
回答by Antonio Carlos Ribeiro
Your code looks right to me, so you have to check some things:
你的代码在我看来是正确的,所以你必须检查一些事情:
1) A manual attempt works for you?
1)手动尝试对您有用吗?
dd( Auth::attempt(['email' => 'youremail', 'password' => 'passw0rt']) );
2) The user hash checks manually?
2)用户手动检查哈希?
$user = User::find(1);
var_dump( Hash::check($user->password, 'passw0rt') );
dd( Hash::check($user->password, Input::get('password')) );
回答by George
Try to add primaryKey
field in your user model. It should be something like that:
尝试primaryKey
在您的用户模型中添加字段。它应该是这样的:
protected $primaryKey = 'user_id';