Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Fixed Function Definitions and Access Modifiers #31

Merged
merged 21 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 18 additions & 10 deletions sample/callback.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,29 @@
echo "Descope Project ID not present. Please check .env file.";
exit(1);
}

$descopeSDK = new DescopeSDK([
'projectId' => $_ENV['DESCOPE_PROJECT_ID']
]);

if (isset($_POST["sessionToken"]) && $descopeSDK->verify($_POST["sessionToken"])) {
$_SESSION["user"] = json_decode($_POST["userDetails"], true);
$_SESSION["sessionToken"] = $_POST["sessionToken"];

session_write_close();

// Redirect to dashboard
header('Location: dashboard.php');
exit();
if (isset($_POST["sessionToken"])) {
if ($descopeSDK->verify($_POST["sessionToken"])) {
$_SESSION["user"] = json_decode($_POST["userDetails"], true);
$_SESSION["sessionToken"] = $_POST["sessionToken"];
session_write_close();

// Redirect to dashboard
header('Location: dashboard.php');
exit();
} else {
error_log("Session token verification failed.");
$descopeSDK->logout();
// Redirect to login page
header('Location: login.php');
exit();
}
} else {
error_log("Session token is not set in POST request.");
// Redirect to login page
header('Location: login.php');
exit();
Expand Down
18 changes: 3 additions & 15 deletions sample/dashboard.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
<?php
// session_start();

require '../vendor/autoload.php';
use Descope\SDK\DescopeSDK;

session_start();

$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();

if (!isset($_ENV['DESCOPE_PROJECT_ID'])) {
echo "Descope Project ID not present. Please check .env file.";
exit(1);
}
session_start();

$descopeSDK = new DescopeSDK([
'projectId' => $_ENV['DESCOPE_PROJECT_ID']
]);
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__ . '/..');
$dotenv->load();

if (!isset($_SESSION["user"])) {
session_destroy();
Expand Down
23 changes: 21 additions & 2 deletions sample/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,32 @@ function sendFormData(sessionToken, userDetails) {
return user;
}

async function handleLogout() {

}

async function handleLogin() {
try {
// Wait for refresh to ensure token validity
console.log("Attempting to refresh the session...");
gshriki marked this conversation as resolved.
Show resolved Hide resolved
await sdk.refresh();

const sessionToken = sdk.getSessionToken();
console.log("Session token obtained:", sessionToken);

if (!sessionToken) {
console.log("Session token is missing after refresh. Redirecting to login.");
window.location.href = 'login.php'; // Redirect to login if session token is invalid
return;
}

const user = await getUserDetails();
console.log("User details obtained:", user);

sendFormData(sessionToken, user.data);
} catch (error) {
console.log("Error during login:", error);
sdk.logout();
window.location.href = 'login.php'; // Redirect to login on error
gshriki marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -57,15 +73,18 @@ function sendFormData(sessionToken, userDetails) {
console.log("Valid refresh token found. Logging in...");
handleLogin();
} else {
sdk.logout();
console.log("No valid refresh token. Displaying login form.");
const container = document.getElementById("container")
container.innerHTML = '<descope-wc project-id="' + projectId + '" flow-id="sign-up-or-in"></descope-wc>';
const wcElement = document.getElementsByTagName('descope-wc')[0];

const onSuccess = async (e) => {
console.log("Login successful, handling login.");
await handleLogin(); // Wait for login and session details
}

const onError = (err) => console.log(err);
const onError = (err) => console.log("Login error:", err);

if (wcElement) {
wcElement.addEventListener('success', onSuccess);
Expand Down
Loading