PHP code example of dutchcodingcompany / filament-socialite

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

    

dutchcodingcompany / filament-socialite example snippets


use Laravel\Socialite\Contracts\User as SocialiteUserContract;
use Illuminate\Contracts\Auth\Authenticatable;

// ...
->plugin(
    FilamentSocialitePlugin::make()
        // (   'icon' => 'fab-github',
                // (optional) Button color override, default: 'gray'.
                'color' => 'primary',
                // (optional) Button style override, default: true (outlined).
                'outlined' => false,
            ],
        ])
        // (optional) Enable/disable registration of new (socialite-) users.
        ->setRegistrationEnabled(true)
        // (optional) Enable/disable registration of new (socialite-) users using a callback.
        // In this example, a login flow can only continue if there exists a user (Authenticatable) already.
        ->setRegistrationEnabled(fn (string $provider, SocialiteUserContract $oauthUser, ?Authenticatable $user) => (bool) $user)
        // (optional) Change the associated model class.
        ->setUserModelClass(\App\Models\User::class)
        // (optional) Change the associated socialite class (see below).
        ->setSocialiteUserModelClass(\App\Models\SocialiteUser::class)
);

$table->string('password')->nullable();

->plugin(
    FilamentSocialitePlugin::make()
        ->setRegistrationEnabled(true)
        ->setDomainAllowList(['localhost'])
);

use DutchCodingCompany\FilamentSocialite\Facades\FilamentSocialite as FilamentSocialiteFacade;
use DutchCodingCompany\FilamentSocialite\FilamentSocialite;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

// Default
FilamentSocialiteFacade::setCreateUserCallback(fn (string $provider, SocialiteUserContract $oauthUser, FilamentSocialite $socialite) => $socialite->getUserModelClass()::create([
    'name' => $oauthUser->getName(),
    'email' => $oauthUser->getEmail(),
]));

FilamentSocialiteFacade::setUserResolver(fn (string $provider, SocialiteUserContract $oauthUser, FilamentSocialite $socialite) => /* ... */);

// app/Providers/Filament/AdminPanelProvider.php
->plugins([
    FilamentSocialitePlugin::make()
        // ...
        ->setSocialiteUserModelClass(\App\Models\SocialiteUser::class)

namespace App\Models;

use DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser as FilamentSocialiteUserContract;
use Illuminate\Contracts\Auth\Authenticatable;
use Laravel\Socialite\Contracts\User as SocialiteUserContract;

class SocialiteUser implements FilamentSocialiteUserContract
{
    public function getUser(): Authenticatable
    {
        //
    }

    public static function findForProvider(string $provider, SocialiteUserContract $oauthUser): ?self
    {
        //
    }
    
    public static function createForProvider(
        string $provider,
        SocialiteUserContract $oauthUser,
        Authenticatable $user
    ): self {
        //
    }
}

use DutchCodingCompany\FilamentSocialite\Models\Contracts\FilamentSocialiteUser as FilamentSocialiteUserContract;
use DutchCodingCompany\FilamentSocialite\Models\SocialiteUser;

FilamentSocialite::setLoginRedirectCallback(function (string $provider, FilamentSocialiteUserContract $socialiteUser) {
    return redirect()->intended(
        route(FilamentSocialite::getPlugin()->getDashboardRouteName())
    );
});

## in Service Provider file
public function boot()
{
    //...
    
    Filament::registerRenderHook(
        'filament-fortify.login.end',
        fn (): string => Blade::render('<x-filament-socialite::buttons />'),
    );
}

'github' => [
    'client_id' => '...',
    'client_secret' => '...',
    'scopes' => [
        // Add scopes here.
        'read:user',
        'public_repo',
    ],
]

'github' => [
    'client_id' => '...',
    'client_secret' => '...',
    'with' => [
        // Add optional parameters here
        'hd' => 'example.com',
    ],
]
bash
php artisan vendor:publish --tag="filament-socialite-migrations"
php artisan migrate
bash
php artisan vendor:publish --tag="filament-socialite-config"
php artisan vendor:publish --tag="filament-socialite-views"
php artisan vendor:publish --tag="filament-socialite-translations"
bash
php artisan vendor:publish --tag="filament-breezy-views"