dongzuo4666 2019-04-24 16:49
浏览 96

重置用户密码时尝试获取非对象的属性

I am trying to reset the password but I am getting the error message "Trying to get property of non-object". I have also attached the screen shot of my error please have a look at it.

enter image description here

My Controller for resetting the password:

class ResetPasswordController extends Controller
{
protected $user;

public function __construct(User $user)
{
    $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)
{
    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 Routes configuration:

   \Illuminate\Support\Facades\Auth::routes();

   Route::get('password/reset/{token}', 'Auth\ResetPasswordController@showResetForm');
   Route::post('password/reset', 'Auth\ResetPasswordController@reset')->name('password.request');

My View template:

        <form action="{{ route('password.request') }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">

<div class="form-group">
    <label for="login-form-email">Current Password</label>
    <input type="password" class="form-control" name="current_password" id="login-form-password" tabindex="2" placeholder="Current Password" tabindex="4">
</div>

<div class="form-group">
    <label for="login-form-password">New password</label>
    <input type="password" class="form-control" name="new_password" id="login-form-password" tabindex="2" placeholder="New Password" tabindex="4">
</div><!-- /.form-group -->

<div class="form-group">
    <label for="login-form-password-retype">Confirm new password</label>
    <input type="password" class="form-control" name="new_password_confirmation" id="login-form-password-retype" tabindex="3" placeholder="Confirm password">
</div><!-- /.form-group -->

<div class="form-group">
    <input type="submit"  class="btn btn-primary pull-right" name="reset-confirm" id="reset-confirm" tabindex="4" value="Reset Password">
</div>

Does anyone have a solution based on this code and error message? Your help will be highly appreciated!

  • 写回答

1条回答 默认 最新

  • douwo1517 2019-04-24 17:24
    关注

    User doesn't need to be a member

    Your first problem is here:

    public function __construct(User $user)
    

    You're injecting a user, without it knowing what user to use, unless this is coming from middleware. So the constructor shouldn't take a user, nor do you need to protected member. If you really want it as a protected member you could do the following:

    public function __construct()
    {
        $this->user = Auth::user();
    }
    

    But since you have Auth::user(), you don't need it as a member.

    where on a Model is Static

    You have

    $this->user->where('id', Auth::user()->id)->value('password')
    

    Model's where function is a static function you shouldn't call it on an individual object. Instead you shoud call it using the scoping operator (::). Most versions of PHP should error out at that point. The correct way to get the current user's password hash from the database is:

    $hash = Auth::user()->password;
    

    If you had an id, you could:

    $hash = User::where('id','=',$userId)->get()->password;
    

    If you kept the user as a member (against the recommendation) but did it as in the above section of this answer, you could:

    $hash = $this->user->password
    

    Why?

    Lastly, the Auth module from Laravel in modern versions already takes care of this for you in app\Http\Controllers\Auth. Why are reinventing the wheel?

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 WPF 大屏看板表格背景图片设置
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真