protected function finishPasswordReset(array $resetData)
    {
        if (time() > $resetData['validity']) {
            $this->session->remove("pwreset");
            $this->session->addFlash("error", "Page validity expired, please try again.");
            $this->picoAuth->redirectToLogin();
        }

        $this->picoAuth->addOutput("isReset", true);
        $this->picoAuth->setRequestFile($this->picoAuth->getPluginPath() . '/content/pwreset.md');

        // Check the form submission
        $post = $this->httpRequest->request;
        if ($post->has("new_password")
            && $post->has("new_password_repeat")
        ) {
            $newPassword = new Password($post->get("new_password"));
            $newPasswordRepeat = new Password($post->get("new_password_repeat"));
            $username = $resetData['user'];

            // CSRF validation
            if (!$this->picoAuth->isValidCSRF($post->get("csrf_token"))) {
                $this->picoAuth->redirectToPage("password_reset");
            }

            if ($newPassword->get() !== $newPasswordRepeat->get()) {
                $this->session->addFlash("error", "The passwords do not match.");
                $this->picoAuth->redirectToPage("password_reset");
            }

            // Check password policy
            $localAuth = $this->picoAuth->getContainer()->get('LocalAuth');
            if (!$localAuth->checkPasswordPolicy($newPassword)) {
                $this->picoAuth->redirectToPage("password_reset");
            }

            // Remove pwreset record on successful submission
            $this->session->remove("pwreset");

            // Save the new userdata
            $userData = $this->storage->getUserByName($username);
            $localAuth->userDataEncodePassword($userData, $newPassword);
            $this->storage->saveUser($username, $userData);
            $this->logPasswordReset($username);

            $localAuth->login($username, $userData);
            $this->picoAuth->afterLogin();
        }
    }