<?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';
}
<?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;
}
}
lzakrzewski_facebook_authentication:
app_id: 1234
app_secret: secret
scope: ["public_profile", "email", "user_birthday"]
fields: ["name", "email", "birthday"]
<?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']));
}
}
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.