【问题标题】:Trying to get property of non-object while reset user password尝试在重置用户密码时获取非对象的属性
【发布时间】:2019-04-24 16:49:26
【问题描述】:

我正在尝试重置密码,但收到错误消息“尝试获取非对象的属性”。 我还附上了我的错误的屏幕截图,请看一下。

用于重置密码的我的控制器:

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

}

我的路线配置:

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

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

我的视图模板:

        <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>

有没有人有基于此代码和错误消息的解决方案?非常感谢您的帮助!

【问题讨论】:

    标签: php laravel passwords


    【解决方案1】:

    用户无需成为会员

    你的第一个问题在这里:

    public function __construct(User $user)
    

    您正在注入一个用户,但它不知道要使用哪个用户,除非它来自中间件。所以构造函数不应该带用户,也不需要受保护的成员。如果您真的希望它成为受保护的成员,您可以执行以下操作:

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

    但既然你有Auth::user(),你就不需要它作为会员了。

    模型上的 where 是静态的

    你有

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

    模型的where 函数是一个静态函数,您不应该在单个对象上调用它。相反,您应该使用范围运算符 (::) 来调用它。大多数版本的 PHP 应该在那时出错。从数据库中获取当前用户密码哈希的正确方法是:

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

    如果你有一个 id,你可以:

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

    如果您将用户保留为会员(反对建议),但按照本答案的上述部分进行操作,您可以:

    $hash = $this->user->password
    

    为什么?

    最后,来自 Laravel 的现代版本的 Auth 模块已经在 app\Http\Controllers\Auth 中为您处理了这个问题。为什么要重新发明轮子?

    【讨论】:

      猜你喜欢
      • 2019-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-02
      • 1970-01-01
      • 2015-09-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多