Search code examples
symfonyoauthhwioauthbundle

HWIOAuthBundle eagerly creates users


In my app I need to be able to let users log in and connect their accounts via OAuth. I'm using HWIOAuthBundle and FOSUserBundle to accomplish that. But there's a problem: when I'm registering via Facebook or whatnot a User entity is automatically created and persisted to the database, and a token is created for the user.

What I actually need is to only fetch some data the provider gives me and map it to a user yet to be persisted.


Solution

  • If I understand, you would like to add for example Facebook information to a logged-in user.

    So what you have to do in your provider is to check that there is a connected user and then add information:

    if (($this->container->get('security.context')->getToken() instanceof AbstractToken) {
        $loggedUser = $this->container->get('security.context')->getToken()->getUser();
        $loggedUser->setFacebookId($fbdata['id']);
    
        /* userManager is fos_user.user_manager service */
        $this->userManager->updateUser($loggedUser);
    
        return $loggedUser;
    }