Laravel Auth::attempt 每次都失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25329584/
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 each time
提问by Javacadabra
Hi guys I've created a test user on my laravel
app. The details are
大家好,我在我的laravel
应用程序上创建了一个测试用户。详情是
user: [email protected] pass: 123456
用户:[email protected] 密码:123456
When I go through the registration process everything works as expected and an entry is made into the users table
of the database
当我完成注册过程时,一切都按预期进行,并且users table
在database
Once this is finished I redirect the user
to the dashboard.
完成后,我会将其重定向user
到仪表板。
public function postCreate(){
//Rules
$rules = array(
'fname'=>'required|alpha|min:2',
'lname'=>'required|alpha|min:2',
'email'=>'required|email|unique:users',
'password'=>'required|alpha_num|between:6,12|confirmed',
'password_confirmation'=>'required|alpha_num|between:6,12'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->passes()){
//Save in DB - Success
$user = new User;
$user->fname = Input::get('fname'); //Get the details of form
$user->lname = Input::get('lname');
$user->email = Input::get('email');
$user->password = Hash::make(Input::get('password'));//Encrypt the password
$user->save();
return Redirect::to('/books')->with('Thank you for Registering!');
}else{
//Display error - Failed
return Redirect::to('/')->with('message', 'The Following Errors occurred')->withErrors($validator)->withInput();
}
}
I then navigate back to the landing page and attempt to log in using the credentials above and I keep getting told that Auth::attempt()
is failing hence my user cannot log into the application.
然后我导航回登录页面并尝试使用上面的凭据登录,但我一直被告知Auth::attempt()
失败,因此我的用户无法登录应用程序。
public function login(){
if(Auth::attempt(array('email'=>Input::get('email'), 'password'=>Input::get('password')))){
//Login Success
echo "Success"; die();
return Redirect::to('/books');
}else{
//Login failed
echo "Fail"; die();
return Redirect::to('/')->with('message', 'Your username/password combination was incorrect')->withInput();
}
}
Does anyone know why this is happening? This is the Schema
for my users
table:
有谁知道为什么会这样?这是Schema
我的users
桌子:
Schema::create('users', function($table){
$table->increments('id');
$table->integer('type')->unsigned();
$table->string('fname', 255);
$table->string('lname', 255);
$table->string('email')->unique();
$table->string('password', 60);
$table->string('school', 255);
$table->string('address_1', 255);
$table->string('address_2', 255);
$table->string('address_3', 255);
$table->string('address_4', 255);
$table->string('remember_token', 100);
$table->timestamps();
});
Any help is much appreciated.
任何帮助深表感谢。
'View for Login':
'查看登录':
<div class="page-header">
<h1>Home page</h1>
</div>
<!-- Register Form -->
<form action="{{ action('UsersController@postCreate') }}" method="post" role="form">
<h2 class="form-signup-heading">Register</h2>
<!-- Display Errors -->
<ul>
@foreach($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
<!-- First Name -->
<div class="form-group">
<label>First Name</label>
<input type="text" class="form-control" name="fname" />
</div>
<!-- Last Name -->
<div class="form-group">
<label>Last Name</label>
<input type="text" class="form-control" name="lname" />
</div>
<!-- Email -->
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" />
</div>
<!-- Password-->
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" />
</div>
<!-- Confirm Password -->
<div class="form-group">
<label>Confirm Password</label>
<input type="password" class="form-control" name="password_confirmation" />
</div>
<input type="submit" value="Register" class="btn btn-primary"/>
</form>
<!-- Login Form -->
<form action="{{ action('UsersController@login') }}" method="post" role="form">
<h2 class="form-signup-heading">Login</h2>
<!-- Email -->
<div class="form-group">
<label>Email</label>
<input type="text" class="form-control" name="email" />
</div>
<!-- Password-->
<div class="form-group">
<label>Password</label>
<input type="password" class="form-control" name="password" />
</div>
<input type="submit" value="Login" class="btn btn-primary"/>
</form>
回答by Laurence
Can you run this function below - and tell me where the error occurs? It will diagnose the problem:
你能在下面运行这个函数 - 并告诉我错误发生的地方吗?它将诊断问题:
public function testLogin()
{
$user = new User;
$user->fname = 'joe';
$user->lname = 'joe';
$user->email = '[email protected]';
$user->password = Hash::make('123456');
if ( ! ($user->save()))
{
dd('user is not being saved to database properly - this is the problem');
}
if ( ! (Hash::check('123456', Hash::make('123456'))))
{
dd('hashing of password is not working correctly - this is the problem');
}
if ( ! (Auth::attempt(array('email' => '[email protected]', 'password' => '123456'))))
{
dd('storage of user password is not working correctly - this is the problem');
}
else
{
dd('everything is working when the correct data is supplied - so the problem is related to your forms and the data being passed to the function');
}
}
Edit: one thought - are you sure the user is being correctlysaved in the database? Have you tried to 'empty/delete' your database and try your code again? In your current code, it will fail if you keep registering with [email protected] - because it is unique. But you dont catch the error anywhere. So empty the database and try again...
编辑:一个想法 - 您确定用户已正确保存在数据库中吗?您是否尝试过“清空/删除”您的数据库并再次尝试您的代码?在您当前的代码中,如果您继续使用 [email protected] 注册,它将失败 - 因为它是独一无二的。但是您不会在任何地方发现错误。所以清空数据库并重试......
Edit 2: I found another question you posted with the same problem- and in there you mentioned that the following code is your user model?
编辑 2:我发现您发布的另一个问题也有同样的问题- 在那里您提到以下代码是您的用户模型?
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* 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 = array('password');
public function getAuthIdentifier() {
}
public function getAuthPassword() {
}
public function getRememberToken() {
}
public function getRememberTokenName() {
}
public function getReminderEmail() {
}
public function setRememberToken($value) {
}
}
Is that EXACTLY your current user model? Because if so - it is wrong - none of those functions should be blank.
这正是您当前的用户模型吗?因为如果是这样 - 这是错误的 - 这些功能都不应该是空白的。
This is what a CORRECT user model should look like for Laravel 4.2
这就是 Laravel 4.2 的正确用户模型应该是什么样子
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
use UserTrait, RemindableTrait;
/**
* 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 = array('password', 'remember_token');
}
回答by Alireza Rahmani Khalili
You would make sure about:
您将确保:
- your model:
- 您的型号:
mine looks like:
我的看起来像:
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface {
protected $table = 'users';
protected $hidden = array('password');
public function getAuthIdentifier()
{
Return $this->getKey ();
}
public function getAuthPassword()
{
return $this->password;
}
}
- make sure your app/config/auth.php is configured correctly
- make sure app/config/app.php has service provider
- 确保您的 app/config/auth.php 配置正确
- 确保 app/config/app.php 有服务提供者
'Illuminate\Auth\AuthServiceProvider',
'照亮\Auth\AuthServiceProvider',
- Make sure your controller class has auth. before writing class you have used Auth (I mean include Auth class)
- 确保您的控制器类具有身份验证。在编写类之前,您已经使用了 Auth(我的意思是包括 Auth 类)
That all could make Auth doesn't work well
这一切都可能使 Auth 无法正常工作
回答by kfriend
What is the value for strlen(Hash::make(Input::get('password')))
? If it is greater than 60, then this would cause the authentication to fail each time, as the stored password is not the full hash.
的价值是strlen(Hash::make(Input::get('password')))
什么?如果大于 60,则每次都会导致身份验证失败,因为存储的密码不是完整的哈希值。