Question

I installed FOSUserBundle and FOSFacebookBundle using this method : How to make a separate url for signin via Facebook using FOSFacebookBundle in Symfony2. Actually I didn't really understand the whole security thing process, but it is working fine now.

When someone is using facebook signup, I would like the possibility to choose an username before being registred (instead of the facebook id as username)... what I do is that I send a POST parameter to the facebook login route but I can't find the controller where the registration is being processed.

What would be the best practise ? Where should I retrieve the username (the POST param) and set it ?

Here is my configuration in security.yml :

firewalls:

public:
  pattern: ^/

  fos_facebook:
    app_url: "http://www.appName.com"
    server_url: "http://local.appName.com/app_dev.php/"
    login_path: /login
    check_path: /login_check/facebook
    provider: appName.facebook.provider

  form_login:
    login_path: /login
    check_path: /login_check/form
    provider: fos_userbundle
    csrf_provider: form.csrf_provider  

and here is the routing I use to signup with facebook :

_security_check_facebook:
    pattern: /login_check/facebook
Was it helpful?

Solution

There is no controller processing authentication, ever authentication method add a listener that will trigger on check_path, and do the trick.

Authentication process shouldn't be responsible to add custom user data. If you followed FOSFacebookBundle documentation you should have a custom user provider that store the user on database, this may confuse a bit but this is not a registration step, is an authentication step. Adding a username is much similar to a profile editing.

To help you i should see your implementation (how do you send post parameter ? ) but you could try something like this:

  1. send post parameter to a custom controller (implemented by you)
  2. temporary store username (maybe user session data ?)
  3. trigger authentication process (this depend on your implementation but essentially we are talking about authenticate on facebook and then post to login_check path)
  4. add username at the authenticated user

This could work, but I never did something like this.

OTHER TIPS

Old question, but I was working on a similar problem. I wanted to add a role to a user when they were registered on the first time they connected with Facebook.

If you follower these steps from the FOSFacebookBundle documentation, then you can tap into the Facebook connected user's "registration", take a look at the FacebookProvider class.

In loadUserByUsername method there is this part:

if (!empty($fbdata)) {
    if (empty($user)) {
        $user = $this->userManager->createUser();
        $user->setEnabled(true);
        $user->setPassword('');
    }

    $user->setFBData($fbdata);
    ...

After the setPassword within the if is where I added the role for my user.

For your original problem of choosing the username... notice that call to setFBData?

In the User entity the method looks like this:

public function setFBData($fbdata)
{
    if (isset($fbdata['id'])) {
        $this->setFacebookId($fbdata['id']);
        $this->addRole('ROLE_FACEBOOK');
    }
    if (isset($fbdata['first_name'])) {
        $this->setFirstname($fbdata['first_name']);
    }
    if (isset($fbdata['last_name'])) {
        $this->setLastname($fbdata['last_name']);
    }
    if (isset($fbdata['email'])) {
        $this->setEmail($fbdata['email']);
    }
}

and setFacebookId() looks like this:

public function setFacebookId($facebookId)
{
    $this->facebookId = $facebookId;
    $this->setUsername($facebookId);
}

So that's where the username comes from. I guess that is a flow where you could plug your chosen username into and set it instead of the $facebookId.

I did another way for this. I used GraphAPI.

public function setFacebookId($facebookId)
{
    $this->facebookId = $facebookId;
    $username = json_decode(file_get_contents('http://graph.facebook.com/'.$this->facebookId))->username;
    $this->setUsername($username);
}

It takes the Facebook username, an unique one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top