PHPackages                             audunru/social-accounts - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. audunru/social-accounts

ActiveLibrary[Authentication &amp; Authorization](/categories/authentication)

audunru/social-accounts
=======================

Add social login to your Laravel app

v9.0.0(1mo ago)23.8k↑16.7%MITPHPPHP ^8.3CI passing

Since Aug 8Pushed 1mo agoCompare

[ Source](https://github.com/audunru/social-accounts)[ Packagist](https://packagist.org/packages/audunru/social-accounts)[ RSS](/packages/audunru-social-accounts/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (10)Dependencies (22)Versions (31)Used By (0)

Social Accounts for Laravel
===========================

[](#social-accounts-for-laravel)

[![Build Status](https://github.com/audunru/social-accounts/actions/workflows/validate.yml/badge.svg)](https://github.com/audunru/social-accounts/actions/workflows/validate.yml)[![Coverage Status](https://camo.githubusercontent.com/325335d420a2646835161c2ad911cb10155663adf479b83ad2466933e48e2a94/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f617564756e72752f736f6369616c2d6163636f756e74732f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/audunru/social-accounts?branch=main)

Add social login (Google, Facebook, and others) to your Laravel app.

This package uses [Laravel Socialite](https://github.com/laravel/socialite) to authenticate users, and takes care of storing the provider (eg. Google) and provider user ID (eg. 123456789) as a SocialAccount (a related model of the User model).

Your users can add one or more social logins to their account. It's up to you if you want them to sign up with a normal username and password first, or if they can sign up just by signing in with a provider.

The package also has a JSON API so you can display which social accounts a user has logged in with, and allow them to remove them.

Upgrading from earlier versions
===============================

[](#upgrading-from-earlier-versions)

Notable changes are documented in the [migration guide](MIGRATE.md).

Installation
============

[](#installation)

Step 1: Install with Composer
-----------------------------

[](#step-1-install-with-composer)

```
composer require audunru/social-accounts
```

Step 2: Make changes to your code
---------------------------------

[](#step-2-make-changes-to-your-code)

Add the `HasSocialAccounts` trait to your `User` model:

```
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
     */
}
```

Second, you need to specify which providers you are going to support. Publish the configuration, and open up `config/social-accounts.php` and add them to the array.

```
php artisan vendor:publish --tag=social-accounts-config
```

There is an array called "providers" where you can specify the ones you want:

```
'providers' => [
    // 'bitbucket',
    // 'facebook',
    // 'github',
    // 'gitlab',
    'google',
    // 'linkedin',
    // 'twitter',
],
```

Third, you need to add credentials for your supported social login providers to `config/services.php`. To login with Google, you would add the following to `config/services.php`:

```
'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 required to add it to services.php yourself. If you don't want to use this package's default routes, you will need to configure it. If so, the package will use the value from services.php.
    // 'redirect' => '/social-accounts/login/google/callback',
],
```

Step 3: Configuration and customization
---------------------------------------

[](#step-3-configuration-and-customization)

You can find the configuration and documentation of all options in [config/social-accounts.php](config/social-accounts.php).

Step 4: Run migrations
----------------------

[](#step-4-run-migrations)

```
php artisan vendor:publish --tag=social-accounts-migrations
php artisan migrate
```

The migrations will create a `social_accounts` table, which will hold all added social accounts.

If you set the "automatically\_create\_users" option in `config/social-accounts.php` to `true`, the `email` and `password` columns in your `users` table will be made nullable. Not all providers require users to have an email address, and the `password` column must be nullable because users who sign up this way won't have password.

Usage
=====

[](#usage)

Adding social login to existing users
-------------------------------------

[](#adding-social-login-to-existing-users)

If you want to allow your existing users to log in with Google, add a link to `/social-accounts/login/google` somewhere in your application:

```
@auth
Add Google login to my account
@endauth
```

After clicking on this link, the user will be redirected to Google, where they must authorize the request. Afterwards they will be returned to your app. Then, a new `SocialAccount` model will be added as related model of the `User`.

Signing up users
----------------

[](#signing-up-users)

If you want to allow users to sign up with this package, you must first publish the configuration file and then set `automatically_create_users` to `true`.

```
'automatically_create_users' => true,
```

Then, run the migrations so that the email and password columns are made nullable.

```
php artisan migrate
```

Then add a link to `/social-accounts/login/google`:

```
Sign up with Google
```

Logging in
----------

[](#logging-in)

For users who are not signed in, simply add a link to `/social-accounts/login/google`:

```
Sign in with Google
```

Remember Me
-----------

[](#remember-me)

Add "remember" to the login URL to keep the user signed in:

```
Sign in with Google
```

Redirect after login
--------------------

[](#redirect-after-login)

Add "redirect=/some-url" to the login URL to redirect the user after login:

```
Sign in with Google
```

Only relative URLs are supported.

API
---

[](#api)

The JSON API, which by default is accessible at `/social-accounts`, allows authenticated users to retrieve their social accounts and remove them.

To retrieve an array of social accounts, make a GET request to `/social-accounts`.

To retrieve a single social account, make a GET request to `/social-accounts/123`, where 123 is the ID of the account.

To delete a social account, make a DELETE request to `/social-accounts/123`.

Users can't update social accounts through the API, they will have to delete them first and then authorize again.

Optional Parameters, Access Scopes and Stateless Authentication
---------------------------------------------------------------

[](#optional-parameters-access-scopes-and-stateless-authentication)

To include optional parameters in the request, call the `registerProviderSettings` method on the facade. The method takes three parameters, the provider name, the name of the method to call on the provider object, and an array of parameters.

For example, you can use this to limit the domains a Google user can choose to sign in with to just one:

```
SocialAccounts::registerProviderSettings('google', 'with', ['hd' => 'seinfeld.com']);
```

[See Socialite's documentation for more info on Optional Parameters, Access Scopes and Stateless Authentication](https://laravel.com/docs/master/socialite#optional-parameters)

Gates
-----

[](#gates)

You can use [gates](https://laravel.com/docs/master/authorization#gates) to allow or deny certain actions. Gates should be defined within the boot method of your AppServiceProvider.

```
