Guest User

Untitled

a guest
Jan 30th, 2018
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.00 KB | None | 0 0
  1. public function changePassword(Request $request)
  2. {
  3. try{
  4.  
  5. //check user token
  6. $user_model = JWTAuth::parseToken()->authenticate();
  7.  
  8. $validator = Validator::make(
  9. $request->all(),
  10. [
  11. 'email' => 'exists:users,email|required',
  12. 'current_password' => 'required',
  13. 'new_password' => 'required|min:8',
  14. 'confirm_password' => 'same:new_password'
  15. ]
  16. );
  17.  
  18. if($validator->fails()){
  19. // Validation failed - send errors in response
  20. return $this->sendErrorResponse(['errors' => $validator->getMessageBag()]);
  21. }
  22. else{
  23. $user = User::where('email',$request->json('email'))->first();
  24.  
  25. // Validation Successful
  26. if (!\Hash::check($request->json('current_password'), $user->password)){
  27. return $this->sendErrorResponse([
  28. 'errors' => [
  29. 'current_password' => ['Your current password is incorrect.']
  30. ]
  31. ]);
  32. }
  33. $user->password = \Hash::make($request->json('new_password'));
  34.  
  35. //Update user password
  36. if($user->save()){
  37.  
  38. $response_array = $user->toArray();
  39.  
  40. return $this->sendSuccessResponse(['message' => 'Password changed successfully.','data' => $response_array],true);
  41.  
  42. }else{
  43.  
  44. return $this->sendErrorResponse(['message' => array("message" => 'Oops!! error while changing password.')],true);
  45. }
  46.  
  47. }
  48.  
  49. }catch(Exception $ex){
  50.  
  51. return $this->sendErrorResponse(['errors' => array("message" =>"Error !! Please provide data. (".$ex->getMessage().") in file ".$ex->getFile()." on line".$ex->getLine())],true);
  52.  
  53. }
  54.  
  55. }
Add Comment
Please, Sign In to add comment