Laravel在更改新密码时检查旧密码 [英] Laravel check for old password, when change new password

查看:284
本文介绍了Laravel在更改新密码时检查旧密码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查用户是否将new_password输入作为当前密码传递,我想使用消息Your current password can't be with new password进行重定向.我该如何检查?我想执行系统更改密码用户,但是我希望拒绝旧密码通过.我该怎么办?

I want check if user pass new_password input as current password, I want to redirect with message: Your current password can't be with new password. How I can check this? I want do system change password user, but I want denied old passwords pass. How I can do ?

if (!(Hash::check($request->old_password, Auth::user()->password))) {
    return response()->json(['errors' => ['Your current password can't be with new password']], 400);
}

代码不起作用. 我需要将旧密码写入数据库吗?

Code is not working. Do I need write old passwords to database?

推荐答案

use Illuminate\Support\Facades\Hash;
$user = User::findOrFail($id);

/*
* Validate all input fields
*/
$this->validate($request, [
    'password' => 'required',
    'new_password' => 'confirmed|max:8|different:password',
]);

if (Hash::check($request->password, $user->password)) { 
   $user->fill([
    'password' => Hash::make($request->new_password)
    ])->save();

   $request->session()->flash('success', 'Password changed');
    return redirect()->route('your.route');

} else {
    $request->session()->flash('error', 'Password does not match');
    return redirect()->route('your.route');
}

这篇关于Laravel在更改新密码时检查旧密码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆