Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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: 28 additions & 0 deletions examples/authTokens.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wie ist das nochmal bei PHP mit Groß/Kleinschreibung der Files? (Config einmal groß und authTokens klein).


// Run development server with "CORBADO_PROJECT_ID=<Project ID> CORBADO_API_SECRET=<API secret> php -S localhost:3000" and
// open http://localhost:3000/authTokens.php?corbadoAuthToken=<Auth token> in your browser

require_once __DIR__ . '/../vendor/autoload.php';

$config = Corbado\Config::fromEnv();
$sdk = new Corbado\SDK($config);

try {
$corbadoAuthToken = $_GET['corbadoAuthToken'] ?? '';
$remoteAddress = $_SERVER['REMOTE_ADDR'] ?? '';
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? '';

$request = new Corbado\Generated\Model\AuthTokenValidateReq();
$request->setToken($corbadoAuthToken);
$request->setClientInfo(Corbado\SDK::createClientInfo($remoteAddress, $userAgent));

// Returns $response on valid auth token, throws exception on invalid auth token
$response = $sdk->authTokens()->validate($request);

echo $response->getData()->getUserId();

} catch (Throwable $e) {
// Handle exception (see https://github.com/corbado/corbado-php?tab=readme-ov-file#error-handling for more details)
echo $e->getMessage();
}
20 changes: 20 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,26 @@ public function __construct(string $projectID, string $apiSecret = '')
$this->apiSecret = $apiSecret;
}

/**
* @throws AssertException
* @throws ConfigException
*/
public static function fromEnv(): self
{
$projectID = getenv('CORBADO_PROJECT_ID');
$apiSecret = getenv('CORBADO_API_SECRET');

if ($projectID === false) {
throw new Exceptions\ConfigException('Environment variable "CORBADO_PROJECT_ID" not set');
}

if ($apiSecret === false) {
throw new Exceptions\ConfigException('Environment variable "CORBADO_API_SECRET" not set');
}

return new self($projectID, $apiSecret);
}

/**
* @throws AssertException
*/
Expand Down