Auth ::尝试[Laravel]不能很好地工作,它总是返回false

时间:2015-02-05 18:26:24

标签: php authentication laravel controllers

每当我尝试测试我的TestLogin函数时,它返回false,无论我的输入是不正确的[未存储在DataBase中]还是正确的[存储在我的DataBase中] ......

auth.phph的配置正确[' model' => '用户'&安培;&安培; '表' => '用户']

请在此处提供任何帮助:)

公共功能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 :(
    }

}

2 个答案:

答案 0 :(得分:0)

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

执行dd(Input::All());查看返回的内容。

发现问题(我想):你应该哈希密码,因为Auth会自动执行此操作。简而言之,你是手工编写它,然后Auth再次哈希它,这就是为什么密码永远不会匹配存储在数据库表中的内容:)

答案 1 :(得分:0)

在将密码传递给Auth::attempt之前,不得对密码进行哈希处理。只需传递普通密码:

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

另外,请确保在数据库中存储密码哈希,密码字段 60个字符。否则哈希将被截断,并且它不会起作用。