From 8ac9d4a2fdfcd047120d8c3b18d30f5ac65e748f Mon Sep 17 00:00:00 2001 From: Priatmoko Date: Mon, 8 Apr 2019 10:21:30 +0700 Subject: [PATCH] Add password change features --- .../Admin/Profile/FormController.php | 1 - .../Admin/Profile/PwdController.php | 42 +++++++++++ public/assets/Admin/Profile/password.js | 30 ++++++++ public/assets/Admin/Profile/setting.js | 63 ++++++++-------- .../Admin/Profile/form-password.blade.php | 40 ++++++++++- .../Admin/Profile/form-setting.blade.php | 72 +++++++++---------- .../views/Admin/Profile/setting.blade.php | 1 + .../layouts/elements/topbar/usernav.blade.php | 2 +- routes/web.php | 1 + 9 files changed, 184 insertions(+), 68 deletions(-) create mode 100644 app/Http/Controllers/Admin/Profile/PwdController.php create mode 100644 public/assets/Admin/Profile/password.js diff --git a/app/Http/Controllers/Admin/Profile/FormController.php b/app/Http/Controllers/Admin/Profile/FormController.php index 0c0fee8..07fea70 100644 --- a/app/Http/Controllers/Admin/Profile/FormController.php +++ b/app/Http/Controllers/Admin/Profile/FormController.php @@ -27,7 +27,6 @@ public function store(Request $r) //check validataion result if ($validation->fails()) return response()->json(['errors'=>$validation->errors()], 422); - //passed input continue to run update operation $user = User::find($r->input('id')); diff --git a/app/Http/Controllers/Admin/Profile/PwdController.php b/app/Http/Controllers/Admin/Profile/PwdController.php new file mode 100644 index 0000000..688199c --- /dev/null +++ b/app/Http/Controllers/Admin/Profile/PwdController.php @@ -0,0 +1,42 @@ +all(),['password' => ['required', 'string', 'min:8', 'confirmed']]); + //add custom validator to validate current password, make sure that actor are user owner + $validation->after(function($validation) use($r){ + if (!\Hash::check($r->input('password_current'), \Auth::user()->password)) + $validation->errors()->add('password_current', 'Please fill in matched password to your user!'); + }); + //check validataion result + if ($validation->fails()) + return response()->json(['status'=>'error', 'errors'=>$validation->errors()], 422); + //run the operation to change password + $user = User::find($r->input('id')); + $user->password=\Hash::make($r->input('password')); + if ($user->save()){ + \Auth::logoutOtherDevices($r->input('password')); + \Auth::logout(); + //return the success message + $response =['status'=>'success','data'=>'','message'=>'']; + return response()->json($response, 200); + } + } +} diff --git a/public/assets/Admin/Profile/password.js b/public/assets/Admin/Profile/password.js new file mode 100644 index 0000000..e7c7b1a --- /dev/null +++ b/public/assets/Admin/Profile/password.js @@ -0,0 +1,30 @@ +/** + * Submit form change password + */ +var submitPassword = function(){ + $('#form-user-pwd').submit(function(e){ + //prevent submit event as default and we change it using our custom event (not reload page) + e.preventDefault(); + if ($('#form-user-pwd').postValidate()===false){ + return false; + } + $('#form-user-pwd').postAjax({ + success : function(r){ + if (r.status=="success"){ + iziToast.success({ + title: 'INFO !', + message: 'Operation success. Please re-sign in using new password', + position: 'topRight' + }); + setTimeout(function(){window.location.reload()},5000); + }else if (r.status=="error"){ + iziToast.error({ + title: 'INFO !', + message: 'Operation failed, please check the data input', + position: 'topRight' + }); + } + } + }); + }); +} \ No newline at end of file diff --git a/public/assets/Admin/Profile/setting.js b/public/assets/Admin/Profile/setting.js index f5b2934..39dcd09 100644 --- a/public/assets/Admin/Profile/setting.js +++ b/public/assets/Admin/Profile/setting.js @@ -1,38 +1,43 @@ //initial var initProfile = function(){ - $('#user-profile').submit(function(e){ - e.preventDefault(); - saveProfile(e); - }); + //form user setting event + saveProfile(); + //form change password event + submitPassword(); } /** * save the profile */ -var saveProfile = function(e){ - // if ($('#user-profile').postValidate()===false){ - // return false; - // } - $('#user-profile').postFile({ - ext : ['png', 'jpg'], - maxsize : 1024, - success : function(r){ - console.log(r); - $('#avatar').val(''); - if (r.status=="success"){ - if (r.data.hasOwnProperty('image')) - $('#avatar-image').attr('src', r.data.image); - iziToast.success({ - title: 'INFO !', - message: 'Operation success, the changing has been saved', - position: 'topRight' - }); - }else if (r.status=="error"){ - iziToast.error({ - title: 'INFO !', - message: 'Operation failed, please check the data input', - position: 'topRight' - }); - } +var saveProfile = function(){ + //catch submit event + $('#user-profile').submit(function(e){ + e.preventDefault(); + //validate form + if ($('#user-profile').postValidate()===false){ + return false; } + //make ajax request + $('#user-profile').postFile({ + ext : ['png', 'jpg'], + maxsize : 1024, + success : function(r){ + $('#avatar').val(''); + if (r.status=="success"){ + if (r.data.hasOwnProperty('image')) + $('#avatar-image').attr('src', r.data.image); + iziToast.success({ + title: 'INFO !', + message: 'Operation success, the changing has been saved', + position: 'topRight' + }); + }else if (r.status=="error"){ + iziToast.error({ + title: 'INFO !', + message: 'Operation failed, please check the data input', + position: 'topRight' + }); + } + } + }); }); } \ No newline at end of file diff --git a/resources/views/Admin/Profile/form-password.blade.php b/resources/views/Admin/Profile/form-password.blade.php index b5d3e0f..cb20112 100644 --- a/resources/views/Admin/Profile/form-password.blade.php +++ b/resources/views/Admin/Profile/form-password.blade.php @@ -3,6 +3,44 @@ ['title'=>'User Profile Change Password']) @csrf

Update your current password to the safer new password.

+
+ +
+ +
+ @if ($errors->has('id')) + {{ $errors->first('id') }} + @else + {{__('Please fill in your username')}} + @endif +
+
+
+ +
+ @if ($errors->has('username')) + {{ $errors->first('username') }} + @else + {{__('Please fill in your username')}} + @endif +
+
+
@slot('footer')
- +
@endslot @endcomponent diff --git a/resources/views/Admin/Profile/form-setting.blade.php b/resources/views/Admin/Profile/form-setting.blade.php index 51a9ea5..5c58794 100644 --- a/resources/views/Admin/Profile/form-setting.blade.php +++ b/resources/views/Admin/Profile/form-setting.blade.php @@ -4,45 +4,30 @@ @csrf

General settings such as name, photo profile, etc

-