PHP code example of ussoccerfederation / soccer-id-sdk-php
1. Go to this page and download the library: Download ussoccerfederation/soccer-id-sdk-php library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
ussoccerfederation / soccer-id-sdk-php example snippets
ad .env - not needed if using real environment variables
(Dotenv\Dotenv::createImmutable(__DIR__))->load();
$logger = new StdoutLogger(); // Can also point to your PSR/log instance (ex: Monolog)
$ussfAuth = new UssfAuth(
auth0: new Auth0Client(
auth0Configuration: Auth0Configuration::fromEnv(), // Load from environment variables
logger: new StdoutLogger(), // Can specify your own PSR/log-compatible logger, such as Monolog
),
identity: new IdentityClient(new IdentityClientConfiguration()), // can be `null` if you don't need profiles
);
$ussfAuth->login();
$session = $ussfAuth->callback(function (Auth0Session $session, ?object $profile) {
// $session will contain information related to Auth0, such as name, email, access token, etc.
// $profile will contain additional information about the user. Data shape is configurable.
// This is where you can sync the user's info with your database.
// Remember to set any cookies or do any other session management here
// You can also update the user's profile by returning an array of key-value pairs to change:
// return [
// 'name' => 'John Doe',
// ];
});
$ussfAuth->logout('/index.php'); // Redirect them back to index.php after logout
use \USSoccerFederation\UssfAuthSdkPhp\Auth\Store\CookieStore;
use \USSoccerFederation\UssfAuthSdkPhp\Auth\Store\SessionStore;
use \USSoccerFederation\UssfAuthSdkPhp\Logging\StdoutLogger;
use \USSoccerFederation\UssfAuthSdkPhp\Auth\Auth0Client;
// ...
$logger = new StdoutLogger(); // Feel free to substitute your own PSR/log logger instance instead
// All session properties are optional; safe defaults will be chosen for you
// This will hold information about the user when they are logged in
$statefulStore = new SessionStore(
prefix: 'my_app_auth', // Session fields used by Soccer ID are prefixed with this
cookieTtlSeconds: 600, // 10 minutes - or whatever you want
cookiePath: '/',
cookieDomain: '',
cookieSecure: true, // HTTPS only
cookieSamesite: 'Strict', // Or use 'Lax' if you prefer
);
// This holds temporary data during the login process
$transientStore = new CookieStore(
cookieName: 'my_app_cookie',
cookieSecret: $_ENV['APP_COOKIE_SECRET'], // Used to encrypt/decrypt the cookie. Keep this secured
);
$authClient = new Auth0Client(
auth0Configuration: Auth0Configuration::fromEnv(), // Load from environment variables
statefulStore: $statefulStore,
transientStore: $transientStore,
logger: $logger,
);
$authClient->login(); // Will redirect browser
$authClient->callback(); // Will automatically handle code-exchange for you and set up the user's session
$session = $authClient->getSession();
$loggedIn = ($session !== null);
if( $loggedIn ) {
$userId = $session->user['sub'];
$userEmail = $session->user['email'] ?? null; // Should work if the `email` claim was requested
$accessToken = $session->accessToken;
$idToken = $session->idToken;
}
// Option 1: Clear all data on the user (stateful & transient). The user will, effectively, be considered
// logged out within your application. You may continue to run additional code after calling this.
$authClient->flushStores();
// Option 2: Log the user out locally & on the IdP end. This redirects the user, so you cannot run additional
// code after this. After the session has been terminated on the IdP end, the user is redirected back to the
// given route, or to the configured logout URI otherwise.
$authClient->logout('/index.php');
use \USSoccerFederation\UssfAuthSdkPhp\Logging\StdoutLogger;
use \USSoccerFederation\UssfAuthSdkPhp\Identity\IdentityClient;
use \USSoccerFederation\UssfAuthSdkPhp\Identity\IdentityClientConfiguration;
$identityClient = new IdentityClient(
configuration: IdentityClientConfiguration::fromEnv(), // Or manually configure if you'd like
logger: new StdoutLogger()
);
// The below code assumes the user had already logged into your application and that you have their access_token
$profile = $identityClient->getProfile($accessToken);
$fullName = $profile?->first_name . ' ' . $profile?->last_name;
$identityClient->updateProfile($accessToken, [
'example_property' => 'abc123'
]);
return [
'auth0' => [
/* Get these from U.S. Soccer */
'client_id' => env('USSF_AUTH0_CLIENT_ID'),
'client_secret' => env('USSF_AUTH0_CLIENT_SECRET'),
'domain' => env('USSF_AUTH0_DOMAIN'),
/* Use a long, random secret for cookie encryption */
'cookie_secret' => env('USSF_AUTH0_COOKIE_SECRET'),
/* The route that the user will be directed back to handle Auth0 code exchange and complete login */
'callback_route' => env('USSF_AUTH0_CALLBACK_ROUTE', '/ussf_callback.php'),
],
];
namespace App\Providers;
use Illuminate\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use Psr\Log\LoggerInterface;
use USSoccerFederation\UssfAuthSdkPhp\Auth\Auth0Client;
use USSoccerFederation\UssfAuthSdkPhp\Auth\Auth0Configuration;
use USSoccerFederation\UssfAuthSdkPhp\Identity\IdentityClient;
use USSoccerFederation\UssfAuthSdkPhp\Identity\IdentityClientConfiguration;
use USSoccerFederation\UssfAuthSdkPhp\UssfAuth;
class SoccerIdServiceProvider extends ServiceProvider
{
public function register(): void
{
//
}
public function boot(): void
{
$this->app->bind(Auth0Configuration::class, function () {
return new Auth0Configuration(
domain: config('soccerid.auth0.domain'),
clientId: config('soccerid.auth0.client_id'),
clientSecret: config('soccerid.auth0.client_secret'),
cookieSecret: config('soccerid.auth0.cookie_secret'),
callbackRoute: config('soccerid.auth0.callback_route'),
);
});
$this->app->bind(
UssfAuth::class,
function (Application $app) {
$configuration = $app->make(Auth0Configuration::class);
$logger = $app->make(LoggerInterface::class);
return new UssfAuth(
auth0: new Auth0Client(
auth0Configuration: $configuration,
logger: $logger,
),
identity: new IdentityClient(new IdentityClientConfiguration()),
);
}
);
}
}
return [
App\Providers\AppServiceProvider::class,
App\Providers\SoccerIdServiceProvider::class, // Add this
];
Route::get('/login_ussf', function (UssfAuth $ussfAuth) {
$ussfAuth->login();
});
Route::get('/logout_ussf', function (UssfAuth $ussfAuth) {
session()->invalidate(); // Or whatever you need to log out a user from your app
$ussfAuth->logout('/');
});
Route::get(config('soccerid.auth0.callback_route'), function (UssfAuth $ussfAuth) {
$ussfAuth->callback(function (Auth0Session $session, ?object $profile) {
session()->put('logged_in', true); // Example only; do whatever you need to actually log the user into your app
// Update your database with information from the Auth0 session and/or U.S. Soccer profile
// Example:
//
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.