php Auth0 examples

May 12, 07:29 AM

  1. cat passParamters.php — pass parameters to Auth0 and have them returned to you.

<?php

$parmArray = [ ‘Parm1’ => ‘One’ , ‘Parm2’ => ‘Two’ , ‘ServerTime’ => date(‘Y-m-d H:i:s’) ];

$state = !empty($_GET[“state”]) ? $_GET[“state”] : null; // ?? $code = !empty($_GET[“code”]) ? $_GET[“code”] : null;
require ‘vendor/autoload.php’;
use Auth0SDKAuth0;

define(‘redirectUrl’ , ( $_SERVER[‘HTTPS’] ? ‘https’ : ‘http’ ) . ‘://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘SCRIPT_NAME’] );

$auth0 = new Auth0([ ‘domain’ => ‘dev-2XXX8.auth0.com’, ‘client_id’ => ‘kZvXXXX6TC’, ‘client_secret’ => ‘4pXXXW0’, ‘redirect_uri’ => constant(‘redirectUrl’), ‘scope’ => ‘openid profile email’,
]);

$userInfo = null;
try { $userInfo = $auth0->getUser();
} catch (Exception $e) { $auth0->logout(); error_log( ‘Line: ‘ . LINE . ‘ — Caught Auth0 exception: ‘ . $e->getMessage() . ‘ — exiting program.’ . “n” ); header(‘Location: ?logout=1’ ) ; exit;
}

if (!$userInfo) { // We have no user info // pass the param in $state variable

$state = http_build_query($parmArray); $auth0->login($state, null, []); // normally the first parm is ‘null’ unless you want auth0 to return passed values! exit; } else { // User is authenticated $userInfo = $auth0->getUser(); printf( ‘Hello %s!’, htmlspecialchars( $userInfo[‘name’] ) ); echo ‘
Logout‘; $get_string = $state; // $state is passed back from Auth0! echo ‘
Result:
‘; parse_str($get_string, $get_array); echo $get_string; echo ‘
‘; print_r($get_array); echo ‘
‘; phpinfo(); // See below for how to display user information }
  1. cat prefillEmailAddressMark.php

require ‘vendor/autoload.php’;
use Auth0SDKAuth0;

define(‘defaultEmail’ , ‘mark@edwardsmark.com’ );

define(‘redirectUrl’ , ( $_SERVER[‘HTTPS’] ? ‘https’ : ‘http’ ) . ‘://’ . $_SERVER[‘HTTP_HOST’] . $_SERVER[‘SCRIPT_NAME’] );

$auth0 = new Auth0([ ‘domain’ => ‘dev-2aXXXX8.auth0.com’, ‘client_id’ => ‘kZXXXXXTC’, ‘client_secret’ => ‘4XXXXXXXXW0’, ‘redirect_uri’ => constant(‘redirectUrl’), ‘scope’ => ‘openid profile email’,
]);

$userInfo = null;
try { $userInfo = $auth0->getUser();
} catch (Exception $e) { $auth0->logout(); error_log( ‘Line: ‘ . LINE . ‘ — Caught Auth0 exception: ‘ . $e->getMessage() . ‘ — exiting program.’ . “n” ); header(‘Location: ?logout=1’ ) ; exit;
}

if (!$userInfo) { // We have no user info // pass login_hint in additonalParams array

$additionalParams= array(‘login_hint’ => constant(‘defaultEmail’) ); $auth0->login(null, null, $additionalParams); exit; } else { $userInfo = $auth0->getUser(); printf( ‘Hello %s!’, htmlspecialchars( $userInfo[‘name’] ));; phpinfo(); // User is authenticated // See below for how to display user information }
Mark Edwards

,

---

Commenting is closed for this article.

---