Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ResetPasswordPost.php
Go to the documentation of this file.
1 <?php
7 
15 
22 {
26  protected $accountManagement;
27 
32 
36  protected $session;
37 
47  public function __construct(
48  Context $context,
49  Session $customerSession,
52  CredentialsValidator $credentialsValidator = null
53  ) {
54  $this->session = $customerSession;
55  $this->accountManagement = $accountManagement;
56  $this->customerRepository = $customerRepository;
57  parent::__construct($context);
58  }
59 
67  public function execute()
68  {
70  $resultRedirect = $this->resultRedirectFactory->create();
71  $resetPasswordToken = (string)$this->getRequest()->getQuery('token');
72  $password = (string)$this->getRequest()->getPost('password');
73  $passwordConfirmation = (string)$this->getRequest()->getPost('password_confirmation');
74 
75  if ($password !== $passwordConfirmation) {
76  $this->messageManager->addError(__("New Password and Confirm New Password values didn't match."));
77  $resultRedirect->setPath('*/*/createPassword', ['token' => $resetPasswordToken]);
78 
79  return $resultRedirect;
80  }
81  if (iconv_strlen($password) <= 0) {
82  $this->messageManager->addError(__('Please enter a new password.'));
83  $resultRedirect->setPath('*/*/createPassword', ['token' => $resetPasswordToken]);
84 
85  return $resultRedirect;
86  }
87 
88  try {
89  $this->accountManagement->resetPassword(
90  null,
91  $resetPasswordToken,
92  $password
93  );
94  $this->session->unsRpToken();
95  $this->messageManager->addSuccess(__('You updated your password.'));
96  $resultRedirect->setPath('*/*/login');
97 
98  return $resultRedirect;
99  } catch (InputException $e) {
100  $this->messageManager->addError($e->getMessage());
101  foreach ($e->getErrors() as $error) {
102  $this->messageManager->addError($error->getMessage());
103  }
104  } catch (\Exception $exception) {
105  $this->messageManager->addError(__('Something went wrong while saving the new password.'));
106  }
107  $resultRedirect->setPath('*/*/createPassword', ['token' => $resetPasswordToken]);
108 
109  return $resultRedirect;
110  }
111 }
$session
$accountManagement
$customerRepository
__()
Definition: __.php:13
__construct(Context $context, Session $customerSession, AccountManagementInterface $accountManagement, CustomerRepositoryInterface $customerRepository, CredentialsValidator $credentialsValidator=null)