Skip to content

Latest commit

 

History

History
129 lines (103 loc) · 3.08 KB

File metadata and controls

129 lines (103 loc) · 3.08 KB

Events

<?php

namespace Lzakrzewski\FacebookAuthenticationBundle;

final class Events
{
    /**
     * Dispatched after new user is created with data retrieved from /me endpoint
     * Hook into this event to modify new user with facebook graph api data.
     */
    const USER_CREATED = 'lzakrzewski_facebook_authentication.on_user_created';

    /**
     * Dispatched after existing user loaded from repository and updated with data retrieved from /me endpoint
     * Hook into this event to modify existing user with facebook graph api data.
     */
    const USER_UPDATED = 'lzakrzewski_facebook_authentication.on_user_updated';
}

Example: How to get dateOfBirth from Facebook graph API

Step 1: Add setDateOfBirth for your User class

<?php
// src/AppBundle/Entity/User.php

namespace AppBundle\Entity;

use FOS\UserBundle\Model\User as BaseUser;
use Doctrine\ORM\Mapping as ORM;
use Lzakrzewski\FacebookAuthenticationBundle\Model\FacebookUser;

/**
 * @ORM\Entity
 * @ORM\Table(name="users")
 */
class User extends BaseUser implements FacebookUser
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    /**
     * @ORM\Column(type="bigint", nullable=true)
     */
    private $facebookId;

    /**
     * @ORM\Column(type="datetime", nullable=true)
     */
    private $dateOfBirth;

    /** {@inheritdoc} */
    public function getFacebookId()
    {
        return $this->facebookId;
    }

    /** {@inheritdoc} */
    public function setFacebookId($facebookId)
    {
        $this->facebookId = $facebookId;
    }

    /**
     * @param \DateTime $dateOfBirth
     */
    public function setDateOfBirth(\DateTime $dateOfBirth)
    {
        $this->dateOfBirth = $dateOfBirth;
    }
}

Step 2: Add user_birthday to lzakrzewski_facebook_authentication.scope

lzakrzewski_facebook_authentication:
    app_id: 1234
    app_secret: secret
    scope: ["public_profile", "email", "user_birthday"]
    fields: ["name", "email", "birthday"]

Step 3: Create event listener event lzakrzewski_facebook_authentication.on_user_created

<?php

namespace AppBundle\EventListener;

use Lzakrzewski\FacebookAuthenticationBundle\Event\FacebookUserEvent;

class FacebookUserListener
{
    /**
     * @param FacebookUserEvent $event
     */
    public function updateDateOfBirth(FacebookUserEvent $event)
    {
        $data = $event->getData();
        $user = $event->getUser();

        $user->setDateOfBirth(new \DateTime($data['birthday']));
    }
}

Step 4: Register event listener as service

services:
    app.event_listener.facebook_user_listener:
        class: AppBundle\EventListener\FacebookUserListener
        tags:
            - { name: kernel.event_listener, event: lzakrzewski_facebook_authentication.on_user_updated, method: updateDateOfBirth }
            - { name: kernel.event_listener, event: lzakrzewski_facebook_authentication.on_user_created, method: updateDateOfBirth }

That it's all. Now after creating/updating user with facebook DateOfBirth will be set.