-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from priatmoko/user-profile
Add Change Password features
- Loading branch information
Showing
9 changed files
with
184 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
/** | ||
* Password Controller, handle all operation user profile change password | ||
*/ | ||
namespace App\Http\Controllers\Admin\Profile; | ||
|
||
use Illuminate\Http\Request; | ||
use App\Http\Controllers\Controller; | ||
|
||
use App\User; | ||
|
||
class PwdController extends Controller | ||
{ | ||
/** | ||
* Change password event | ||
* @param void $r laravel request object | ||
* @return string json | ||
*/ | ||
public function change(Request $r) | ||
{ | ||
//form validation | ||
$validation = \Validator::make($r->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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}); | ||
} | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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' | ||
}); | ||
} | ||
} | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters