Implementing the installation and documentation in Integrating with FOSUserBundle and following comments and leads from A way to integrate FosUserBundle and HWIOAuthBundle
Getting the correct code from Facebook connect API i am able to connect my Facebook account with the project, I can see the app added to my Facebook apps.
But now i want to fetch the user data from FB and store it in my database. I kind of understand what to do but the functions public function connect(UserInterface $user, UserResponseInterface $response)
and public function loadUserByOAuthUserResponse(UserResponseInterface $response)
are not being called after connecting with FB.
I am sure of that because i added non-existing methods in there just like getWhatever and setSomething even with syntax errors nothing happened and no logs are logged which means for 100% this class is never called.
How to fix this issue ?
Following is the files edited as in the examples.
Thanks in advance.
MyFOSUBUserProvider.php
namespace MarketplaceBundle\Security\Core\User;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider as BaseFOSUBProvider;
use Symfony\Component\Security\Core\User\UserInterface;
class MyFOSUBUserProvider extends BaseFOSUBProvider
{
/**
* {@inheritDoc}
*/
public function connect(UserInterface $user, UserResponseInterface $response)
{
// get property from provider configuration by provider name
// , it will return `facebook_id` in that case (see service definition below)
$property = $this->getProperty($response);
$username = $response->getUsername(); // get the unique user identifier
//we "disconnect" previously connected users
$existingUser = $this->userManager->findUserBy(array($property => $email));
if (null !== $existingUser) {
// set current user id and token to null for disconnect
// ...
$this->userManager->updateUser($existingUser);
}
// we connect current user, set current user id and token
// ...
$this->userManager->updateUser($user);
}
/**
* {@inheritdoc}
*/
public function loadUserByOAuthUserResponse(UserResponseInterface $response)
{
$userEmail = $response->getEmail();
$user = $this->userManager->findUserByEmail($userEmail);
// if null just create new user and set it properties
if (null === $user) {
$username = $response->getRealName();
$user = new User();
$user->setFirstName($response->getFirstName());
$em = $this->getDoctrine()->getManager();
$em->persist($user);
$em->flush();
// ... save user to database
return $user;
}else{
$username = $response->getYamen();
$user = new User();
$user->setUsername($username);
}
// else update access token of existing user
$serviceName = $response->getResourceOwner()->getName();
$setter = 'set' . ucfirst($serviceName) . 'AccessToken';
$user->$setter($response->getAccessToken());//update access token
return $user;
}
}
security.yml
security:
encoders:
FOS\UserBundle\Model\UserInterface: bcrypt
providers:
fos_userbundle:
id: fos_user.user_provider.username
firewalls:
...
secured_area:
pattern: ^/
oauth:
resource_owners:
facebook: /login_facebook
login_path: /login
failure_path: /login
oauth_user_provider:
service: my_user_provider
# Turn on anonymous for testings need.
anonymous: ~
access_control:
...
config.yml
...
hwi_oauth:
connect:
account_connector: my_user_provider
firewall_names: [secured_area]
fosub:
username_iterations: 30
properties:
facebook: facebook_id
resource_owners:
facebook:
type: facebook
client_id: 'id'
client_secret: 'secret'
scope: "email,public_profile,user_friends,user_birthday,user_location"
options:
display: popup
services.yml
parameters:
...
my_user_provider.class: MarketplaceBundle\Security\Core\User\MyFOSUBUserProvider
services:
...
hwi_oauth.user.provider.entity:
class: HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider
my_user_provider:
class: "%my_user_provider.class%"
arguments: ['@fos_user.user_manager', { facebook: facebook_id }]
#routing.yml
hwi_oauth_redirect:
resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
prefix: /facebook/connect
facebook_login:
path: /login_facebook
...
facebookJsCallFunction
window.fbAsyncInit = function() {
// init the FB JS SDK
FB.init({
appId : 'id', // App ID from the app dashboard
status : true, // Check Facebook Login status
xfbml : true, // Look for social plugins on the page
version : 'v2.5'
});
};
// Load the SDK asynchronously
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
function fb_login() {
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
// connected
alert('Already connected, redirect to login page to create token.');
console.log(response);
document.location = "{{ url("account") }}";
} else {
// not_authorized
FB.login(function(response) {
if (response.authResponse) {
document.location = "{{ url("account") }}";
} else {
alert('Cancelled.');
}
}, {scope: 'email,public_profile,user_friends,user_birthday,user_location'});
}
});
}