Automatically manage user persistence and resolution for any Laravel Socialite provider.
genealabs/laravel-socialiter is a Laravel package for automatically manage user persistence and resolution for any laravel socialite provider..
It currently has 134 GitHub stars and 429.465 downloads on Packagist (latest version 13.0.0).
Install it with composer require genealabs/laravel-socialiter.
Discover more Laravel packages by genealabs
or browse all Laravel packages to compare alternatives.
Last updated

This is an MIT-licensed open source project with its ongoing development made possible by the support of the community. If you'd like to support this, and our other packages, please consider sponsoring us via the button above.
We thank the following sponsors for their generosity, please take a moment to check them out:
Install the composer package:
composer require genealabs/laravel-socialiter
Add the social credentials table:
php artisan migrate
To prevent automatic migrations from running (for example if you have a different migration setup, like multi-tenancy, etc.), add the following entry to your app's service provider:
<?php
namespace App\Providers;
use GeneaLabs\LaravelSocialiter\Socialiter;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
public function register()
{
//
}
public function boot()
{
Socialiter::ignoreMigrations();
}
}
And then publish the migration files and manipulate them as needed:
php artisan vendor:publish --provider="GeneaLabs\LaravelSocialiter\Providers\ServiceProvider" --tag=migrations
Update the user model:
use GeneaLabs\LaravelSocialiter\Traits\SocialCredentials;
class User extends Authenticatable {
use SocialCredentials;
...
}
By default, Socialiter creates new users with just their name, email, and a
random password. If you need to customize how users are created (for example, to
set additional attributes or use a custom creation flow), register a callback in
your AppServiceProvider:
use GeneaLabs\LaravelSocialiter\Socialiter;
use Laravel\Socialite\Contracts\User as SocialiteUser;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
Socialiter::createUsersUsing(function (SocialiteUser $socialiteUser) {
return \App\Models\User::create([
'name' => $socialiteUser->getName(),
'email' => $socialiteUser->getEmail(),
'password' => \Illuminate\Support\Str::random(64),
'avatar' => $socialiteUser->getAvatar(),
'locale' => 'en',
// ... any additional attributes
]);
});
}
}
The callback receives the raw Socialite user object and must return a persisted
User model instance. If no callback is registered, the default behavior is
used. You can reset to default at any time with
Socialiter::createUsersUsingDefault().
Note: The custom callback is only invoked when creating a new user. If an existing user with the same email is found, Socialiter links the social credentials to that user without invoking the callback.
The following is an example controller implementation using the "Sign in with Apple" driver:
<?php
namespace App\Http\Controllers\Auth;
use App\Http\Controllers\Controller;
use GeneaLabs\LaravelSocialiter\Socialiter;
use Illuminate\Http\RedirectResponse;
use Laravel\Socialite\Facades\Socialite;
class SignInWithAppleController extends Controller
{
public function redirectToProvider() : RedirectResponse
{
// use Socialite, as before
return Socialite::driver("sign-in-with-apple")
->scopes(["name", "email"])
->redirect();
}
public function handleProviderCallback()
{
// but handle the callback using Socialiter
$user = (new Socialiter)
->driver("sign-in-with-apple")
->login();
// or you can use the facade:
$user = Socialiter::driver("sign-in-with-apple")
->login();
// or you can use the app binding:
$user = app("socialiter")
->driver("sign-in-with-apple")
->login();
}
}
During package development I try as best as possible to embrace good design and development practices, to help ensure that this package is as good as it can be. My checklist for package development includes:
Please observe and respect all aspects of the included Code of Conduct.
When reporting issues, please fill out the included template as completely as possible. Incomplete issues may be ignored or closed if there is not enough information included to be actionable.
Please review the Contribution Guidelines. Only PRs that meet all criterium will be accepted.
We have included the awesome symfony/thanks composer package as a dev dependency. Let your OS package maintainers know you appreciate them by starring the packages you use. Simply run composer thanks after installing this package. (And not to worry, since it's a dev-dependency it won't be installed in your live environment.)