0

我的代码如下。

我面临问题。当我想通过usernameand进行检查时password
password每次都改变。所以我无法给出结果email password

public function login()
{   
    $data = Input::all();

    print_r($data);

    $email= $data['email'];
    $password= bcrypt($data['password']);

    echo $password;

    $count = User::where('email', '=',$email);
    $count->Where('password', '=', $password);
    $count_row=$count->get()->count();

    echo $count_row; die;

    if($count_row==1)
    {
        $users = User::where('email', '=',$email);
        $users->Where('password', '=', $password);
        $users->select(
            'id','name','email', 
            'username','address','zip', 
            'city','country','phone_number', 
            'domain','type') ;

        $result=$users->get();

        $response= response()->json(['success'=>true,'message'=>'success', 'data'=>$result]); 

        return $response;
    }
    else
    {
        $response= response()->json(['success'=>false,'message'=>'Not Login successfull']); 

        return $response;            
    }
}
4

1 回答 1

1

根据文档,验证密码的正确方法是

Hash::check('plain-text', $hashedPassword)

所以你可以将你的函数重写为

public function login()
{ 
    $email = Input::get('email');
    $user = User::where('email', '=', $email)->first();

    if (!$user) {
        return response()->json(['success'=>false, 'message' => 'Not Login successfull']);
    }

    if (!Hash::check(Input::get('password'), $user->password)) {
        return response()->json(['success'=>false, 'message' => 'Not Login successfull']);
    }

    return response()->json(['success'=>true,'message'=>'success', 'data' => $user]); 
}

或者更简单

public function authenticate(Request $request)
{
    $credentials = $request->only('email', 'password');

    if (!Auth::attempt($credentials)) {
        return response()->json(['success'=>false, 'message' => 'Not Login successfull']);
    }

    $user =  User::where('email', '=', $request->get('email'))->first()

    return response()->json(['success'=>true,'message'=>'success', 'data' => $user]); 
}
于 2018-06-14T13:18:41.087 回答