diff options
| author | ilotterytea <iltsu@alright.party> | 2025-08-19 10:02:56 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-08-19 10:02:56 +0500 |
| commit | 440e8f50feda7a3c1d78c16678624bfe8eaf4db1 (patch) | |
| tree | b6e41b0cf0a0609f73fc5b35723d732f5a89312c | |
| parent | baadea8a6dc35babe226d473f0ea533fb6678a0d (diff) | |
feat: edit account information + display it
| -rw-r--r-- | account/edit.php | 99 | ||||
| -rw-r--r-- | index.php | 24 |
2 files changed, 122 insertions, 1 deletions
diff --git a/account/edit.php b/account/edit.php new file mode 100644 index 0000000..9c46c02 --- /dev/null +++ b/account/edit.php @@ -0,0 +1,99 @@ +<?php +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/partials.php'; +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/utils.php'; +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/config.php'; +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/alert.php'; + +$user = $_SESSION['user'] ?: null; + +if (!$user) { + exit(create_alert('/', 401, 'You must be authorized before editing an account', null)); +} + + +if ($_SERVER['REQUEST_METHOD'] == 'POST') { + if (!isset($_POST['password']) || !password_verify($_POST['password'], $user['password'])) { + exit(create_alert('/account/edit.php', 401, 'Incorrect password.', null)); + } + + $username = $user['username']; + $password = $user['password']; + + if (isset($_POST['new_password'])) { + $password = $_POST['new_password']; + if (strlen($password) < 7) { + exit(create_alert('/account/edit.php', 400, 'New password must be at least 8 characters long', null)); + } + $password = password_hash($password, PASSWORD_DEFAULT); + } + + if (isset($_POST['username'])) { + $username = $_POST['username']; + $count = strlen($username); + if ($count < 4 || $count > 20) { + exit(create_alert('/account/edit.php', 400, 'New username must be between 4 and 20 characters long', null)); + } + } + + $db = new PDO(DB_URL, DB_USER, DB_PASS); + + $db->prepare('UPDATE users SET username = ?, password = ? WHERE id = ?') + ->execute([$username, $password, $user['id']]); + + $user['username'] = $username; + $user['password'] = $password; + + $_SESSION['user'] = $user; + + exit(create_alert('/account/edit.php', 200, 'Success!', $user)); +} + +?> +<!DOCTYPE html> +<html> + +<head> + <title>id system</title> + <link rel="stylesheet" href="/static/style.css"> +</head> + +<body> + <main> + <?php html_navbar(); ?> + <?php display_alert(); ?> + + <form action="/account/edit.php" method="post"> + <div> + <h1>Edit account information</h1> + <table> + <tr> + <th>Username</th> + <td><input type="text" name="username" placeholder="New username" + value="<?= $user['username'] ?>"></td> + </tr> + <tr> + <th>Password</th> + <td><input type="password" name="new_password" placeholder="New password"></td> + </tr> + <tr> + <th></th> + <td></td> + </tr> + <tr> + <th></th> + <td class="column gap-8"> + <input type="password" name="password" placeholder="Enter current password" required> + <div><button type="submit">Save</button></div> + </td> + </tr> + </table> + </div> + </form> + <div> + <h1>Danger zone</h1> + <a href="/account/delete.php"><button>Delete account</button></a> + </div> + </main> +</body> + +</html>
\ No newline at end of file @@ -2,6 +2,7 @@ include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/partials.php'; include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/utils.php'; include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/config.php'; +include_once $_SERVER['DOCUMENT_ROOT'] . '/lib/alert.php'; $user = $_SESSION['user'] ?: null; ?> @@ -16,9 +17,30 @@ $user = $_SESSION['user'] ?: null; <body> <main> <?php html_navbar(); ?> + <?php display_alert(); ?> <?php if (isset($user)): ?> - <h1>Hey, <?= $user['username'] ?></h1> + <div> + <h1>Account information</h1> + <table> + <tr> + <th>Username</th> + <td><?= $user['username'] ?></td> + </tr> + <tr> + <th>Password</th> + <td>*****</td> + </tr> + <tr> + <th>Joined</th> + <td><?= $user['joined_at'] ?></td> + </tr> + <tr> + <th></th> + <td><a href="/account/edit.php"><button>Edit</button></a></td> + </tr> + </table> + </div> <?php else: ?> <div class="row gap-16"> <section class="column gap-16 grow"> |
