Laravel 5.3 Auth :: attempt()总是返回false

时间:2017-01-10 06:52:05

标签: php laravel authentication login laravel-5.3

当新用户注册后,它会自动登录,这样可以正常工作。我正在使用这样的密码:

'$newUser->password = bcrypt($request->get('password'));' 

它成功地哈希密码,我的用户表有密码列,这是varchar 255.我在users表中也有remember_token字段。 'dd($request->all())'返回:

array:3 [
  "email" => "info@hotmail.com"
  "_token" => "QIwnHacApWg3SotAXtoCCMFNK3FYFoFBAv2LSx4c"
  "password" => "adminadmin"
]  

并且,电子邮件和密码对用户表记录100%正确。 请求是Ajax,所以我有以下JS代码:

$('.post-btn').click(function(){            
    $.ajax({
        url: '/sign-in',
        type: "post",
        data: {'email':$('input[name=email]').val(), '_token': $('input[name=_token]').val(),'password': $('input[name=password]').val()},
        success: function(data){
            console.log(data);
            window.location = '/';
        }
    });      
}); 

验证方法

public function authenticate(Request $request) {
    $validator = Validator::make($request->all(),
        [
            'email' => "required",
            'password' => 'required'   
        ]
    );

    $user = array('email' => $request->get('email'),'password' => $request->get('password'));
        if (Auth::attempt($user)) {
            $response = array(
                'status' => 'success',
                'msg' => 'Successfully Logins.',
            );
            $user = new \App\User;
            if(Auth::check()) {

            }
            return \Response::json($response);
        } else {
            $response = array(
                'status' => 'failed',
                'msg' => 'Invalid Credentials!',
            );
            return \Response::json($response);
        }
    }

输入字段名称正确。

我还缺少什么?

2 个答案:

答案 0 :(得分:0)

这项工作对我来说:

public function authenticate(Request $request)
{
    $validator = Validator::make($request->all(),
        [
            'email' => "required",
            'password' => 'required'

        ]
    );
    $email = $request->input('email');
    $password = $request->input('password');
    if (Auth::attempt(['email' => $email, 'password' => $password])) {
        // Authentication passed...
        return \Response::json([
            'status' => 'success',
            'msg' => 'Successfully Logins.',
        ]);
    }
    else {
        return \Response::json([
            'status' => 'failed',
            'msg' => 'Invalid Credentials!',
        ]);
    }
}

答案 1 :(得分:0)

希望这会对你有帮助..

$user = array('email' => $request->email,'password' => $request->password);