douh9817 2014-10-22 07:38
浏览 50
已采纳

Laravel Auth ::尝试功能不起作用

I am trying to use the Laravel attempt function within the Auth class to allow a user to login and store themselves in the session.

I understand this function requires an array with the 'password' and 'email' variables and I am sending this fine, it just doesn't seem to be logging me in or returning true.

Here is my controller code:

class LoginController extends BaseController {
    public function login()
    {       
        if(isset($_POST)) {     
            $errorsArray = array();
            foreach($_POST as $key => $postItem) {
                if(empty($postItem) || $postItem == "") {
                    $errorsArray[] = array(
                        'errorItem' => $key,
                        'errorMessage' => "Please enter a value for " .ucfirst($postItem)
                    );
                }
            }
            if(!empty($errorsArray)) {
                $returnArray = array( 
                    'didLog' => false,
                    'errors' => $errorsArray 
                );
            } else {

                if(Auth::attempt(array('username' => $_POST['username'], 'password' => $_POST['password']))) {
                    die("here");
                } else {
                    die("no");
                }

            }
            return json_encode($returnArray);
        }
    }

}

As you can see above, I am checking to see if the inputs are valid, and if so, calling the Auth::attempt function in order to log a user in.

The view code:

<form>
        <h3><i class="lock"></i>Please enter your login details</h3>
        <input type="email" name="username" placeholder="email" data-required=1 />
        <input type="password" name="password" placeholder="password" data-required=1 />
        <input type="submit" value="login" />   
        <button>Signup with Facebook</button>
        <button>Signup with Twitter</button>
        <a href="#" class="close-overlay" data-related="login-overlay">CLOSE</a>
        <div class="clear"></div>
    </form>

And the appropriate Javascript code

            jQuery.ajax({
                type     : "post",
                dataType : "json",
                url      : url,
                data     : "username=" + username + "&password=" + password,
                success  : function(data) {
                    if(data.didLog == true) {
                        alert("rrue");
                    } else {
                        alert("false");
                    }
                }
            });

And I have die(print_r(()) the input (which is present in my database) as follows:

Array

( [username] => jamesholman@bigwavemedia.co.uk [password] => password )

And the appropriate record in the database

enter image description here

I am getting a "no" (which is a fail in my code) when attempting to login. Can anyone see where I am going wrong as I can't for the life of me figure out why it won't work! Thanks

  • 写回答

1条回答 默认 最新

  • duanchen7401 2014-10-22 07:53
    关注

    Are you logging in with "jamesholman@bigwavemedia.co.uk" or "jamesholman" as your username? The Auth::attempt is pointing $_POST['username'] to your username column in your database table. So if you're trying to authenticate with 'jamesholman@bigwavemedia.co.uk' as your username, it won't find any user

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,怎样解决呢?
  • ¥50 如何用单纯形法寻优不能精准找不到给定的参数,并联机构误差识别,给定误差有7个?matlab
  • ¥15 workstation加载centos进入emergency模式,查看日志报警如图,没有XFS,怎样解决呢?
  • ¥15 应用商店如何检测在架应用内容是否违规?
  • ¥15 Ubuntu系统配置PX4
  • ¥50 nw.js调用activex
  • ¥15 数据库获取信息反馈出错,直接查询了ref字段并且还使用了User文档的_id而不是自己的
  • ¥15 将安全信息用到以下对象时发生以下错误:c:dumpstack.log.tmp 另一个程序正在使用此文件,因此无法访问
  • ¥15 速度位置规划实现精确定位的问题
  • ¥15 MAC虚拟机(win11)USB插上后无串口com,无法烧录