Skip to content

Commit

Permalink
testing seesions
Browse files Browse the repository at this point in the history
  • Loading branch information
KotsiosDimis committed Dec 17, 2024
1 parent 4662efd commit 4aa1431
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
2 changes: 2 additions & 0 deletions blokus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand All @@ -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);
Expand Down
44 changes: 44 additions & 0 deletions lib/users.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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()]);
}
}




?>

0 comments on commit 4aa1431

Please sign in to comment.