提问者:小点点

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


当一个新用户注册后,它会自动登录,这很正常。我对密码进行如下哈希运算:

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

它成功地散列密码,我的用户表有密码列,它是varchar 255。我也remember_token用户表中的字段。

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

并且,电子邮件和密码与users表记录完全一致。请求是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个答案

匿名用户

这是我的工作:

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!',
        ]);
    }
}

匿名用户

希望这对你有帮助。。

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