0

I have tried all day to figure this out. So here goes.

I am using Symfony version 2.6.4

I originally had a login and signup form using friendsofsymfony/user-bundle": "2.0.*@dev

I have just added "hwi/oauth-bundle": "0.4.*@dev" to allow for facebook signup/login.

The problem is that if I signup using my regular form and then attempt to login with facebook i get the following error. (I have changed some values for display purposes):

An exception occurred while executing 'INSERT INTO users (username, username_canonical, email, email_canonical, enabled, salt, password, last_login, locked, expired, expires_at, confirmation_token, password_requested_at, roles, credentials_expired, credentials_expire_at, facebook_id, facebook_access_token) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' with params ["10155387178995713", "10155387178995713", "someemail@hotmail.com", "someemail@hotmail.com", 1, "fgf90m9yxzc4ggkgwg8cgk0g4sk0ocg", "10155345628995713", null, 0, 0, null, null, null, "a:0:{}", 0, null, "10155387178995713", "CAALREhhNZBmEBANQjAMjlrAHZduiwOidsUyVwCI0hQkSj7W9imZC2xlL76jGH3MA10yUBkF2kSCpr9OdBbUSPoKkriZA2FdVlAhFsfXBNjBYiywzodiWYKGrcVMVsMVIVbk6XDUFVDtighTDYKuT9fZBm96VUSZBNSOE92RJMNwtAZCwBKrerAKGB11gNLNqDzNOfnBgih6TQDhacd9xSOC"]:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'someemail@hotmail.com' for key 'UNIQ_1483A5E9A0D96FBF'

If I signup using the regular form and then sign it also with the regular form I get the expected "Email is already in use"

if I sign up with facebook first and then login with facebook it works without issue.

The username/username_cononical when using the standard login form are set differently than when using the facebook signup. Currently the username under the normal login form generates a random string.

I dont care about the username either way. If it updates to the one given by facebook that would be great. I use email login only but keep the username entry in the database as it is standard with FOSUserBundle

I have followed the instructions here: https://gist.github.com/danvbe/4476697

I have also read this: http://m2mdas.github.io/blog/2013/11/21/integrate-hwioauthbundle-with-fosuserbundle/ but still no luck.

Any help would be really appreciated. Glad to post config info or code if requested.

thanks

Chris
  • 2,166
  • 1
  • 24
  • 37

3 Answers3

1

Solved

I had to overide the HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider

as described here: https://gist.github.com/danvbe/4476697

However I had to customize the loadUserByOAuthUserResponse method to fit my needs.

the parent method expected any existing user in the DB to also have been previously added with the same info from the OAuth response

In my particular case I had to do the following:

/**
 * {@inheritdoc}
 */
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
    $username = $response->getUsername();
    $email = $response->getEmail();

    /** @var \AppBundle\Entity\User $user */
    $user = $this->userManager->findUserByEmail($email);

    $serviceName = $response->getResourceOwner()->getName();
    $setter = 'set'.ucfirst($serviceName);
    $setter_id = $setter.'Id';
    $setter_token = $setter.'AccessToken';
    $getter = 'get'.$serviceName;
    $getter_id = $getter.'Id';
    $getter_token = $getter.'AccessToken';

    //when the user is registering
    if (null === $user) {
        // create new user here
        $user = $this->userManager->createUser();
        $user->$setter_id($username);
        $user->$setter_token($response->getAccessToken());
        $user->setEmail($email);
        $user->setPassword($this->generatePassword(12));
        $user->setEnabled(true);
        $this->userManager->updateUser($user);
        return $user;
    }
    //if user exists

    if($user->$getter_id() == ""){
        $user->$setter_id($username);
    }

    //update access token
    $user->$setter_token($response->getAccessToken());

    return $user;
}
Chris
  • 2,166
  • 1
  • 24
  • 37
0

There was a bug of validation at a previous symfony 2.5 you may want to try using the 2.4 for the validation in the config.yml, if it fixes the problem.

app/config/config.yml

framework:
  validation:
     enabled: true
     api: 2.4
     enable_annotations: true # Provided already by when first installing Symfony

https://github.com/FriendsOfSymfony/FOSUserBundle/issues/1516

Nawfal Serrar
  • 2,213
  • 1
  • 14
  • 22
  • Thanks @Nawfal Serrar for your input but I had already changed those settings and the validation was working if a user registered with the my regular login form and then attempted a facebook login. my issue was when the user first registered with my web app's login form and than later tried to login with facebook. I have solved my issue though. thanks – Chris Mar 17 '15 at 21:58
0

For those who may face this problem after 5 years, you may need to set username:

$user->setUsername($email);

Worked for me.

Milad Safaei
  • 492
  • 1
  • 5
  • 16