Git Product home page Git Product logo

laravel-passport-socialite's Introduction

Laravel Passport Socialite

The missing social authentication plugin (i.e. SocialGrant) for laravel passport.

Description

This package helps integrate social login using laravel's native packages i.e. (passport and socialite). This package allows social login from the providers that is supported in laravel/socialite package.

Getting Started

To get started add the following package to your composer.json file using this command.

composer require schedula/laravel-passport-socialite

Configuration

When composer installs this package successfully, register the Schedula\Laravel\PassportSocialite\PassportSocialiteServiceProvider::class in your config/app.php configuration file.

'providers' => [
    // Other service providers...
    Schedula\Laravel\PassportSocialite\PassportSocialiteServiceProvider::class,
],

Note: You need to configure third party social provider keys and secret strings as mentioned in laravel socialite documentation https://laravel.com/docs/5.6/socialite#configuration

Usage

Step 1 - Setting up the User model

Implement UserSocialAccount on your User model and then add method findForPassportSocialite. findForPassportSocialite should accept two arguments i.e. $provider and $id

$provider - string - will be the social provider i.e. facebook, google, github etc.

$id - string - is the user id as per social provider for example facebook's user id 1234567890

And the function should find the user which is related to that information and return user object or return null if not found

Below is how your User model should look like after above implementations.

namespace App;

use Schedula\Laravel\PassportSocialite\User\UserSocialAccount;
class User extends Authenticatable implements UserSocialAccount {
    
    use HasApiTokens, Notifiable;

    /**
    * Find user using social provider's id
    * 
    * @param string $provider Provider name as requested from oauth e.g. facebook
    * @param string $id User id of social provider
    *
    * @return User
    */
    public static function findForPassportSocialite($provider,$id) {
        $account = SocialAccount::where('provider', $provider)->where('provider_user_id', $id)->first();
        if($account) {
            if($account->user){
                return $account->user;
            }
        }
        return;
    }
}

Note: SocialAccount here is a laravel model where I am saving provider and provider_user_id and local database user id. Below is the example of social_accounts table

id provider provider_user_id user_id created_at updated_at
1 facebook XXXXXXXXXXXXXX 1 XX-XX-XX XX:XX:XX XX-XX-XX XX:XX:XX
2 github XXXXXXXXXXXXXX 2 XX-XX-XX XX:XX:XX XX-XX-XX XX:XX:XX
3 google XXXXXXXXXXXXXX 3 XX-XX-XX XX:XX:XX XX-XX-XX XX:XX:XX

Step 2 - Getting access token using social provider

I recommend you to not to request for access token from social grant directly from your app since the logic / concept of social login is you need to create account if it doesn't exists or else login if exists.

So here in this case we will be making a custom route and a controller that will recieve the Access Token or Authorization Token from your client i.e. Android, iOS etc. application. Here client fetches access token / authorization token from provider

Our route here can be something like this:

Route::post('/auth/social/facebook', 'SocialLogin@loginFacebook');

And here is how we can write our controller and its method for that :

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Route;
class SocialLogin extends Controller {

	public function loginFacebook(Request $request) {
		try {

			$facebook = Socialite::driver('facebook')->userFromToken($request->accessToken);
			if(!$exist = SocialAccount::where('provider',  SocialAccount::SERVICE_FACEBOOK)->where('provider_user_id', $facebook->getId())->first()){
				
				// create user account
			}
			return response()->json($this->issueToken($request, 'facebook', $request->accessToken));
		}
		catch(\Exception $e) {
			return response()->json([ "error" => $e->getMessage() ]);
		}
		
	}
    
	public function issueToken($request, $provider, $accessToken) {
		
		/**
		* Here we will request our app to generate access token 
		* and refresh token for the user using its social identity by providing access token 
		* and provider name of the provider. (I hope its not confusing)
		* and then it goes through social grant and which fetches providers user id then calls 
		* findForPassportSocialite from your user model if it returns User object then it generates 
		* oauth tokens or else will throw error message normally like other oauth requests.
		*/
		$params = [
			'grant_type' => 'social',
			'client_id' => 'your-client-id', // it should be password grant client
			'client_secret' => 'client-secret',
			'accessToken' => $accessToken, // access token from provider
			'provider' => $provider, // i.e. facebook
		];
		$request->request->add($params);
		
		$requestToken = Request::create("oauth/token", "POST");
		$response = Route::dispatch($requestToken);
		
		return json_decode((string) $response->content(), true);
	}
}

Note: SocialGrant will only accept access token not authorization token, for example google provides authorization token in android when requested server auth code i.e. offline access, so you need to exchange auth code for an access token. Refer here: https://github.com/google/google-api-php-client

Note: SocialGrant acts similar to PasswordGrant so make sure you use client id and secret of password grant while making oauth request

That's all folks

Wait you still here?

I am working on this cool project called yoheim. It's an all in one collaboration platform to manage and share ssh servers. Download now and start using it for free www.yoheim.com

yoheim log

laravel-passport-socialite's People

Contributors

anandsiddharth avatar lenrsmith avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

laravel-passport-socialite's Issues

OAuthServerException

Hi , @anandsiddharth

I am trying to get access_token from laravel passport but the request to /oauth/token always returns error 500.

2

I'm following the readme example

1

Note:
i'm using sfelix-martins/passport-multiauth

Laravel 6.x support

Using version ^2.0 for schedula/laravel-passport-socialite
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for schedula/laravel-passport-socialite ^2.0 -> satisfiable by schedula/laravel-passport-socialite[v2.0.0].
- Conclusion: remove laravel/framework v6.0.4
- Conclusion: don't install laravel/framework v6.0.4
- schedula/laravel-passport-socialite v2.0.0 requires illuminate/support ~5.7 -> satisfiable by illuminate/support[5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, 5.8.x-dev, v5.7.0,
v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.26, v5.7.27, v5.7.28, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9, v5.8.0, v5.8.11, v5.8.12, v5.8.14, v5.8.15, v5.8.17, v5.8.18, v5.8.19, v5.8.2, v5.8.20, v5.8.22, v5.8.24, v5.8.27, v5.8.28, v5.8.29, v5.8.3, v5.8.30, v5.8.31, v5.8.32, v5.8.33, v5.8.34, v5.8.35, v5.8.4, v5.8.8, v5.8.9].
- don't install illuminate/support 5.7.17|don't install laravel/framework v6.0.4
- don't install illuminate/support 5.7.18|don't install laravel/framework v6.0.4

and goes on.

Cannot get oAuth token or get this package to work

@anandsiddharth thanks for the effort but I am trying to get this working as an end-to-end example so I can both move on to other parts of my app, and that this helps more people that are stuck. I'm just not able to figure out how to get this working.

Here's my code. I followed all the instructions in the readme. I even included some sample github and google API keys in my .env.example, so you can copy that as-is for your .env file.

https://gitlab.com/connecteev/_laravel_auth_socialite_with_laravel_passport/tree/schedula_laravel_passport_socialite
I also created a readme which contains the setup instructions. I also recorded some short videos in trying to get this (and social auth in general using laravel passport + socialite) working.

I'm stuck on step 1 and am not able to get a valid token.
POST http://localhost:8000/api/auth/social/github

This is the error I get back:

{
    "error": "Client error: `GET https://api.github.com/user?access_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3Q6ODAwMFwvYXBpXC92MVwvYXV0aFwvbG9naW5cL2dpdGh1YlwvY2FsbGJhY2siLCJpYXQiOjE1NzE0NDQyMDgsImV4cCI6MTU3MTQ0NzgwOCwibmJmIjoxNTcxNDQ0MjA4LCJqdGkiOiJrNjlTNHpNZ041dUhLSUtwIiwic3ViIjoyOCwicHJ2IjoiODdlMGFmMWVmOWZkMTU4MTJmZGVjOTcxNTNhMTRlMGIwNDc1NDZhYSJ9.Kbfia0I_SovgjyRsu1CVrxHWqhTKF6UBeS7aKnDlZPQ` resulted in a `401 Unauthorized` response:\n{\n  \"message\": \"Bad credentials\",\n  \"documentation_url\": \"https://developer.github.com/v3\"\n}\n\n"
}

What am I doing wrong?
How can we make this more dummy-proof (and I realize I may be the dummy here)? What would be awesome is if we can get this working and then use that as a reference going forward, possibly even in a "demo" or "example" branch in this repo itself.

Please advise...

Support Issue with laravel 6

- Conclusion: remove laravel/framework v6.18.10
- don't install illuminate/support 5.7.17|don't install laravel/framework 6.x-dev

Problem with refreshing the token

When I send refresh token that I get from social auth using Linkedin access token, I get 401, and description "The refresh token is invalid".

The Guzzle snippet that I'm using:
$response = $http->post($url, [ 'form_params' => [ 'grant_type' => 'refresh_token', 'refresh_token' => $refreshToken, 'client_id' => $client_id, 'client_secret' => $client_secret, 'scope' => '', ] ]);

Update to passport: ^7.0

- schedula/laravel-passport-socialite v1.0.2 requires laravel/passport ^5.0 || ^6.0 -> satisfiable by laravel/passport[5.0.x-dev, 6.0.x-dev, v5.0.0, v5.0.1, v5.0.2, v5.0.3, v6.0.0, v6.0.1, v6.0.2, v6.0.3, v6.0.4, v6.0.5, v6.0.6, v6.0.7] but these conflict with your requirements or minimum-stability.

Laravel 8

Hi, any update for laravel 8?
Thanks

Problem getting oauth token

When i try to get the token the response comes empty, what can i do, the client_id and secret that i am using are password clients

Google login

access token for google is it called tokenId in mobile app ??

Laravel 6.0 cannot be installed

Problem 1
- Installation request for schedula/laravel-passport-socialite ^2.0 -> satisfiable by schedula/laravel-passport-socialite[v2.0.0].
- Conclusion: remove laravel/framework v6.0.4
- Conclusion: don't install laravel/framework v6.0.4
- schedula/laravel-passport-socialite v2.0.0 requires illuminate/support ~5.7 -> satisfiable by illuminate/support[5.7.17, 5.7.18, 5.7.19, 5.7.x-dev, 5.8.x-dev, v5.7.0, v5.7.1, v5.7.10, v5.7.11, v5.7.15, v5.7.2, v5.7.20, v5.7.21, v5.7.22, v5.7.23, v5.7.26, v5.7.27, v5.7.28, v5.7.3, v5.7.4, v5.7.5, v5.7.6, v5.7.7, v5.7.8, v5.7.9, v5.8.0, v5.8.11, v5.8.12, v5.8.14, v5.8.15, v5.8.17, v5.8.18, v5.8.19, v5.8.2, v5.8.20, v5.8.22, v5.8.24, v5.8.27, v5.8.28, v5.8.29, v5.8.3, v5.8.30, v5.8.31, v5.8.32, v5.8.33, v5.8.34, v5.8.35, v5.8.36, v5.8.4, v5.8.8, v5.8.9].

Versioning error

Hi Anand

I am developing a web app with socialite and passport. But while installing your package I am facing the following versioning error. Do you have any suggestion on that ?

screen shot 2018-06-21 at 9 07 43 am

Your requirements could not be resolved to an installable set of packages.

Hello any idea about what is wrong at the moment of installing this package?

Your requirements could not be resolved to an installable set of packages.

Problem 1
- Installation request for league/oauth2-server (locked at 7.2.0) -> satisfiable by league/oauth2-server[7.2.0].
- Conclusion: don't install schedula/laravel-passport-socialite v1.0.2
- Conclusion: don't install schedula/laravel-passport-socialite v1.0.1
- Can only install one of: laravel/passport[5.0.x-dev, v6.0.3].
- Can only install one of: laravel/passport[v6.0.3, 5.0.x-dev].
- Can only install one of: laravel/passport[5.0.x-dev, v6.0.3].
- schedula/laravel-passport-socialite v1.0.0 requires laravel/passport ^5.0 -> satisfiable by laravel/passport[5.0.x-dev].
- Installation request for schedula/laravel-passport-socialite ^1.0 -> satisfiable by schedula/laravel-passport-socialite[v1.0.0, v1.0.1, v1.0.2].
- Installation request for laravel/passport (locked at v6.0.3, required as ^6.0) -> satisfiable by laravel/passport[v6.0.3].

Installation failed, reverting ./composer.json to its original content.

Cant install

Hi, i want install this package in my project, i make new project and use compose require schedula/laravel-passport-socialit but say i cant install this package because i need remove laravel/framework, How i can install this repository to use passport with socialite? is in laravel 7.4

"league/oauth2-server" requirement conflicts with the installer

there's a requirement in laravel/passport package: "league/oauth2-server": "^7.0".
And trying to install laravel-passport-socialite I'm getting the following error:

  • schedula/league-oauth2-social v1.0.2 requires league/oauth2-server ^6.0 -> satisfiable by league/oauth2-server[6.0.0, 6.0.1, 6.0.2, 6.1.0, 6.1.1].
    • don't install league/oauth2-server 6.0.0|don't install league/oauth2-server 7.2.0

Laravel 5.6;
"laravel/passport": "^6.0@dev",

Class Socialite not found error

Its showing me "Error." as error description. I tried to catch and print. Its says Class Socialite not found error.

So please fix this in
File name : laravel-passport-socialite/src/Bridge/UserSocialRepository.php
Line no : 17
Line : use Socialite;

its should be like 'use Laravel\Socialite\Facades\Socialite'. Please fix this asap. I am waiting for this.

Laravel 8 Support

Hi, is possible update Laravel 8 Support on “Packgist”, please ?

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.