7*4 2015-02-12 19:00 采纳率: 50%
浏览 34

无法登录laravel

I'm trying to authenticate a user through Ajax in Laravel.

public function authenticate(){
        $email = Input::get('email');
        $password = Hash::make(Input::get('password'));

        if(!filter_var($email, FILTER_VALIDATE_EMAIL)){
            return Response::json(["success"=>"true", "login"=>"false", "error_msg"=>"<span style='margin-bottom:20px;' class='val_error_msg'>Email is not valid!</span>"]);
        }
        elseif(Auth::attempt(['email' => $email, 'password' => $password])){
            return Response::json(["success"=>"true", "login"=>"false", "error_msg"=>"<span style='margin-bottom:20px;' class='val_error_msg'>Logged in</span>"]);
        }
        else{
            return Response::json(["success"=>"true", "login"=>"false", "error_msg"=>"<span style='margin-bottom:20px;' class='val_error_msg'>Email/password is wrong</span>"]);
        }
    }

Ajax call

    $.post(
             '/login/authenticate', // location of your php script
             { email: $("#log_email").val(), password: $("#log_password").val()}, // any data you want to send to the script
                            function( data ){  // a function to deal with the returned information
              if(data.login=='false'){
                    $("#login_response").empty();
                    $("#login_response").append(data.error_msg);
              }
 });

But I'm getting the Email/password is wrong message all the time even if the credentials are good.

I tried if((User::where('email', $email)->where('pasword', $password)->count())==1) and it worked.

What's wrong in the Auth::attempt() method?

  • 写回答

2条回答 默认 最新

  • Lotus@ 2015-02-12 19:05
    关注

    You have to pass the password in plain text to Auth::attempt():

    $password = Input::get('password');
    Auth::attempt(['email' => $email, 'password' => $password])
    

    Edit

    Apparently you are using md5 to hash your password. This is not only insecure but will also not work with Laravel's Auth methods. Use Hash::make() instead when creating the user instead:

    $user->password = Hash::make(Input::get('password'));
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?