Search code examples
facebooksymfonyfosuserbundlehwioauthbundle

HWIOAuthBundle get birthday, gender after Facebook Login


as per documentation I am able to configure and store Facebook ID to FOSUserBundle Entity User

I want to store birthday and gender also.

app/config/config.yml

hwi_oauth:
    firewall_name:         secured_area
    resource_owners:
        facebook:
            type:          facebook
            client_id:     <client_id>
            client_secret: <client_secret>
            scope:         "email,user_birthday"

in documentation there is some description Here but I am unable to get it work. as I am beginner here this documentation isn't helpful to me.

where can I get facebook response which contains all local data.

I need to store that data also along with other FOSUserBundle Fields.


Solution

  • Your config would look like this:

    facebook:
        type:          facebook
        client_id:     %client_id%
        client_secret: %client_secret%
        scope:         "email, basic_info, user_birthday"
        infos_url:     "https://graph.facebook.com/me?fields=id,email,first_name,last_name,gender,birthday"
    

    Then on your AccountConnector service / or how you handle the response you can do something like this:

    $data = $response->getResponse();
    //check if all fields are set 
    ...
    $user->setLastname($data['last_name']);
    $user->setBirthdate(new \DateTime($data['birthday']));
    $user->setGender($this->setGender($data['gender']));
    $user->setEmail($data['email']);
    

    Since Facebook sends nominal values for gender, I have created the setGender function.

    private function setGender($gender) {
        return ($gender === 'male') ? '1' : '0' ;
    }