Facebook登录 - HWIOAuthBundle - Symfony3

时间:2017-05-26 13:23:58

标签: php facebook symfony facebook-javascript-sdk

我已将此配置用于FOSU​​BUserProvider.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;
    }
}

我遇到的问题如下:

  1. 如果我使用Facebook登录,并且我已经使用相同的电子邮件注册了帐户,我想从facebook获取响应 - >电子邮件,只需更新facebook_id列。
  2. 如果存在具有相同电子邮件的帐户,我不想插入另一行。
  3. 有人知道我应该更新我的代码吗? 谢谢。

1 个答案:

答案 0 :(得分:0)

  1. 保留您的身份验证代码,无需真正更改任何内容。

  2. 我认为你有某种能力让用户能够管理'他们的个人资料,所以只需添加一个按钮,我们就会将其标记为“连接Facebook'为了论证。

  3. 确保您已经设置了facebook身份验证代码,并通过回复处理退货;

  4. 
    
    $(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'});
        });
    });
    
    
    

    通过存储facebook用户名和令牌等,用户应该能够使用电子邮件/密码和/或Facebook进行身份验证