【问题标题】:Auth::attempt($credentials) always returns falseAuth::attempt($credentials) 总是返回 false
【发布时间】:2012-10-25 05:41:41
【问题描述】:

您好,我是 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() 不能正常工作。

可能是什么问题?

【问题讨论】:

  • $remember 真的是凭证吗?
  • @WaleedKhan 没问题。 Auth::attempt 将参数作为 array('username', 'password', 'remember')
  • @HasanAyan 不是真的,内置的记住是尝试的第二个参数(array $credentials, boolean $remember)。第一个数组中的其他参数是您尝试匹配的用户列,例如在 where 子句中。 laravel.com/docs/security#authenticating-users

标签: php laravel


【解决方案1】:

骗我! 虽然我检查了很多次,但我在 auth 配置文件中看不到 'username' => 'email', 行。

应该是'username' => 'username',,因为我要使用用户名登录。

【讨论】:

  • 观察力不错。此类错误常常被忽视。发生在我身上很多次了。
【解决方案2】:

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

【讨论】:

  • 它已经有 60 个字符了。我已经使用 Hash::check() 检查了密码和 hashed_pa​​ssword,它返回了 true。没问题。
【解决方案3】:

致所有面临 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();
    }
    

希望我已经帮助了那里的人

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 2014-03-15
    • 1970-01-01
    • 2016-09-12
    • 2017-05-24
    • 2013-06-11
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多