PHP code example of ptpn / ion-client

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

    

ptpn / ion-client example snippets


use IonClient;

$loginUrl = IonClient::getLoginUrl(
    redirectUri: 'https://app.example.com/auth/callback',
);

return redirect($loginUrl);

use Illuminate\Support\Facades\Route;
use Ptpn\IonClient\IonClient;

Route::get('/auth/callback', function (\Illuminate\Http\Request $request) {
    return app(IonClient::class)->callback($request);
});

use Illuminate\Support\Facades\Route;
use IonClient;

Route::get('/auth/callback', function (\Illuminate\Http\Request $request) {
    return IonClient::callback($request);
});

use IonClient;

// Cek session
$session = IonClient::checkSession($sessionId);

// Verifikasi auth code
$user = IonClient::verify('AUTH_CODE_HERE');

// Ambil data session lengkap
$fullInfo = IonClient::getSessionFullInfo($sessionId);

// Ambil role user untuk aplikasi tertentu
$roles = IonClient::getUserRoles($sessionId, 'hris');

// Heartbeat agar session tetap aktif
IonClient::heartbeat($sessionId);

// Logout user (trigger pemutusan session ke SSO)
IonClient::logout($sessionId);

use Illuminate\Http\Request;
use Ptpn\IonClient\IonClient;

class AuthController extends Controller
{
    protected $ion;

    public function __construct(IonClient $ion)
    {
        $this->ion = $ion;
    }

    public function logout(Request $request)
    {
        try {
            $data = $this->ion->logout($request->input('session_id'));

            return response()->json($data);
        } catch (\Ptpn\IonClient\Exceptions\IonClientException $e) {
            return response()->json(['error' => $e->getMessage()], 500);
        }
    }
}

$roles = explode(';', $response['data']['user_roles']);

use Ptpn\IonClient\Exceptions\IonClientException;

try {
    $user = IonClient::verify('AUTH_CODE_HERE');
} catch (IonClientException $e) {
    // $e->getMessage() berisi pesan error dari ION
    logger()->error($e->getMessage());
}
bash
php artisan vendor:publish --tag=ion-client-config