How to pass auth0 access tokken in Wordpress url to login?

Viewed 11

I have two apps one is develop in react and one is in Wordpress. Both the applications have login page and only login if user is valid in auth0. I am looking for some help like if users already logged in the react application and open wordpress application in new tab then it will not ask for login again or by pass login page and directly enters in the wordpress website. Anyyone have any idea on this. I used auth0 sdk to authenticate the users from auth0.

<?php
declare(strict_types=1);

use Auth0\Quickstart\Application;

use Auth0\SDK\API\Authentication;
use Auth0\SDK\Auth0;
use Auth0\SDK\Configuration\SdkConfiguration;
use Auth0\SDK\Utility\HttpResponse;
use Auth0\SDK\Token;
// Import the Composer Autoloader to make the SDK classes accessible:
require 'vendor/autoload.php';

/**
* This file bootstraps our application.
*/

require __DIR__ . DIRECTORY_SEPARATOR . 'public/bootstrap.php';

// Import the files necessary for our Quickstart Application.
foreach ([
'vendor/autoload.php', // Composer autoloader, for our dependencies, such as the SDK 
 itself.
// These classes are application boilerplate and not directly relevant to SDK usage:
'src/ApplicationRouter.php',
'src/ApplicationTemplates.php',
'src/ApplicationErrorHandler.php',
// Import our Application class, where our app logic resides, and where we'll make our 
SDK calls.
'src/Application.php',
] as $import) {
require_once join(DIRECTORY_SEPARATOR, [APP_ROOT, $import]);
}
// Load our environment variables from the .env file:
(Dotenv\Dotenv::createImmutable(__DIR__))->load();


try {
$configuration = new SdkConfiguration([
    'domain' => $_ENV['AUTH0_DOMAIN'],
    'cookieSecret' => uniqid(),
    'clientId' => $_ENV['AUTH0_CLIENT_ID'],
    'redirectUri' => 'http://127.0.0.1:3000/callback',
    'audience' => [$_ENV['AUTH0_AUDIENCE']],
    'scope' => ['openid profile', 'openid', 'profile', 'read'],
    'clientSecret' => $_ENV['AUTH0_CLIENT_SECRET'],
    'Issuer'=> 'https://m3techdevelop.us.auth0.com/',
    'Realm' => 'Username-Password-Authentication',
    'EnableAuth0' => true,
    'connection' => 'con_y8vfupM7o6CN0fhn'
]);
$sdk = new Auth0($configuration);

header("Content-Type:application/json");
$inputData  = file_get_contents('php://input');
$data       = json_decode($inputData);

if(!$data){
   return ;
}

$username = $data->user_login;
$password = $data->user_password; 
$auth0    = new \Auth0\SDK\API\Authentication($configuration);
$request  = $sdk->authentication()->login($username, $password,'Username-Password- 
Authentication');
$decoded = HttpResponse::decodeContent($request);
if(isset($decoded['error'])){
    echo json_encode(array('status' => 'ERROR', 'message' =>  $decoded['error'], 'data' 
 => null)); exit();
}
// $access_token = 'eyJhbGciOiJkaXIiLCJlbmMiOiJBMjU2R0NNIiwiaXNzIjoiaHR0cHM6Ly9tM3RlY2hkZXZlbG9wLnVzLmF1dGgwLmNvbS8ifQ..Qow1fYemmnbuTth3.sS-MPCxSq1leLaiXDTqrvYIamFrUtbwos--Azfbl0HNCdmHTEnbqo6ANVUmwshthjUMsODWTYKxduaD55XyCOp-9perWheEumIA_QSm_E8Y-owb9ogGdWzuxNfX0sQHNk7iTgX1G9eabcoSTGDRmpaItIpbTo1STMv0S93WuEX5-qSWljNWMhImiWvALUst6MUqnJdTQdmwLjHevHmck8eBOxeDdywHEA0R_BBq2sb7wzPSQlynPIc2EdhPFBC_dKT_kDaCbul2sxIpdGhillveifPqvFE7kjan7M5RtUHQdvgVXa6TZ3LtdGLEmszOjC9aU95PS0izIvXBftJyhFg7l-vOy7OxC2dxQXB5_hfmcC2k.4-Fk7bzS6jnuKHR0sngcsA';
$access_token = $decoded['access_token'];
if($access_token){
    $responseToken = $sdk->authentication()->userInfo($access_token);
    $userData = HttpResponse::decodeContent($responseToken);

    echo json_encode(array('status' => 'OK', 'message' => 'User successfully loggedIn' , 
   'data' => $userData)); exit();
   }
   } catch (Exception $e) {
   echo json_encode(array('status' => 'ERROR', 'message' =>  'Something went wrong', 
  'data' 
   => null)); exit();
   }
0 Answers
Related