Auth::attempt($credentials) 总是返回 false

问题描述 投票:0回答:4

您好,我是 Laravel 新手,我正在尝试编写登录表单的功能,代码如下:

这就是我创建新用户的方式(效果很好)

public function action_newuser()
{

    $email = Input::get('email');
    $password = Input::get('password');
    $new_user = Input::get('new_user', 'off');
    $first_name= Input::get('firstname');
    $last_name= Input::get('last_name');
    $username= Input::get('username');

    $user = new User();
    $user->email = $email;
    $user->password = Hash::make($password);
    $user->first_name =$first_name;
    $user->last_name= $last_name;
    $user->username= $username;
    try{

        $user->save();
    }
    catch(Exception $e) {
        echo "Failed to create new user!";
    }

这是我的登录功能:

public function action_login()
{   

    $password = Input::get('password');    
    $username= Input::get('username');
    $remember= Input::get('remember', 'off');
    if($remember =='off')   $remember=FALSE;
    if($remember =='on') $remember=TRUE;
    $credentials = array(
            'username' => $username,
            'password' => $password,
            'remember' => $remember   
        );
     if( Auth::attempt($credentials)) 
        {
            return Redirect::to('dashboard/index');
        } 
        else 
        {
            #I put this line to check are the values passed from my form are correct.
            echo $password.'  ' .$username.' '. $remember;

        }


}   

当我提交登录表单时,它总是显示一个空白页面,其中包含 $password、$username 和 $remember 值。这意味着 Auth::attempt() 无法正常工作。

可能是什么问题?

php laravel
4个回答
4
投票

骗我! 虽然我检查了很多次,但我在auth配置文件中看不到

'username' => 'email',
这一行。

应该是

'username' => 'username',
,因为我将使用用户名登录。


3
投票

检查并确保您的数据库密码字段为 60 个字符。


1
投票

致所有面临 Auth::attempt() 使用有效积分登录失败的 Laravel 4 开发者!

解决方案:

  • 在 app/config/app.php 中,将类型: 'cipher' => MCRYPT_RIJNDAEL_128 更改为 'cipher' => MCRYPT_RIJNDAEL_256 并重新哈希所有密码
  • 在模型中添加方法:

     public function setPasswordAttribute($pass) {
    
         $this->attributes['password'] = Hash::make($pass);
     }
    
  • 在用户控制器中:

    //创建用户或注册

       public function postCreate() {
    
        $input = Input::all();
        $user = new User;
        $user->username = Input::get('username');
        $user->email = Input::get('email');
        $user->password = Hash::make(Input::get('password'));    
        $user = User::create($input);
        return Redirect::action("Frontend\HomeController@index");
    

    }

    //登录或登录

    public function postSignin() {
    
        $userdata = array(
            'username' => Input::get('username'),
            'password' => Input::get('password')
        );
    
        if (Auth::attempt($userdata, true)) {
             Session::put('username', Auth::user()->username);
             return Redirect::action("Frontend\HomeController@index");
        } else {
             return Redirect::action("Frontend\UserController@getLogin")
                    ->with('message', 'Your username/password combination was incorrect')
                    ->withInput();
    }
    

希望我能帮助别人


0
投票

如果您已尝试了所有其他答案但仍然无法解决问题,那么也许您应该检查一下您是否尚未在代码中的某处对密码进行哈希处理,例如在

User.php
中的突变器中,现在您正在尝试在
action_newuser
方法中对其进行第二次哈希处理,最终在数据库中保存了双重哈希密码。

它应该看起来像这样:

public function setPasswordAttribute($password)
{
     $this->attributes['password'] = bcrypt($password);
}

如果是这种情况,我建议删除它或将其调整为仅对尚未散列的密码进行散列。您可以通过使用 Laravel 的

Hash::needsRehash()
方法检查密码是否需要重新哈希来完成此操作。

public function setPasswordAttribute($password)
{
    // Check if the password is not already hashed
    if (! \Illuminate\Support\Facades\Hash::needsRehash($password)) {
        $this->attributes['password'] = bcrypt($password);
    } else {
        $this->attributes['password'] = $password;
    }
}

通过此更改,如果密码尚未散列,则变通器只会对密码进行散列,从而防止出现诸如

Auth::attempt()
返回
false
之类的问题。

© www.soinside.com 2019 - 2024. All rights reserved.