public function handleRegistration(Request $httpRequest)
    {
        // Abort if disabled
        if (!$this->config["enabled"]) {
            return;
        }

        $user = $this->picoAuth->getUser();
        if ($user->getAuthenticated()) {
            $this->picoAuth->redirectToPage("index");
        }

        $this->picoAuth->addAllowed("register");
        $this->picoAuth->setRequestFile($this->picoAuth->getPluginPath() . '/content/register.md');

        // Check the form submission
        $post = $httpRequest->request;
        if ($post->has("username")
            && $post->has("email")
            && $post->has("password")
            && $post->has("password_repeat")
        ) {
            // CSRF validation
            if (!$this->picoAuth->isValidCSRF($post->get("csrf_token"), self::REGISTER_CSRF_ACTION)) {
                $this->picoAuth->redirectToPage("register");
            }
            
            // Abort if a limit for maximum users is exceeded
            $this->assertLimits();

            // Registration fields
            $reg = array(
                "username" => strtolower(trim($post->get("username"))),
                "email" => trim($post->get("email")),
                "password" => new Password($post->get("password")),
                "passwordRepeat" => new Password($post->get("password_repeat")),
            );

            $isValid = $this->validateRegistration($reg);

            if ($isValid) {
                // Check if the action is not rate limited
                if (!$this->limit->action("registration")) {
                    $this->session->addFlash("error", $this->limit->getError());
                    $this->picoAuth->redirectToPage("register");
                }

                $this->logSuccessfulRegistration($reg);

                $userData = array('email' => $reg["email"]);

                $localAuth = $this->picoAuth->getContainer()->get('LocalAuth');
                $localAuth->userDataEncodePassword($userData, $reg["password"]);

                $this->storage->saveUser($reg["username"], $userData);

                $this->session->addFlash("success", "Registration completed successfully, you can now log in.");
                $this->picoAuth->redirectToLogin();
            } else {
                // Prefill the old values to the form
                $this->session->addFlash("old", array(
                    'username' => $reg["username"],
                    'email' => $reg["email"]
                ));

                // Redirect back and display errors
                $this->picoAuth->redirectToPage("register");
            }
        }
    }