0

My code is given below.

I am facing problem. When I want check by username and password.
password change every time. so I am unable to give result by 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;            
    }
}
2
  • You can directly use auth facade for the authentication. Authentication uses salt which might be the reason for changing the password. Also refer following link :mnshankar.wordpress.com/2014/03/29/laravel-hash-make-explained Commented Jun 14, 2018 at 13:04
  • i have already login by web. how can use directly use auth facade Commented Jun 14, 2018 at 13:24

1 Answer 1

1

According to the docs, the correct way to verify a password is

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

So you can rewrite your function as

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

Or even simpler

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]); 
}
Sign up to request clarification or add additional context in comments.

2 Comments

my route is Route::post('api-login','ApiController@authenticate');
i am using authenticate controller but it's not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.