21

I'm trying to use Laravel Socialite package over an api. I try to pass the code into my api to fetch the user but it keeps giving me an error:

Fatal error: Call to a member function pull() on null

Since I'm doing the request over an API, I take the following steps.

Send a request to api for the url to fetch the code:

Socialite::with('facebook')->stateless()->redirect()->getTargetUrl()

Then make a request with the above fetched url, which redirects with the code parameter.

Send the code to the api and fetch the user:

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

This is where it crashes. I'm not sure why.

I've used the package before and it works fine when I just have an app that reloads the page. But when I send it to an api (on a different domain) it crashes. I'm thinking there is some issue with how the code is generated. Is there anyway to fix this?

Rob
  • 10,851
  • 21
  • 69
  • 109
  • check out this, it may helps [laravel socialite](http://stackoverflow.com/questions/35536548/unable-to-use-laravel-socialite-with-lumen/35548759#35548759) – nooby Mar 13 '16 at 10:30

2 Answers2

41

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.

Rob
  • 10,851
  • 21
  • 69
  • 109
12

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 :

<?php

namespace 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

Sushant Pimple
  • 1,337
  • 1
  • 13
  • 26