PHP code example of cboxdk / laravel-id-client

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

    

cboxdk / laravel-id-client example snippets


use Cbox\Id\Client\Migration\{LegacyLogin, LegacyUser};

Route::post('/cbox-legacy', LegacyLogin::using(function (string $email, string $password): ?LegacyUser {
    $row = DB::connection('legacy')->table('users')->where('email', $email)->first();

    return $row && Hash::check($password, $row->password)
        ? new LegacyUser($row->email, $row->name, $row->confirmed_at !== null, $row->password)
        : null;
}));

use Cbox\Id\Client\Facades\CboxId;

// routes/web.php
Route::get('/auth/redirect', fn () => CboxId::redirect());          // → Cbox ID login

Route::get('/auth/callback', function (\Illuminate\Http\Request $request) {
    $cbox = CboxId::authenticate($request);   // verifies state, PKCE, id_token

    $user = User::updateOrCreate(
        ['cbox_id' => $cbox->id],                              // the stable `sub`
        ['email' => $cbox->email, 'name' => $cbox->name],
    );

    auth()->login($user);
    return redirect('/dashboard');
});

$principal = CboxId::principal();

$principal->organization()?->role;             // OrganizationRole::Admin
$principal->hasPermission('invoices:create');
$principal->isSupportSession();                // staff acting as this person

Route::middleware(['auth', 'cbox-id.org:admin'])->get('/team/settings', …);
Route::middleware(['auth', 'cbox-id.permission:invoices:create'])->post('/invoices', …);

CboxId::principal()?->hasFeature('new-dashboard');

Route::middleware(['auth', 'feature:new-dashboard'])->get('/dashboard', …);

$tokens = CboxId::refresh($refreshToken);   // persist $tokens->refreshToken — it rotates

$cbox = CboxId::fake();

$cbox->actingAs('user_1', organization: 'org_1', permissions: ['invoices:create']);
$this->withToken((string) $cbox->token()->permissions(['reports:read']))->getJson('/api/reports');
$cbox->management()->assertInvited('[email protected]');

// config/cbox-id-client.php — CBOX_ID_PUBLISHABLE_KEY
use Cbox\Id\Client\Frontend\FrontendClient;

$config = app(FrontendClient::class)->config();
$acme = app(FrontendClient::class)->config('acme');   // the buttons acme's hosted page shows

$config->endpoint('authorization');  // where the form posts on to
$config->social;                     // the buttons this environment has enabled
$config->accent();                   // the customer's brand colour
$config->isLive();                   // false for a pk_test_ key — draw the badge

$session = app(FrontendClient::class)->session($accessToken);

$session->signedIn();          // false is a state, not an error
$session->user?->initials();   // 'AL' — for the avatar fallback

Route::get('/account', fn () => CboxId::redirectToProfile(returnTo: route('dashboard')));
// or just the URL: CboxId::profileUrl(route('dashboard'))

$token   = CboxId::machineToken(['api.read']);       // client-credentials (M2M)
$claims  = CboxId::userinfo($accessToken);           // OIDC userinfo
$active  = CboxId::introspect($token)['active'];      // RFC 7662
CboxId::revoke($refreshToken, 'refresh_token');      // RFC 7009

$ok = CboxId::verifyWebhook(
    payload: $request->getContent(),                 // the RAW body
    signatureHeader: $request->header('X-Cbox-Signature'),
    secret: config('services.cbox.webhook_secret'),
);
abort_unless($ok, 400);

use Cbox\Id\Client\Facades\CboxIdWebhooks;

CboxIdWebhooks::on('organization.member_added', fn ($e) => Seat::allocate($e->string('user_id')));
CboxIdWebhooks::on('organization.member_removed', fn ($e) => Seat::release($e->string('user_id')));
CboxIdWebhooks::on('role.assigned', fn ($e) => /* … */);
CboxIdWebhooks::on('*', fn ($e) => Log::info('cbox event', ['type' => $e->type]));

use Cbox\Id\Client\Facades\CboxIdApi;          // config: CBOX_ID_MANAGEMENT_KEY, CBOX_ID_WORKSPACE_KEY
use Cbox\Id\Client\Management\EnvironmentClient;

$env = CboxIdApi::environment();                // or: new EnvironmentClient(baseUrl: 'https://acme.cboxid.com', apiKey: $key)

$app = $env->apps->create(['name' => 'Billing', 'type' => 'web', 'redirect_uris' => [$callback]])->data;
$app->clientSecret;                             // in this response and no other — store it now

$secret = $env->apps->secrets->rotate($app->id, ['grace_seconds' => 3600])->data;

foreach ($env->organizations->listAll(['status' => 'active']) as $organization) {
    // every page, fetched lazily
}

$created = CboxIdApi::workspace()->environments->create(['name' => 'Staging', 'type' => 'sandbox'])->data;