Using Laravel Socialite with an API? Using Laravel Socialite with an API? laravel laravel

Using Laravel Socialite with an API?


Just found my answer. Need to use stateless in both calls:

Socialite::with('facebook')->stateless()->redirect()->getTargetUrl()$fb_user = Socialite::with('facebook')->stateless()->user();

Hope this helps someone.


I made SocialController.php and url (POST request) /api/social-login which accepts provider and access_token.

SocialAccount here is a laravel model where you'll provider and provider_user_id and local database user id. Below is the example of social_accounts table

enter image description here

And in SocialController :

<?phpnamespace App\Http\Controllers;use App\Http\Controllers\Controller;use Illuminate\Support\Facades\Hash;use Illuminate\Http\Request;use App\User;use App\SocialAccount;use Socialite;class SocialController extends Controller{public function social(Request $request) {    $provider = $request->input('provider');    switch($provider){        case SocialAccount::SERVICE_FACEBOOK:            $social_user = Socialite::driver(SocialAccount::SERVICE_FACEBOOK)->fields([                'name',                 'first_name',                 'last_name',                 'email'            ]);            break;        case SocialAccount::SERVICE_GOOGLE:            $social_user = Socialite::driver(SocialAccount::SERVICE_GOOGLE)            ->scopes(['profile','email']);            break;        default :            $social_user = null;    }    abort_if($social_user == null , 422,'Provider missing');    $social_user_details = $social_user->userFromToken($request->input('access_token'));    abort_if($social_user_details == null , 400,'Invalid credentials'); //|| $fb_user->id != $request->input('userID')    $account = SocialAccount::where("provider_user_id",$social_user_details->id)            ->where("provider",$provider)            ->with('user')->first();    if($account){        return $this->issueToken($account->user);    }    else {         // create new user and social login if user with social id not found.        $user = User::where("email",$social_user_details->getEmail())->first();        if(!$user){              // create new social login if user already exist.            $user = new User;            switch($provider){                case SocialAccount::SERVICE_FACEBOOK:                    $user->first_name = $social_user_details->user['first_name'];                    $user->last_name = $social_user_details->user['last_name'];                    break;                case SocialAccount::SERVICE_GOOGLE:                    $user->first_name = $social_user_details->user['name']['givenName'];                    $user->last_name = $social_user_details->user['name']['familyName'];                    break;                default :            }                        $user->email = $social_user_details->getEmail();            $user->username = $social_user_details->getEmail();            $user->password = Hash::make('social');            $user->save();        }        $social_account = new SocialAccount;        $social_account->provider = $provider;        $social_account->provider_user_id = $social_user_details->id;        $user->social_accounts()->save($social_account);        return $this->issueToken($user);    }} private function issueToken(User $user) {    $userToken = $user->token() ?? $user->createToken('socialLogin');    return [        "token_type" => "Bearer",        "access_token" => $userToken->accessToken    ];}}

EDIT:

I have created package for the same https://packagist.org/packages/pimplesushant/laravelsocialiteapi