PHP code example of elytica / elytica-socialite

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

    

elytica / elytica-socialite example snippets


// config/services.php
'elytica_service' => [
    'client_id'     => env('ELYTICA_SERVICE_CLIENT_ID'),
    'client_secret' => env('ELYTICA_SERVICE_CLIENT_SECRET'),
    'redirect'      => env('ELYTICA_SERVICE_REDIRECT_URI'),
    'base_url'      => env('ELYTICA_SERVICE_BASE_URL'),
],

class User extends Authenticatable
{
    protected $fillable = [
        'name',
        'email',
        'password',
        'elytica_service_id',
        'elytica_service_token',
        'elytica_service_refresh_token',
        'elytica_service_token_expires_at',
    ];

    protected $hidden = [
        'password',
        'remember_token',
        'elytica_service_token',
        'elytica_service_refresh_token',
    ];

    protected function casts(): array
    {
        return [
            'email_verified_at'                => 'datetime',
            'password'                         => 'hashed',
            'elytica_service_token_expires_at' => 'datetime',
        ];
    }
}



use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;
use App\Models\User;
use Illuminate\Support\Facades\Auth;
use Laravel\Socialite\Facades\Socialite;

Route::middleware('guest')->group(function () {
    Route::get('/auth/redirect', function () {
        return Socialite::driver('elytica_service')->redirect();
    })->name('elytica_service.auth');

    Route::get('/auth/callback', function () {
        $user = Socialite::driver('elytica_service')->user();

        $authUser = User::updateOrCreate(
            ['email' => $user->getEmail()],
            [
                'name'                             => $user->getName(),
                'elytica_service_id'               => $user->getId(),
                'elytica_service_token'            => $user->token,
                'elytica_service_refresh_token'    => $user->refreshToken,
                'elytica_service_token_expires_at' => now()->addSeconds($user->expiresIn),
            ]
        );

        Auth::login($authUser, true);

        return redirect('/dashboard');
    });

    Route::get('login', function () {
        return view('welcome');
    })->name('login');
});

Route::middleware('auth')->group(function () {
    Route::post('logout', function (Request $request) {
        Auth::guard('web')->logout();
        $request->session()->invalidate();
        $request->session()->regenerateToken();

        return redirect('/');
    })->name('logout');
});

Socialite::driver('elytica_service')->scopes(['user:read', 'projects:read', 'jobs:read'])->redirect();

use Laravel\Socialite\Facades\Socialite;

$provider = Socialite::driver('elytica_service');
$newTokenData = $provider->refreshToken($user->elytica_service_refresh_token);

$user->update([
    'elytica_service_token'            => $newTokenData['access_token'],
    'elytica_service_refresh_token'    => $newTokenData['refresh_token'],
    'elytica_service_token_expires_at' => now()->addSeconds($newTokenData['expires_in']),
]);

public function handle(Request $request, Closure $next)
{
    $user = $request->user();

    if ($user && $user->elytica_service_token_expires_at?->isPast()) {
        $data = Socialite::driver('elytica_service')
            ->refreshToken($user->elytica_service_refresh_token);

        $user->update([
            'elytica_service_token'            => $data['access_token'],
            'elytica_service_refresh_token'    => $data['refresh_token'],
            'elytica_service_token_expires_at' => now()->addSeconds($data['expires_in']),
        ]);
    }

    return $next($request);
}

Socialite::driver('elytica_service')->setBaseUrl('https://staging.elytica.example');
bash
php artisan vendor:publish --provider="Elytica\Socialite\ElyticaServiceProvider" --tag=config
php artisan vendor:publish --provider="Elytica\Socialite\ElyticaServiceProvider" --tag=migrations
php artisan migrate