Error after authentication : Column 'email' cannot be null #5

Closed
tristanbes opened this Issue Oct 18, 2011 · 7 comments

Projects

None yet

3 participants

@tristanbes

Hi,

When i successfully auth on google openid, right after, i got the :

SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'email' cannot be null

It seems that he calls updateUser() if that helps.

My code looks like yours :

    public function loginAction()
    {        
    $tokenPersister = $this->get('fp_openid.security.authentication.token_persister');
    $token = $tokenPersister->get();

    $um = $this->get('fos_user.user_manager');
    $email = $token->getAttribute('contact/email');
    $user = $um->findUserBy( array('email'=> $email ) );

    var_dump($user->getEmail()); // he has my email ! (something@gmail.com)

    if( $user ){


        // CRASHES HAPPENS IN THIS IF STATEMENT
        $newToken = new OpenIdToken($token->getIdentifier(), $user->getRoles());
        $newToken->setUser($user);

        $tokenPersister->set($newToken); 

        return $this->redirect($this->generateUrl('fos_user_security_check', array('openid_approved' => 1)));
    } else {

       // If the user doesn't have an account already, we create an account for him and log him in.

        $username = $email;
        $password = sha1(date('Y-m-d').rand(1,9999));
        $user = $this->get('fos_user.util.user_manipulator')->create($username, $password, $email, 1, 0);

        $this->getRequest()->getSession()->setFlash('success', 'Your account has been successfully created, you\'re now logged in');
        $newToken = new OpenIdToken($token->getIdentifier(), $user->getRoles());
        $newToken->setUser($user);
        $tokenPersister->set($newToken); 

        return $this->redirect($this->generateUrl('fos_user_security_check', array('openid_approved' => 1)));
    }
}

`

thanks for your help.

@makasim
Collaborator
makasim commented Oct 19, 2011

I will try to make example app https://github.com/formapro/fosuser-fpopenid working on weekends.

@tristanbes

ok, great, you might recognize some of your code because i started from your code :-). I don't know why he's throwing this error :/

@makasim
Collaborator
makasim commented Oct 25, 2011

I spent sometime today to investigate the code. Unfortunately the logic of FOSUserBundle cannot work with token created in a separate process (It means if you work with token which was unserailiized it will treat the user as a new one).

The fix can be:

  • tweak InteractiveLoginListener to look for the user in db before any operation (don't know it securely safe)
  • dont do any redirects to login_check after success auth with openid (it means you can save the token to SecurityContext and redirect a user to homepage)

I will think about it, I hope I'll find a solution.

@makasim
Collaborator
makasim commented Nov 3, 2011

@tristanbes, @diegogd - I find a solution to fix the problem, you can try the bundle version from refactor-approve branch.

The doc is also updated.

any question feel free to ask.

@makasim
Collaborator
makasim commented Nov 3, 2011

closing

@makasim makasim closed this Nov 3, 2011
@tristanbes

Ok great i'll check it ASAP. Thanks !

@wbancer
wbancer commented Dec 4, 2011

I have testet the refactor-approve branch, and I think it requires a little tweaking.

I have checked out example you made above, and tried to adapt it to refactor-approve branch.
In order to make it working I had to modify:
Security/Core/Authentication/Provider/OpenIdAuthenticationProvider.php
and add:

public function process(OpenIdToken $token)
{
    return $token;
}

and modify supports() method to return true also when the state is null (not just when state is within array).

Without that, symfony was generating exception about no auth provider for class OpenIdToken, as at some point token was generated without any state. I'm not sure I solved it in best way though. Any ideas?

This was referenced Dec 4, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment