Ejemplo n.º 1
0
})->setName('login');
$app->map(['GET', 'POST'], '/users/register', function ($request, $response, $args) {
    $field_errors = [];
    $user = [];
    $flash_messages = $this->flash->getMessages();
    if ($request->isPost()) {
        $user['user_name'] = filter_var($_POST['user_name'], FILTER_SANITIZE_STRING);
        $user['password'] = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
        $user['full_name'] = filter_var($_POST['full_name'], FILTER_SANITIZE_STRING);
        $user['email'] = filter_var($_POST['email'], FILTER_SANITIZE_STRING);
        $user_form = validateRegisterForm($user);
        if ($user_form['is_valid']) {
            $userService = new AuthService();
            $errors = $userService->validateUser($user);
            if (empty($errors)) {
                $userService->addUser($user);
                $this->flash->addMessage('success', 'New user added');
                $this->flash->addMessage('success', 'Please login with your credentials');
                return $response->withRedirect($this->router->pathFor('login'));
            } else {
                $flash_messages['danger'] = $errors;
            }
        } else {
            $field_errors = $user_form['has_errors'];
        }
    }
    return $this->view->render($response, 'forms/users_register.twig', ['user' => $user, 'flash_messages' => $flash_messages, 'errors' => $field_errors, 'userLogged' => isset($_SESSION['user_id']), 'csrf' => ['name' => $request->getAttribute('csrf_name'), 'value' => $request->getAttribute('csrf_value')]]);
})->setName('register');
$app->map(['GET', 'POST'], '/users/edit', function ($request, $response, $args) {
    $field_errors = [];
    $flash_messages = $this->flash->getMessages();