laravel修改密码时Hash::check验证原密码

//修改密码
    public function reset(Request $request)
    {
        $user = DspConfig::select('id', 'name', 'password')->findOrFail(auth('ADsp')->user()->id);
        if ($request->isMethod('post')) {
            $oldpassword = $request->oldpassword;
            $password = $request->password;

            $data = $request->all();
            $rules = [
                'oldpassword'=>'required|between:6,20',
                'password'=>'required|between:6,20|confirmed',
            ];
            $messages = [
                'required' => '密码不能为空',
                'between' => '密码必须是6~20位之间',
                'confirmed' => '新密码和确认密码不匹配'
            ];
            $validator = \Validator::make($data, $rules, $messages);
            $validator->after(function($validator) use ($oldpassword, $user) {
                //验证原密码
                if (!\Hash::check($oldpassword, $user->password)) {
                    $validator->errors()->add('oldpassword', '原密码错误');
                }
            });
            if ($validator->fails()) {
            	//返回一次性错误
                return back()->with('success', false)->withErrors($validator);  
            }
            $user->password = bcrypt($password);
            $user->save();
            //重新登录
            auth('ADsp')->logout();

            return "";;
        }
        return view('adsp.user.reset',compact('user'));
    }

你可能感兴趣的:(php,laravel)