create-berna-stencil 2.0.5 → 2.0.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,38 +0,0 @@
1
- <?php
2
- declare(strict_types=1);
3
-
4
- require_once CORE_PATH . '/modules/Response.php';
5
- require_once __DIR__ . '/../../../database/models/User.php';
6
-
7
- if ($method !== 'POST') {
8
- Response::error('Method not allowed', 405);
9
- }
10
-
11
- //
12
- // Your protected endpoint logic here. You can access route parameters in $requestParams array
13
- //
14
-
15
- $input = json_decode(file_get_contents('php://input'), true) ?? [];
16
-
17
- $email = trim(filter_var($input['email'] ?? '', FILTER_SANITIZE_EMAIL));
18
- $password = trim($input['password'] ?? '');
19
-
20
- if (empty($email) || empty($password)) {
21
- Response::error('Missing fields', 400);
22
- }
23
-
24
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
25
- Response::error('Invalid email', 400);
26
- }
27
-
28
- $user = new User();
29
- $found = $user->findByEmail($email);
30
-
31
- if (!$found || !password_verify($password, $found['password'])) {
32
- Response::error('Invalid credentials', 401);
33
- }
34
-
35
- unset($found['password']);
36
- Response::success([
37
- 'user' => $found,
38
- ]);
@@ -1,44 +0,0 @@
1
- <?php
2
- declare(strict_types=1);
3
-
4
- require_once CORE_PATH . '/modules/Response.php';
5
- require_once __DIR__ . '/../../../database/models/User.php';
6
-
7
- if ($method !== 'POST') {
8
- Response::error('Method not allowed', 405);
9
- }
10
-
11
- //
12
- // Your protected endpoint logic here. You can access route parameters in $requestParams array
13
- //
14
-
15
-
16
- $input = json_decode(file_get_contents('php://input'), true) ?? [];
17
-
18
- $nickname = htmlspecialchars(strip_tags(trim($input['nickname'] ?? '')));
19
- $email = trim(filter_var($input['email'] ?? '', FILTER_SANITIZE_EMAIL));
20
- $password = trim($input['password'] ?? '');
21
-
22
- if (empty($nickname) || empty($email) || empty($password)) {
23
- Response::error('Missing fields', 400);
24
- }
25
-
26
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
27
- Response::error('Invalid email', 400);
28
- }
29
-
30
- if (strlen($password) < 8) {
31
- Response::error('Password must be at least 8 characters', 400);
32
- }
33
-
34
- try {
35
- $user = new User();
36
- $newId = $user->create($nickname, $email, $password);
37
- http_response_code(201);
38
- Response::success(['id' => $newId]);
39
- } catch (PDOException $e) {
40
- if ($e->getCode() === '23000') {
41
- Response::error('Nickname or email already exists', 409);
42
- }
43
- Response::error('Database error', 500);
44
- }
@@ -1,9 +0,0 @@
1
- USE example_db;
2
-
3
- CREATE TABLE IF NOT EXISTS users (
4
- id INT AUTO_INCREMENT PRIMARY KEY,
5
- nickname VARCHAR(50) NOT NULL UNIQUE,
6
- email VARCHAR(255) NOT NULL UNIQUE,
7
- password VARCHAR(255) NOT NULL,
8
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
9
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
@@ -1,61 +0,0 @@
1
- <?php
2
- declare(strict_types=1);
3
-
4
- require_once __DIR__ . '/../Database.php';
5
-
6
- class User {
7
- private PDO $db;
8
-
9
- public function __construct() {
10
- $this->db = Database::getInstance();
11
- }
12
-
13
- public function getAll(): array {
14
- return $this->db->query("SELECT id, nickname, email, created_at FROM users")->fetchAll();
15
- }
16
-
17
- public function getById(int $id): ?array {
18
- $stmt = $this->db->prepare("SELECT id, nickname, email, created_at FROM users WHERE id = :id");
19
- $stmt->execute(['id' => $id]);
20
- return $stmt->fetch() ?: null;
21
- }
22
-
23
- public function findByEmail(string $email): ?array {
24
- $stmt = $this->db->prepare("SELECT id, nickname, email, password, created_at FROM users WHERE email = :email");
25
- $stmt->execute(['email' => filter_var(trim($email), FILTER_SANITIZE_EMAIL)]);
26
- return $stmt->fetch() ?: null;
27
- }
28
-
29
- public function create(string $nickname, string $email, string $password = ''): int {
30
- $stmt = $this->db->prepare("INSERT INTO users (nickname, email, password) VALUES (:nickname, :email, :password)");
31
- $stmt->execute([
32
- 'nickname' => htmlspecialchars(strip_tags(trim($nickname))),
33
- 'email' => filter_var(trim($email), FILTER_SANITIZE_EMAIL),
34
- 'password' => $password !== '' ? password_hash($password, PASSWORD_BCRYPT) : '',
35
- ]);
36
- return (int)$this->db->lastInsertId();
37
- }
38
-
39
- public function update(int $id, array $data): bool {
40
- $fields = [];
41
- $params = ['id' => $id];
42
-
43
- if (isset($data['nickname'])) {
44
- $fields[] = 'nickname = :nickname';
45
- $params['nickname'] = htmlspecialchars(strip_tags($data['nickname']));
46
- }
47
- if (isset($data['email'])) {
48
- $fields[] = 'email = :email';
49
- $params['email'] = filter_var($data['email'], FILTER_SANITIZE_EMAIL);
50
- }
51
-
52
- if (empty($fields)) return false;
53
-
54
- $sql = "UPDATE users SET " . implode(', ', $fields) . " WHERE id = :id";
55
- return $this->db->prepare($sql)->execute($params);
56
- }
57
-
58
- public function delete(int $id): bool {
59
- return $this->db->prepare("DELETE FROM users WHERE id = :id")->execute(['id' => $id]);
60
- }
61
- }