PHP code example of aslnbxrz / oneid-socialite

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

    

aslnbxrz / oneid-socialite example snippets


'oneid' => [
    'client_id'     => env('ONEID_CLIENT_ID'),
    'client_secret' => env('ONEID_CLIENT_SECRET'),
    'redirect'      => env('ONEID_REDIRECT_URI'),
    // Optional (defaults shown):
    'base_url'      => env('ONEID_BASE_URL', 'https://sso.egov.uz'),
    'scope'         => env('ONEID_SCOPE', 'one_code'),
],

use Illuminate\Support\Facades\Event;
use SocialiteProviders\Manager\SocialiteWasCalled;
use SocialiteProviders\OneID\Provider; // or Aslnbxrz\OneID\Provider

public function boot(): void
{
    Event::listen(function (SocialiteWasCalled $event) {
        $event->extendSocialite('oneid', Provider::class);
    });
}

use Laravel\Socialite\Facades\Socialite;

// Redirect to OneID
Route::get('/auth/oneid/redirect', function () {
    return Socialite::driver('oneid')->redirect();
});

// Callback
Route::get('/auth/oneid/callback', function () {
    /** @var \Aslnbxrz\OneID\OneIDUser $user */
    $user = Socialite::driver('oneid')->user();

    // Standard fields
    $id    = $user->getId();
    $name  = $user->getName();
    $email = $user->getEmail();

    // Custom fields
    $pinfl    = $user->getPinfl();
    $sessId   = $user->getSessionId();
    $passport = $user->getPassport();
    $phone    = $user->getPhone();
    $gender   = $user->getGender();

    // TODO: login/register user logic
});

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use Laravel\Socialite\Facades\Socialite;

// --- Variant A: using access_token directly ---
Route::post('/api/auth/oneid/token', function (Request $request) {
    $validated = $request->validate([
        'access_token' => '        'pinfl'    => $user->getPinfl(),
        'sess_id'  => $user->getSessionId(),
        'passport' => $user->getPassport(),
        'phone'    => $user->getPhone(),
        'gender'   => $user->getGender(),
    ]);
});

// --- Variant B: exchanging authorization code ---
Route::post('/api/auth/oneid/code', function (Request $request) {
    $validated = $request->validate([
        'code' => '

use GuzzleHttp\Exception\GuzzleException;
use Laravel\Socialite\Facades\Socialite;

// $accessTokenOrSessionId - access_token or sess_id
try {
    Socialite::driver('oneid')->logout($accessTokenOrSessionId);
} catch (GuzzleException $e) {
    // Handle OneID logout request error
}