【问题标题】:Laravel Auth::attempt failing each timeLaravel Auth::attempt 每次都失败
【发布时间】:2022-02-03 22:53:41
【问题描述】:

我在我的laravel 应用上创建了一个测试用户。详情是

用户:joe@gmail.com 密码:123456

当我完成注册过程时,一切都按预期工作,并且在databaseusers table 中输入了一个条目

完成后,我将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();
        }
    }

然后,我导航回登录页面并尝试使用上面的凭据登录,但我一直被告知 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();
        }
    }

有人知道为什么会这样吗?这是我的users 表的Schema

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(); 
        });

非常感谢任何帮助。

'查看登录':

<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>

【问题讨论】:

  • 我刚刚使用了注册表
  • 对不起,我评论后才看到。
  • 你能告诉我们登录的视图吗?在您的方法login 中,如果您放置dd(Input::all()),您是否有预期值?
  • 另外,你的用户模型是否实现了Illuminate\Auth\UserInterface
  • 我也遇到过类似的情况,在我的情况下,出于某种奇怪的原因,我只能在 config/auth.php 中使用 database 作为 driver 进行身份验证,而不是 eloquent

标签: php laravel laravel-4


【解决方案1】:

你能在下面运行这个函数吗?告诉我错误发生在哪里?它将诊断问题:

public function testLogin()
{
     $user = new User;
     $user->fname = 'joe';
     $user->lname = 'joe';
     $user->email = 'joe@gmail.com';
     $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' => 'joe@gmail.com', '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');
     }
}

编辑:一个想法 - 你确定用户被正确保存在数据库中吗?您是否尝试过“清空/删除”您的数据库并再次尝试您的代码?在您当前的代码中,如果您继续向 joe@gmail.com 注册,它将失败 - 因为它是独一无二的。但是您在任何地方都没有发现错误。所以清空数据库再试一次...

编辑 2:I found another question you posted with the same problem - 您在其中提到以下代码是您的用户模型?

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) { 

} 
}

这正是您当前的用户模型吗?因为如果是这样 - 这是错误的 - 这些函数都不应该是空白的。

这就是 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');

}

【讨论】:

    【解决方案2】:

    你会确保:

    • 您的模型:

    我的样子:

    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;
            }
        }
    
    • 确保您的 app/config/auth.php 配置正确
    • 确保 app/config/app.php 有服务提供者

    'Illuminate\Auth\AuthServiceProvider',

    • 确保您的控制器类具有身份验证。在写课程之前你已经使用了 Auth(我的意思是包括 Auth 类)

    这一切都可能使 Auth 无法正常工作

    【讨论】:

    • 有用的答案,但不是我想要的,谢谢
    【解决方案3】:

    启用密码哈希后,User 模型必须覆盖这些方法:

    public function getAuthIdentifierName()
    {
        return 'email';
    }
    
    public function getAuthIdentifier()
    {
        return request()->get('email');
    }
    
    public function getAuthPassword()
    {
        return Hash::make(request()->get('password'));
    }
    

    【讨论】:

      【解决方案4】:

      strlen(Hash::make(Input::get('password'))) 的值是多少?如果大于 60,那么这会导致每次认证失败,因为存储的密码不是完整的哈希。

      【讨论】:

      • 在 Laravel 中,哈希输出固定为 60
      【解决方案5】:

      美好的一天,当我遇到同样的错误时,我发现:一个简单的字符串比较将显示这两种散列方法产生两个不同的散列值。

      echo strcmp(Hash::make('password'),bcrypt('password'));
      

      我的假设是 Auth::attempt([]) 使用 bcrypt() 来散列密码,这会产生与您使用的 Hash:make() 不同的值。

      【讨论】:

      • 您的假设是错误的:值是使用随机盐散列的,因此永远不应该相同。这是一个 7.5 年前的问题,询问的是古代版本的 Laravel。
      猜你喜欢
      • 2017-04-28
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 2014-08-27
      • 2014-05-22
      • 2013-07-12
      • 2014-10-21
      • 2015-03-27
      相关资源
      最近更新 更多