0

I am trying to reset my password but i am unable to solve my problem and i am stuck in this issue and hours has been passed but i cant figure out where is the issue:

My Controller:

  class ResetPasswordController extends Controller
   {
protected $user;



public function __construct(User $user)
{
    // set the model
    $this->user = $user;

}
public function showResetForm(Request $request, $token = null)
{
    return view('auth.passwords.reset')->with(
        ['token' => $token, 'email' => $request->email]
    );
}

public function reset(Request $request)
{
    $validator = UserValidations::changePassword($request->all());

    if($validator->fails()) {
        return response(['status' => false,'message' => __('messages.validation_errors'), 'errors' => $validator->errors()->all()], 200);
    }

    try {
        $password = $this->user->where('id', Auth::user()->id)->value('password');

        if(Hash::check($request->input('current_password'),$password)) {

            $this->user->where('id', Auth::user()->id)->update(['password' => bcrypt($request->input('new_password'))]);

            $token = $request->header('Authorization');

            JWT::invalidate($token);

            Auth::logout();

            return response(['status' => true, 'message' => 'Password changed successfully'], 200);

        } else {
            return response(['status' => false, 'message' => 'The Current Password is invalid.'], 200);
        }
    } catch (\Exception $ex) {
        return response(['status' => false, 'message' => $ex->getMessage()], 500);
    }
}

}

my view:

                    <form class="form-horizontal" method="POST" action="{{ route('password.request') }}">
                    {{ csrf_field() }}

                    <input type="hidden" name="token" value="{{ $token }}">

                    <div class="form-group{{ $errors->has('current_password') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label">Current Password</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control" name="current_password" required>

                            @if ($errors->has('current_password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('current_password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('new_password') ? ' has-error' : '' }}">
                        <label for="password" class="col-md-4 control-label">Password</label>

                        <div class="col-md-6">
                            <input id="password" type="password" class="form-control" name="new_password" required>

                            @if ($errors->has('new_password'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('new_password') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group{{ $errors->has('new_password_confirmation') ? ' has-error' : '' }}">
                        <label for="password-confirm" class="col-md-4 control-label">Confirm Password</label>
                        <div class="col-md-6">
                            <input id="password-confirm" type="password" class="form-control" name="new_password_confirmation" required>

                            @if ($errors->has('new_password_confirmation'))
                                <span class="help-block">
                                    <strong>{{ $errors->first('new_password_confirmation') }}</strong>
                                </span>
                            @endif
                        </div>
                    </div>

                    <div class="form-group">
                        <div class="col-md-6 col-md-offset-4">
                            <button type="submit" class="btn btn-primary">
                                Reset Password
                            </button>
                        </div>
                    </div>
                </form>

But it saying trying to get property on non-object please help me on this your help will be highly appreciated i have also attached the screen shot of my 1st screen where there is 3 input fields and 2nd screen shot of error Your help needs please

enter image description here

syed1234
  • 761
  • 5
  • 12
  • 30

1 Answers1

0

Did you bind user parameter in your Route Facade/ServiceProvider ?

If you don't do it just add somewhere in you route password.request user id to implicit model binding. Your error most probably because you have an empty user object output of your __construct().

  • I did not understand what you are trying to say can we have chat? – syed1234 Apr 23 '19 at 05:55
  • I'm trying to say that you have empty User object, sure, lets have the conversation – Yegor Keller Apr 23 '19 at 06:02
  • This is my routes – syed1234 Apr 24 '19 at 14:18
  • \Illuminate\Support\Facades\Auth::routes(); Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm'); Route::post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.request'); – syed1234 Apr 24 '19 at 14:18
  • Can you please show me how to add user id as implicit model binding – syed1234 Apr 24 '19 at 14:18
  • Change your __construct method public function __construct() { $this->user = Auth::user(); } and add use Illuminate\Support\Facades\Auth; it's not the best option but it should work – Yegor Keller Apr 25 '19 at 04:54