PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, February 4, 2022

[FIXED] Input Facebook Data (Gender, Birthdate, Address and Bio) into database with Laravel Socialite

 February 04, 2022     facebook-graph-api, facebook-php-sdk, laravel, laravel-socialite, php     No comments   

Issue

I have created a complete code which allows Facebook Login by getting Name, Email and Avatar of the user in the database by using Laravel Socialite.

I did it by writing this:

$user = User::create([
                'email' => $providerUser->getEmail(),
                'name' => $providerUser->getName(),
                'avatar' => $providerUser->getAvatar(),
                'password' => md5(rand(1,10000)),
            ]);

But for putting Gender, BirthDate and Address in the database, I tried this.

$user = User::create([
                'email' => $providerUser->getEmail(),
                'name' => $providerUser->getName(),
                'avatar' => $providerUser->getAvatar(),
                'gender' => $providerUser->getGender(),
                'bio' => $providerUser->getBio(),
                'password' => md5(rand(1,10000)),
            ]);

But, there are no functions such as getGender() or getBio(). Do you know what they are?

I have also asked necessary permissions to Facebook Driver in SocialController.php

public function redirect()
  {
      return Socialite::driver('facebook')->fields([
        'first_name', 'last_name', 'email', 'gender', 'birthday'
        ])->scopes([
            'email', 'user_birthday'
        ])->redirect();
  }

All necessary stuffs like Database Tables, Migrations, Models and a working project without Gender and Bio is ready. But I need to add more info in database. How can I do so?

I search such questions already on StackOverflow but didn't find a satisfying answer. I hope someone answers this.

Thank you in advance.


Solution

Default socialite provider for Facebook does not have the method to get gender & bio.

For more info, You can check the available methods here:

https://github.com/laravel/socialite/blob/3.0/src/Contracts/User.php https://github.com/laravel/socialite/blob/3.0/src/AbstractUser.php

This StackOverflow might help you:

How to get birthday gender and religion information using laravel socialite implementing facebook Api

Apart from getting the scope in redirect you also need to pass them in call back

 public function handleFacebookCallback()
{
    $facebook_user = Socialite::driver('facebook')->fields([
        'first_name', 'last_name', 'email', 'gender', 'birthday'
    ])->user();
}

Check available helper methods

https://github.com/laravel/socialite/blob/3.0/src/AbstractUser.php

  • offsetExists('gender')
  • offsetGet('gender')

These methods can help get extra info from users array



Answered By - Ajit Bohra
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing