我已将此配置用于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;
}
}
我遇到的问题如下:
有人知道我应该更新我的代码吗? 谢谢。
答案 0 :(得分:0)
保留您的身份验证代码,无需真正更改任何内容。
我认为你有某种能力让用户能够管理'他们的个人资料,所以只需添加一个按钮,我们就会将其标记为“连接Facebook'为了论证。
确保您已经设置了facebook身份验证代码,并通过回复处理退货;
$(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进行身份验证