Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Sam's avatar
Level 4

Socialite - Connecting Social Accounts

Hello all,

First off im going to start with a disclaimer and say that this post may seem like like a bit of a muddle and I apologise for that.

Anyway so im starting up a new little project that allows the following:

  1. User Signs Up For Account (without social media)
  2. User is then prompted to 'connect' their social media accounts, facebook, twitter etc
  3. Certain information is then obtained about the user such as DOB, Amount of Followers/Friends and their likes

Obviously there is more to the project than this but I was just wondering if Socialite will allow me to do all of the above from the social integration side of things. I know I can register a user etc but is it possible to obtain further information from a particular social network using the 'scopes' method?

Secondly, obviously we would want the users data to be updated regularly, is there anyway to do this say once a day, poll the social network sites for a users follow count/likes/post number etc or would this be reliant on the user having to login and re-authenticate?

I hope the above makes sense, I'd be more than happy to clear things up further if required.

TIA

0 likes
13 replies
thomaskim's avatar

Yes, you can connect accounts. Typically (except for Twitter), they will provide you with the user's email, which you can use to connect accounts.

Yes, you can overwrite the default scopes.

http://laravel.com/docs/5.1/authentication

Before redirecting the user, you may also set "scopes" on the request using the scope method. This method will overwrite all existing scopes:

return Socialite::driver('github')
            ->scopes(['scope1', 'scope2'])->redirect();

Yes, I believe you can update certain information, but you should look at the social network's API for that.

1 like
kurucu's avatar

@thomaskim It's not unusual for people to have different email addresses for different OAuth providers. For example, using a work address for LinkedIn, amongst other examples.

Sam's avatar
Level 4

Thank you for clarifying those points.

So just to clarify further, once the user has authenticated/added to the system I won't have to re-request authentication to pull data from the social networks which in turn would update the systems database should I desire?

Basically what I'm trying to get at is, can I just access the users social data when I require once they have authenticated once on the system or do tokens etc expire?

TIA

jekinney's avatar

I sent up a steppe rate table and link via has many with user id this way if data is missing, like Twitter and no email, doesn't matter.

Pretty simple for login via social, if comes back true I login via login by id as you'll have the user id in the table.

jekinney's avatar

Can't edit via mobile but not steppe separate table.

The token received does not expire that is used for login and checks as the user allows access and the token is generated.

On login again the token is checked via api call to ensure proper access etc (true or false). At a login you probably would update the followers etc data or you can on a scheduled task.

Sam's avatar
Level 4

When using facebook - how can I use the scopes to get a users friends? Would I pass through 'user_friends' as a scope, if so how would I then obtain the information that's returned as when i do the following:

    {
        return Socialite::driver($provider)->scopes(['manage_pages', 'user_likes', 'user_friends'])->redirect();
    }

It just returns the standard information

Sam's avatar
Level 4

So after re-reading my questions they make very little sense.

Im trying to obtain a users friend list from facebook. However from reading it would seem that all Socialite is good for is general authentication, not obtaining edge scopes (in facebook's terms) such as 'user_friends'.

Here's what i'd like to obtain: https://developers.facebook.com/docs/graph-api/reference/user/likes/

So my question is, has anyone managed to obtain information from any social network using Socialite using the 'scopes' method? It would seem from several StackOverflow comments and other posts elsewhere that Socialite simply doesn't allow extended scopes?

TIA

thomaskim's avatar

Hm...are you sure that it's not working? It might be because 'user_friends' doesn't give you the user's friends. It gives you a list of the friends that also use your app. I also believe that in order for you to get 'user_likes', your app needs to be approved by FB.

Sam's avatar
Level 4

Hi @thomaskim, unfortunately the only information I get back from the authentication is the general user information.

When using the Graph debug this is what it returns:

{
  "data": [
  ],
  "summary": {
    "total_count": 346
  },
}

You are right about the 'user_likes' scope, but 'user_friends' does not require any further permissions as is approved by default.

veerle.s's avatar

Maybe this will help:

$this->socialite->driver($provider)->fields(['first_name', 'last_name', 'email', 'gender', 'verified', 'friends'])->user()

1 like
chaibialaa's avatar

Solution to connect users if they exist (not twitter) :

public function handleProviderCallback($provider)
    {

        $providerData = Socialite::driver($provider)->user();
        $user = User::where('provider_id', '=', $providerData->id)
            ->orWhere('email', '=' ,$providerData->email)
            ->first();
        if ($providerData->name == null){
            $nom="Unknown";
        } else {
            $nom=$providerData->name;
        }

        $SocialData = [
            'provider_id' => $providerData->id,
            'provider' => $provider,
            'email' => $providerData->email,
            'nom' => $nom,
            'status' => 1,
            'imagepath' => $providerData->avatar,
        ];

        if(!$user) {
            $user = User::create($SocialData);
        }

        $dbData = [
            'imagepath' => $user->imagepath,
            'email' => $user->email,
            'nom' => $user->nom,
            'provider' => $user->provider,
            'provider_id' => $user->provider_id,
        ];

        if (!empty(array_diff($SocialData, $dbData))) {
            $user->imagepath = $providerData->avatar;
            $user->email = $providerData->email;
            $user->nom = $nom;
            $user->provider = $provider;
            $user->provider_id = $providerData->id;
            $user->save();
        }

        Auth::login($user);

        return redirect($this->redirectPath())

Please or to participate in this conversation.