PHP code example of icanid / icanid-sdk-php

1. Go to this page and download the library: Download icanid/icanid-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/ */

    

icanid / icanid-sdk-php example snippets


use Auth0\SDK\Auth0;

$auth0 = new Auth0([
  // The values below are found on the Application settings tab.
  'domain'        => '{YOUR_TENANT}.auth0.com',
  'client_id'     => '{YOUR_APPLICATION_CLIENT_ID}',
  'client_secret' => '{YOUR_APPLICATION_CLIENT_SECRET}',

  // This is your application URL that will be used to process the login.
  // Save this URL in the "Allowed Callback URLs" field on the Application settings tab
  'redirect_uri' => 'https://{YOUR_APPLICATION_CALLBACK_URL}',
]);

// Do we have an authenticated session available?
if ($user = $auth0->getUser()) {
  // Output the authenticated user
  print_r($user);
  exit;
}

// No session was available, so redirect to Universal Login page
$auth0->login();

use Auth0\SDK\API\Management;

$mgmt_api = new Management('{YOUR_ACCESS_TOKEN}', 'https://{YOUR_TENANT}.auth0.com');

$results = $mgmt_api->users()->getAll([
  'q' => 'josh'
]);

if (! empty($results)) {
  echo '<h2>User Search</h2>';

  foreach ($results as $datum) {
    printf(
      '<p><strong>%s</strong> &lt;%s&gt; - %s</p>',
      !empty($datum['nickname']) ? $datum['nickname'] : 'No nickname',
      !empty($datum['email']) ? $datum['email'] : 'No email',
      $datum['user_id']
    );
  }
}

use Auth0\SDK\Auth0;

$auth0 = new Auth0([
  // Found in your Auth0 dashboard, under Organization settings:
  'organization' => '{YOUR_ORGANIZATION_ID}',

  // Found in your Auth0 dashboard, under Application settings:
  'domain'       => '{YOUR_TENANT}.auth0.com',
  'client_id'    => '{YOUR_APPLICATION_CLIENT_ID}',
  'redirect_uri' => 'https://{YOUR_APPLICATION_CALLBACK_URL}',
]);

$auth0->login();

// Expects the Auth0 SDK to be configured first, as demonstrated above.
$auth0->handleInvitation();

// Expects the Auth0 SDK to be configured first, as demonstrated above.

// Returns an object containing the invitation query parameters, or null if they aren't present
if ($invite = $auth0->getInvitationParameters()) {
  // Does the invite organization match your intended organization?
  if ($invite->organization !== '{YOUR_ORGANIZATION_ID}') {
    throw new Exception("This invitation isn't intended for this service. Please have your administrator check the service configuration and request a new invitation.");
  }

  // Redirect to Universal Login using the emailed invitation
  $auth0->login(null, null, [
    'invitation'   => $invite->invitation,
    'organization' => $invite->organization
  ]);
}

use Auth0\SDK\Auth0;

// Example: a list of organizations our app supports
$allowedOrganizations = ['org_123', 'org_456'];
$defaultOrganization = $allowedOrganizations[0];

// For this scenario, do not pass any `organization` during SDK initialization. You'll handle the organization validation yourself.
$auth0 = new Auth0([
  // Found in your Auth0 dashboard, under Application settings:
  'domain'       => '{YOUR_TENANT}.auth0.com',
  'client_id'    => '{YOUR_APPLICATION_CLIENT_ID}',
  'redirect_uri' => 'https://{YOUR_APPLICATION_CALLBACK_URL}',
]);

// Are they authenticated?
if ($user = $auth0->getUser()) {
  // Do they have an organization claim?
  if (! isset($user['org_id'])) {
    // They do not; stop processing their request.
    throw new Exception('Please sign in using an organization.');
  }

  // Does the claim match an expected organization?
  if (! in_array($user['org_id'], $allowedOrganizations)) {
    // It does not; stop processing their request.
    throw new Exception('Access denied.');
  }
}

// Do we have an incoming invitation?
if ($invite = $auth0->getInvitationParameters()) {
  // Is the invite for an expected organization?
  if (! in_array($invite->organization, $allowedOrganizations)) {
    throw new Exception("This invitation isn't intended for this service. Please have your administrator check the service configuration and request a new invitation.");
  }

  // Redirect to Universal Login using the invitation
  $auth0->login(null, null, [
    'invitation'   => $invite->invitation,
    'organization' => $invite->organization
  ]);
}

// Redirect to Universal Login using our default organization
$auth0->login(null, null, [
  'organization' => $defaultOrganization
]);

bash
$ composer