PHP code example of onelabs / xguard-client

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

    

onelabs / xguard-client example snippets


'xguard' => [
    'issuer'        => env('XGUARD_ISSUER'),
    'client_id'     => env('XGUARD_CLIENT_ID'),
    'client_secret' => env('XGUARD_CLIENT_SECRET'),
    'redirect'      => env('XGUARD_REDIRECT_URI'),
],

// app/Auth/MyXGuardResolver.php
namespace App\Auth;

use Onelabs\XGuardClient\Contracts\UserResolver;
use Illuminate\Contracts\Auth\Authenticatable;
use Laravel\Socialite\Two\User as SocialiteUser;

class MyXGuardResolver implements UserResolver
{
    public function resolve(SocialiteUser $ssoUser): Authenticatable
    {
        // Reject users from non-allowed domains, for example:
        if (!str_ends_with((string) $ssoUser->getEmail(), '@mycompany.com')) {
            throw new \DomainException('Hanya email @mycompany.com yang diizinkan.');
        }

        // ... your custom lookup / create logic ...
        return \App\Models\User::firstOrCreate(
            ['sso_user_id' => $ssoUser->getId()],
            ['name' => $ssoUser->getName(), 'email' => $ssoUser->getEmail()],
        );
    }
}

$this->app->bind(
    \Onelabs\XGuardClient\Contracts\UserResolver::class,
    \App\Auth\MyXGuardResolver::class,
);

public function redirect()
{
    return Socialite::driver('xguard')->redirect();
}

public function callback(Request $request)
{
    $ssoUser = Socialite::driver('xguard')->user();
    // ... your logic
}

Socialite::driver('xguard')
    ->scopes(['openid', 'profile', 'email', 'phone', 'offline_access'])
    ->redirect();
bash
php artisan vendor:publish --tag=xguard-migrations
php artisan migrate
bash
php artisan vendor:publish --tag=xguard-config
bash
php artisan vendor:publish --tag=xguard-views
# edits resources/views/vendor/xguard/components/login-button.blade.php