1. Go to this page and download the library: Download audunru/social-accounts 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/ */
audunru / social-accounts example snippets
use audunru\SocialAccounts\Traits\HasSocialAccounts;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Authenticatable
{
use HasSocialAccounts;
/**
* Get user who has logged in with Google account ID 123456789
* $user = User::findBySocialAccount('google', '123456789')
*
* Retrieve all social accounts belonging to $user
* $user->socialAccounts
*/
}
'google' => [
'client_id' => env('GOOGLE_CLIENT_ID'), // Get your client ID and secret from https://console.developers.google.com
'client_secret' => env('GOOGLE_CLIENT_SECRET'),
// Note: The "redirect" setting will be configured automatically. You are not
namespace App\Providers;
use audunru\SocialAccounts\SocialAccounts;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any authentication / authorization services.
*/
public function boot()
{
$this->registerPolicies();
SocialAccounts::routes();
}
}
SocialAccounts::routes(
function ($router) {
$router->forWeb();
}
);
SocialAccounts::routes(
function ($router) {
$router->forApi();
}
);
namespace App\Providers;
use App\User;
use audunru\SocialAccounts\SocialAccounts;
use Laravel\Socialite\Contracts\User as ProviderUser;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
class AuthServiceProvider extends ServiceProvider
{
/**
* Register any authentication / authorization services.
*/
public function boot()
{
$this->registerPolicies();
/*
* If your company uses G Suite and you want to ensure that only employees can log in, you can define a "login-with-provider" gate.
*
* If you don't define this gate, any ProviderUser is allowed to pass through.
*/
Gate::define('login-with-provider', function (?User $user, ProviderUser $providerUser) {
/*
* $providerUser->user['hd'] contains the domain name the Google account belongs to.
*
* It's good practice to verify that the account does in fact belong to your company after the user has authorized with Google and returned to your application.
*/
return 'company.com' === $providerUser->user['hd'];
});
/*
* If you want to restrict who can add social accounts, you can define a "add-social-account" gate.
*
* If you don't define this gate, any authenticated user can add a social account.
*/
Gate::define('add-social-account', function (User $user, ProviderUser $providerUser) {
/*
* isAdmin() is a hypothetical method you could define on your User model.
*
* In this case, only administrators would be allowed to add social accounts.
*/
return $user->isAdmin();
});
SocialAccounts::routes();
}
}
namespace App\Listeners;
use audunru\SocialAccounts\Events\SocialUserCreated;
class AddUserAvatar
{
/**
* Handle the event.
*
* @param SocialUserCreated $event
* @return void
*/
public function handle(SocialUserCreated $event)
{
/*
* The package only saves the user's name and email when creating a new user.
*
* By listening for the event, we can grab more details about the user.
*/
$event->user->update([
'avatar' => $event->providerUser->getAvatar();
]);
}
}