Auth::attempt [Laravel] 不好用, 总是return false

Auth::attempt [Laravel] doesn't work well,, It's always return false

每次我尝试测试我的 TestLogin 函数时,它 return 错误,我的输入是不正确的[未存储在数据库中]还是正确的[存储在我的数据库中] ...

auth.phph 的配置更正 ['model' => 'User'&& 'table' => 'users']

请帮忙:)

public 函数 TestLogin() {

    $data = Input::All();                     //Get Input with POST Request
    $UNameOrEmail = $data['UnameOrEmail'];
    $password = $data['Password'];

    if(filter_var($UNameOrEmail, FILTER_VALIDATE_EMAIL)) {      //Check if Input Is Email or else(then consider it as user name)

        $validator = Validator::make(           //Validation for Email
            array(
                'email' => $UNameOrEmail,
                'password' => $password,
            ),
            array(
                'email' => 'required|email|exists:users,email',
                'password' => 'required|min:8'
            )
        );

        if ($validator->fails()) { //If Email Validation was wrong return validation message
            $messages = $validator->messages();
            return $messages;
        }
        else                           //else store Email&Password in Array
            $user = array(
                'email' => $UNameOrEmail,
                'password' => Hash::make($password)
            );

    }

    else {
        $validator = Validator::make(    //Check User Name Validation
            array(
                'user_name' => $UNameOrEmail,
                'password' => $password,
            ),
            array(
                'password' => 'required|min:8',
                'user_name' => 'required|alpha_dash|between:4,16'
            )
        );

        if ($validator->fails()) {                  //If User Name Validation was wrong return validation message
            $messages = $validator->messages();
            return $messages;
        } else
            $user = array(                           //else store UserName&Password in Array
                'user_name' => $UNameOrEmail,
                'password' => Hash::make($password)
            );

    }


    if (Auth::attempt($user)) {             //Here's the Error

        echo 'Successfully logged in';
    }
    else
    {
        echo 'Some thing go wrong';             //In any case it jum here :(
    }

}

我看到您使用的是 Input::All()(大写字母 A),而不是 Input::all()

做一个dd(Input::All());看看有什么returns。

发现问题(我认为):您应该散列您的密码,因为 Auth 会自动进行。所以简而言之,您手动对其进行哈希处理,然后 Auth 再次对其进行哈希处理,这就是为什么密码与数据库中存储的内容不匹配的原因 table :)

在将密码传递给 Auth::attempt 之前,您不得散列密码。只需传递明文密码:

$user = array(                           
    'user_name' => $UNameOrEmail,
    'password' => $password
);

还要确保在数据库中存储了密码的哈希值,并且密码字段的长度为 60 个字符。否则哈希值将被截断并且无法正常工作。