diff --git a/blokus.php b/blokus.php index 32a4494..9b23392 100644 --- a/blokus.php +++ b/blokus.php @@ -88,6 +88,7 @@ public function routeRequest($input) { $router->add('GET', 'accounts', 'show_users'); // Mapping GET /accounts to show_users function $router->add('GET', 'accounts/{id}', 'getUserProfile'); // Mapping GET /accounts/{id} to getUserProfile function + //user functions $router->add('POST', 'users/register', function($input) { registerUser($input['username'], $input['password'], $input['email']); @@ -97,6 +98,7 @@ public function routeRequest($input) { }); $router->add('POST', 'users/logout', 'logoutUser'); // POST /users/logout -> logoutUser function $router->add('GET', 'users/session', 'checkSession'); // GET /users/session -> checkSession function +$router->add('GET', 'users/user', 'getUserProfilef'); // Mapping DELETE /accounts/{id} to deleteUser function // Handle the request $input = json_decode(file_get_contents('php://input'), true); diff --git a/lib/users.php b/lib/users.php index 3e6b980..f7d40c3 100755 --- a/lib/users.php +++ b/lib/users.php @@ -68,6 +68,18 @@ function checkSession() { } } +function isLoggedIn() { + session_start(); + + if (isset($_SESSION['user_id'])) { + return true; + } else { + return false; + } +} + + + function resetPassword($email) { try { $pdo = getDatabaseConnection(); @@ -115,4 +127,36 @@ function updatePassword($userId, $newPassword) { echo json_encode(['success' => false, 'message' => 'Error: ' . $e->getMessage()]); } } + + +function getUserProfilef() { + session_start(); + + $userId = $_SESSION['user_id']; + + $pdo = getDatabaseConnection(); // Get the PDO connection here + try { + // Modify the query to fetch the season_id, or join with another table if needed + $sql = "SELECT users.id, users.username, users.email, users.created_at, season.season_id + FROM users + LEFT JOIN season ON users.id = season.user_id + WHERE users.id = ?"; + + $stmt = $pdo->prepare($sql); + $stmt->execute([$userId]); + $user = $stmt->fetch(PDO::FETCH_ASSOC); + + if ($user) { + echo json_encode($user, JSON_PRETTY_PRINT); // Return the user's profile data as JSON + } else { + echo json_encode(['error' => 'User not found']); + } + } catch (PDOException $e) { + echo json_encode(['error' => 'Error in getUserProfile: ' . $e->getMessage()]); + } +} + + + + ?> \ No newline at end of file