├── .gitignore ├── LICENSE.md ├── README.md ├── composer.json └── src ├── SocialAccount.php ├── SocialController.php ├── SocialServiceProvider.php ├── migrations └── 2014_10_12_000001_creat_social_accounts_table.php └── routes └── web.php /.gitignore: -------------------------------------------------------------------------------- 1 | /nbproject/private/ -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Sushant Pimple 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Laravel Socialite API 2 | A powerful package designed to implement social signup and signin using access_token in Laravel. This package takes care of Facebook and Google+ social signup and signin as of now. We'll be adding more services soon. 3 | ## Installation 4 | This package is availabe on composer. 5 | ```composer require pimplesushant/laravelsocialiteapi``` 6 | 7 | Alternatively you can edit your ```composer.json``` and add 8 | "pimplesushant/laravelsocialiteapi": "^1.0" 9 | and ```composer update``` 10 | ## Usage 11 | To use this package you'll need to follow required steps of [Laravel Passport](https://laravel.com/docs/passport). You can make following changes in files as follows : 12 | 13 | **1. /.env** 14 | ``` 15 | DB_DATABASE= 16 | DB_USERNAME= 17 | DB_PASSWORD= 18 | 19 | FACEBOOK_CLIENT_ID= 20 | FACEBOOK_CLIENT_SECRET= 21 | FACEBOOK_CLIENT_REDIRECT= 22 | 23 | GOOGLE_CLIENT_ID= 24 | GOOGLE_CLIENT_SECRET= 25 | GOOGLE_CLIENT_REDIRECT= 26 | GOOGLE_DEVELOPER_KEY= 27 | ``` 28 | 29 | Then, 30 | ``` 31 | php artisan migrate 32 | ``` 33 | and 34 | ``` 35 | php artisan passport:install 36 | ``` 37 | 38 | **2. /app/User.php** 39 | ``` 40 | use Laravel\Passport\HasApiTokens; 41 | use HasApiTokens; 42 | ``` 43 | 44 | and 45 | 46 | ``` 47 | public function social_accounts() 48 | { 49 | return $this->hasMany(\Pimplesushant\Laravelsocialiteapi\SocialAccount::class)->with('social_accounts'); 50 | } 51 | ``` 52 | 53 | **3. /app/Providers/AuthServicePorvider.php** 54 | ``` 55 | use Laravel\Passport\Passport; 56 | ``` 57 | and 58 | ``` 59 | Passport::routes(); //in boot() 60 | ``` 61 | 62 | **4. /config/auth.php** 63 | ``` 64 | 'api' => [ 65 | 'driver' => 'passport', 66 | 'provider' => 'users', 67 | ], 68 | ``` 69 | 70 | **5. /config/services.php** 71 | ``` 72 | 'facebook' => [ 73 | 'client_id' => env('FACEBOOK_CLIENT_ID'), 74 | 'client_secret' => env('FACEBOOK_CLIENT_SECRET'), 75 | 'redirect' => env('FACEBOOK_CLIENT_REDIRECT') 76 | ], 77 | 78 | 'google' => [ 79 | 'client_id' => env('GOOGLE_CLIENT_ID'), 80 | 'client_secret' => env('GOOGLE_CLIENT_SECRET'), 81 | 'redirect' => env('GOOGLE_CLIENT_REDIRECT') 82 | ] 83 | ``` 84 | 85 | Now you can serve the application and hit the route ```/social-login``` with ```provider``` (e.g. facebook, google) and ```access_token``` (Access token retrieved from social service providers) 86 | 87 | ## License 88 | Licensed under the MIT License 89 | 90 | ## Author 91 | Pimple Suhsant (https://pimplesushant.in) 92 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "pimplesushant/laravelsocialiteapi", 3 | "description": "Social Login for Laravel API", 4 | "authors": [ 5 | { 6 | "name": "Sushant Pimple", 7 | "email": "pimplesushant@gmail.com" 8 | } 9 | ], 10 | "require": { 11 | "laravel/passport": "^7.0", 12 | "laravel/socialite": "^4.0" 13 | }, 14 | "autoload": { 15 | "psr-4": { 16 | "Pimplesushant\\Laravelsocialiteapi\\": "src/" 17 | } 18 | }, 19 | "extra": { 20 | "laravel": { 21 | "providers": [ 22 | "Pimplesushant\\Laravelsocialiteapi\\SocialServiceProvider" 23 | ] 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/SocialAccount.php: -------------------------------------------------------------------------------- 1 | belongsTo(User::class); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/SocialController.php: -------------------------------------------------------------------------------- 1 | input('provider'); 17 | switch($provider){ 18 | case SocialAccount::SERVICE_FACEBOOK: 19 | $social_user = Socialite::driver(SocialAccount::SERVICE_FACEBOOK)->fields([ 20 | 'name', 21 | 'first_name', 22 | 'last_name', 23 | 'email' 24 | ]); 25 | break; 26 | case SocialAccount::SERVICE_GOOGLE: 27 | $social_user = Socialite::driver(SocialAccount::SERVICE_GOOGLE) 28 | ->scopes(['profile','email']); 29 | break; 30 | default : 31 | $social_user = null; 32 | } 33 | 34 | abort_if($social_user == null , 422,'Provider missing'); 35 | 36 | $social_user_details = $social_user->userFromToken($request->input('access_token')); 37 | 38 | abort_if($social_user_details == null , 400,'Invalid credentials'); //|| $fb_user->id != $request->input('userID') 39 | 40 | $account = SocialAccount::where("provider_user_id",$social_user_details->id) 41 | ->where("provider",$provider) 42 | ->with('user')->first(); 43 | 44 | if($account){ 45 | return $this->issueToken($account->user); 46 | } 47 | else { 48 | $user = User::where("email",$social_user_details->getEmail())->first(); 49 | if(!$user){ 50 | $user = new User; 51 | // switch($provider){ 52 | // case SocialAccount::SERVICE_FACEBOOK: 53 | // $user->first_name = $social_user_details->user['first_name']; 54 | // $user->last_name = $social_user_details->user['last_name']; 55 | // break; 56 | // case SocialAccount::SERVICE_GOOGLE: 57 | // $user->first_name = $social_user_details->user['name']['givenName']; 58 | // $user->last_name = $social_user_details->user['name']['familyName']; 59 | // break; 60 | // default : 61 | // } 62 | $user->name = $social_user_details->getName(); 63 | $user->email = $social_user_details->getEmail(); 64 | // $user->username = $social_user_details->getEmail(); 65 | $user->password = Hash::make('social'); 66 | $user->save(); 67 | } 68 | $social_account = new SocialAccount; 69 | $social_account->provider = $provider; 70 | $social_account->provider_user_id = $social_user_details->id; 71 | $user->social_accounts()->save($social_account); 72 | return $this->issueToken($user); 73 | } 74 | } 75 | 76 | private function issueToken(User $user) { 77 | 78 | $userToken = $user->token() ?? $user->createToken('socialLogin'); 79 | 80 | return [ 81 | "token_type" => "Bearer", 82 | "access_token" => $userToken->accessToken 83 | ]; 84 | } 85 | } 86 | -------------------------------------------------------------------------------- /src/SocialServiceProvider.php: -------------------------------------------------------------------------------- 1 | loadMigrationsFrom(__DIR__.'/migrations'); 18 | } 19 | 20 | /** 21 | * Register services. 22 | * 23 | * @return void 24 | */ 25 | public function register() 26 | { 27 | $this->app->make('Pimplesushant\Laravelsocialiteapi\SocialController'); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/migrations/2014_10_12_000001_creat_social_accounts_table.php: -------------------------------------------------------------------------------- 1 | increments('id'); 18 | $table->string('provider')->nullable(); 19 | $table->unsignedBigInteger('user_id'); 20 | $table->foreign('user_id')->references('id')->on('users'); 21 | $table->string('provider_user_id')->unique(); 22 | $table->timestamps(); 23 | }); 24 | } 25 | 26 | /** 27 | * Reverse the migrations. 28 | * 29 | * @return void 30 | */ 31 | public function down() 32 | { 33 | Schema::dropIfExists('social_accounts'); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/routes/web.php: -------------------------------------------------------------------------------- 1 |