0

I have FOSUser working and now attempting to add Google/Facebook sign-up, meaning that I will still sign up users with regular FOSUser setup.

I know about HWIOAuthBundle but I am reluctant to add another bundle. Already having second thoughts about FOS bundle itself after all the overriding I already did.

When creating the user with the info coming from Google I am of course getting the error that the password field cannot be null.

This is the function creating user in my controller:

private function registerUser($payload) {
    $user = $this->userManager->createUser();
    $user->setEmail($payload['email']);
    $user->setUsername($payload['given_name'] . '-' . mt_rand()); // temporary username
    $this->userManager->updateUser($user);
}

Looking at this SO question, I added the password field to my modified User entity, but Doctrine:migration is not catching it:

/**
* @ORM\Entity
* @ORM\Table(name="`user`")
*/
class User extends BaseUser
{
 .....
/**
 * @ORM\Column(type="string", name="password", nullable=true)
*/
protected $password;

I may be taking the wrong path and in that case I appreciate any guidance.

BernardA
  • 1,391
  • 19
  • 48

2 Answers2

0

I've got a login-with-linkedin/Oauth. I just set a random password. They can't login with it, as I don't even know it, but they can have it reset after a confirmation email.

$user = new User();

$user->addRole($xyz->getRoleType());
$user->setUsername($xyz->getUsername());
$user->setEmail($xyz->getEmail());

// encode a random password, manually
$plainPassword   = base64_encode(random_bytes(16));
$encodedPassword = $this->get('security.password_encoder')
    ->encodePassword($user, $plainPassword);
$user->setPassword($encodedPassword);

$user->setEnabled(true);    // we have to enable the user to let them login again
$em->persist($user);
Alister Bulman
  • 34,482
  • 9
  • 71
  • 110
0

A bit late for this one, but just in case someone is looking for an answer, it can be done through doctrine's AttributeOverrides:

 * @ORM\Entity(repositoryClass=UserRepository::class)
 * @ORM\Table(name="difc_user")
 * @AttributeOverrides({
 *     @AttributeOverride(name="password",
 *         column=@ORM\Column(
 *             nullable=true
 *         )
 *     ),
 *  }
 * )
 * 
 * @UniqueEntity("email")
Touf
  • 106
  • 1
  • 6