public function register(array $data): void {
echo $this->view->render("web/auth/register", $data);
}
public function handleRegister(): void {
if (!$_POST["name"] || !$_POST["email"] || !$_POST["password"]) {
$this->redirectWithError("/register", "Informe todos os dados.");
} else {
$data = (object) filter_input_array(INPUT_POST, FILTER_SANITIZE_FULL_SPECIAL_CHARS);
$users = (new User())->find("email = :email", "email={$data->email}")->count();
if ($users) {
$this->redirectWithError("/register", "Já existe um usuário cadastrado com o e-mail informado.");
} else {
$user = new User();
$user->name = $data->name;
$user->email = $data->email;
$user->password = password_hash($data->password, PASSWORD_DEFAULT);
$user->save();
if ($user->fail()) {
$this->redirectWithError("/register", "Erro ao cadastrar-se, tente novamente.");
} else {
$this->router->redirect("/app/home");
}
}
}
}
public function redirectWithError(string $route, string $error) {
var_dump($error);
$this->router->redirect($route, ["error" => $error]);
}