Guest User

Change Password API

a guest
Dec 11th, 2018
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  1.  function changePassword(Request $request)
  2.         {
  3.             try{
  4.                 $validator = Validator::make($request->all(), [
  5.             'current_pwd' => 'required',
  6.             'new_pwd'=>'required'
  7.            
  8.         ]);
  9.         if ($validator->fails()) {
  10.             $error_message=implode(",",$validator->messages()->all());
  11.            
  12.             throw new Exception($error_message,Config('constants.status_code.fail'));
  13.         }
  14.         $user = User::find(auth()->user()->id);
  15.  
  16.         if($user)
  17.         {
  18.             if(Hash::check($request->get('current_pwd'),$user->password))
  19.                 {
  20.  
  21.                     if(Hash::check($request->get('new_pwd'),$user->password))
  22.                         {
  23.                              throw new Exception("Your new password cannot be same as old password", Config('constants.status_code.fail'));
  24.                         }
  25.                     $user->password = Hash::make($request->get('new_pwd'));
  26.                     $user->save();
  27.                      return response()->json([
  28.                             'settings' => [
  29.                                 'status' => 1,
  30.                                 'statusCode' => Config('constants.status_code.success'),
  31.                                 'message' => trans('Your Password has been updated successfully')
  32.                             ],
  33.                             'data' => array()
  34.                            
  35.                 ],Config('constants.status_code.success'));
  36.  
  37.                 }
  38.                 else
  39.                 {
  40.                     throw new Exception("Invalid current password", Config('constants.status_code.fail'));
  41.                 }
  42.         }
  43.         else
  44.         {
  45.             throw new Exception("Unauthorized", Config('constants.status_code.fail'));
  46.            
  47.         }
  48.      
  49.  
  50.  
  51.             }
  52.             catch(Exception $e)
  53.             {
  54.                 return response()->json([
  55.                             'settings' => [
  56.                                 'status' => 0,
  57.                                 'statusCode' => $e->getCode(),
  58.                                 'message' => $e->getMessage()
  59.                             ],
  60.                             'data' => [
  61.                                
  62.                             ]
  63.                            
  64.                 ],$e->getCode());
  65.             }
  66.         }
Add Comment
Please, Sign In to add comment