简体   繁体   中英

Facebook Login - HWIOAuthBundle - Symfony3

I have used this configs for FOSUBUserProvider.php:

namespace myapp\UserBundle\Security\Core\User;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseClass;
use Symfony\Component\Security\Core\User\UserInterface;
class FOSUBUserProvider extends BaseClass
{
    /**
     * {@inheritDoc}
     */
    public function connect(UserInterface $user, UserResponseInterface $response)
    {
        $property = $this->getProperty($response);
        $username = $response->getUsername();
        //on connect - get the access token and the user ID
        $service = $response->getResourceOwner()->getName();
        $setter = 'set'.ucfirst($service);
        $setter_id = $setter.'Id';
        $setter_token = $setter.'AccessToken';
        //we "disconnect" previously connected users
        if (null !== $previousUser = $this->userManager->findUserBy(array($property => $username))) {
            $previousUser->$setter_id(null);
            $previousUser->$setter_token(null);
            $this->userManager->updateUser($previousUser);
        }
        //we connect current user
        $user->$setter_id($username);
        $user->$setter_token($response->getAccessToken());
        $this->userManager->updateUser($user);
    }
    /**
     * {@inheritdoc}
     */
    public function loadUserByOAuthUserResponse(UserResponseInterface $response)
    {
        $username = $response->getUsername();
        $user = $this->userManager->findUserBy(array($this->getProperty($response) => $username));
        //when the user is registrating
        if (null === $user) {
            $service = $response->getResourceOwner()->getName();
            $setter = 'set'.ucfirst($service);
            $setter_id = $setter.'Id';
            $setter_token = $setter.'AccessToken';
            // create new user here
            $user = $this->userManager->createUser();
            $user->$setter_id($username);
            $user->$setter_token($response->getAccessToken());
            //I have set all requested data with the user's username
            //modify here with relevant data
            $user->setUsername($username);
            $user->setEmail($username);
            $user->setPassword($username);
            $user->setEnabled(true);
            $this->userManager->updateUser($user);
            return $user;
        }
        //if user exists - go with the HWIOAuth way
        $user = parent::loadUserByOAuthUserResponse($response);
        $serviceName = $response->getResourceOwner()->getName();
        $setter = 'set' . ucfirst($serviceName) . 'AccessToken';
        //update access token
        $user->$setter($response->getAccessToken());
        return $user;
    }
}

The problem that i've got is the following:

  1. If i login with facebook, and i already have an account registered with the same e-mail, i want to take the response->email from facebook, and just update the facebook_id column.
  2. I don't want to insert another row if an account with the same email exists.

Does someone knows what should i update to my code? Thank you.

  1. Leave your authentication code as it is, no need to really change anything there.

  2. I assume you've got some sort of ability for a user to 'manage' their profile, so simply add a button we will label it 'connect facebook' for arguments sake.

  3. Make sure you've got your facebook auth code setup and handle the return with a post back;

 $(document).ready(function() { $('#facebook-button').click(function() { FB.login(function(response) { if (response.authResponse) { // Your auth response frmo faceook will contain an email, username, profile photo, etc // console.log(response) to see exactly what's being returned $.ajax({ type: "POST", url: {{ path('user_profile_update_post_path') }}, data: [username: response.username, facebookId: response.facebookToken, /* etc */], success: successCallback, dataType: 'json' }); } }, {scope: 'email'}); }); }); 

By storing the facebook username and token etc, users should then be able to auth with both email/password and/or facebook

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM