Social Authentication
![Software License][ico-license]
![Quality Score][ico-code-quality]
, (*1)
This package give ability to
* Sign In
* Sign Up
* Attach/Detach social network provider to the existing account, (*2)
Install
Via Composer, (*3)
``` bash
$ composer require zfort/social-auth, (*4)
Now add the service provider in config/app.php file:
```php
'providers' => [
// ...
ZFort\SocialAuth\SocialAuthServiceProvider::class,
];
You can publish the migration with:, (*5)
$ php artisan vendor:publish --provider="ZFort\SocialAuth\SocialAuthServiceProvider" --tag="migrations"
The package assumes that your users table name is called "users". If this is not the case you should manually edit the published migration to use your custom table name., (*6)
After the migration has been published you can create the social_providers table for storing supported
providers and user_has_social_provider pivot table by running the migrations:, (*7)
$ php artisan migrate
You can publish the config-file with:, (*8)
$ php artisan vendor:publish --provider="ZFort\SocialAuth\SocialAuthServiceProvider" --tag="config"
This is the contents of the published config/social-auth.php config file:, (*9)
return [
/*
|--------------------------------------------------------------------------
| Additional service providers
|--------------------------------------------------------------------------
|
| The social providers listed here will enable support for additional social
| providers which provided by https://socialiteproviders.github.io/ just
| add new event listener from the installation guide
|
*/
'providers' => [
//
],
'models' => [
/*
* When using the "UserSocialite" trait from this package, we need to know which
* Eloquent model should be used to retrieve your available social providers. Of course, it
* is often just the "SocialProvider" model but you may use whatever you like.
*/
'social' => \ZFort\SocialAuth\Models\SocialProvider::class,
/*
* User model which you will use as "SocialAuthenticatable"
*/
'user' => \App\User::class,
],
'table_names' => [
/*
|--------------------------------------------------------------------------
| Users Table
|--------------------------------------------------------------------------
|
| The table for storing relation between users and social providers. Also there is
| a place for saving "user social network id", "token", "expiresIn" if it exist
|
*/
'user_has_social_provider' => 'user_has_social_provider',
/*
|--------------------------------------------------------------------------
| Social Providers Table
|--------------------------------------------------------------------------
|
| The table that contains all social network providers which your application use.
|
*/
'social_providers' => 'social_providers'
],
'foreign_keys' => [
/*
* The name of the foreign key to the users table.
*/
'users' => 'user_id',
/*
* The name of the foreign key to the socials table
*/
'socials' => 'social_id'
],
/*
|--------------------------------------------------------------------------
| Authentication redirection
|--------------------------------------------------------------------------
|
| Redirect path after success/error login via social network
|
*/
'redirect' => '/home'
];
Or you can publish and modify view templates with:, (*10)
$ php artisan vendor:publish --provider="ZFort\SocialAuth\SocialAuthServiceProvider" --tag="views"
Also you can publish and modify translation file:, (*11)
$ php artisan vendor:publish --provider="ZFort\SocialAuth\SocialAuthServiceProvider" --tag="lang"
Add credetials to your project
File .env, (*12)
FB_ID = <FacebookID>
FB_SECRET = <FacebookSecret>
FB_REDIRECT = <your.domain>/social/facebook/callback
GOOGLE_ID = <GoogleID>
GOOGLE_SECRET = <GoogleSecret>
GOOGLE_REDIRECT = <your.domain>/social/google/callback
GITHUB_ID = <GithubID>
GITHUB_SECRET = <GithubSecret>
GITHUB_REDIRECT = <your.domain>/social/github/callback
File config/services.php, (*13)
'facebook' => [
'client_id' => env('FB_ID'),
'client_secret' => env('FB_SECRET'),
'redirect' => env('FB_REDIRECT')
],
'google' => [
'client_id' => env('GOOGLE_ID'),
'client_secret' => env('GOOGLE_SECRET'),
'redirect' => env('GOOGLE_REDIRECT')
],
'github' => [
'client_id' => env('GITHUB_ID'),
'client_secret' => env('GITHUB_SECRET'),
'redirect' => env('GITHUB_REDIRECT')
]
After that, create your social providers in the database, (*14)
SocialProvider::create(['label' => 'Facebook', 'slug' => 'facebook']);
SocialProvider::create(['label' => 'Google', 'slug' => 'google']);
SocialProvider::create(['label' => 'github', 'slug' => 'Github']);
Or add rows directly, (*15)
You can add additional scopes and parameters to the social auth request, (*16)
SocialProvider::create([
'label' => 'github',
'slug' => 'Github',
'scopes' => ['foo', 'bar'],
'parameters' => ['foo' => 'bar']
]);
To override default scopes, (*17)
$SocialProvider->setScopes(['foo', 'bar'], true);
@include('social-auth::attach') // for authenticated user to attach/detach another socials
@include('social-auth::buttons') // for guests to login via
Prepare your user model
Implement SocialAuthenticatable interface, (*18)
Add UserSocialite trait to your User model, (*19)
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use ZFort\SocialAuth\Traits\UserSocialite;
use ZFort\SocialAuth\Contracts\SocialAuthenticatable;
class User extends Model implements SocialAuthenticatable
{
use UserSocialite;
...
}
Routes
If you need do some custom with social flow, you should define yourself controllers and
put your custom url into routes file., (*20)
For example, (*21)
Route::get('social/{social}', 'Auth\SocialAuthController@getAccount');
Route::get('social/{social}/callback', 'Auth\SocialAuthController@callback');
Route::get('social/{social}/detach', 'SocialAuthController@deleteAccount');
In case if you no need any special functionality ypu can use our default controllers, (*22)
Customize for your project
Custom User Model
User model we takes from the config('social-auth.models.user');, (*23)
User Fields Mapping
SocialAuthenticatable interface contains method mapSocialData for mapping social fields for user model
If you need you can redefine this method for your preferences project in your UserModel, (*24)
Change log
Please see CHANGELOG for more information on what has changed recently., (*25)
Testing
bash
$ composer test, (*26)
Contributing
Please see CONTRIBUTING and CONDUCT for details., (*27)
Security
If you discover any security related issues, please email developer@zfort.com instead of using the issue tracker., (*28)
Credits
License
The MIT License (MIT). Please see License File for more information., (*29)