Instantly share code, notes, and snippets.

# danvbe/1-Explanations.md Last active Oct 22, 2017

What would you like to do?
A way to integrate FosUserBundle and HWIOAuthBundle

I have managed to install this… and make it work. I implemented it for Facebook and Google, but you can extend it. My solution it is mostly as described in #116, with a bit of more code presented. The key aspects that lack in the #116 presentation (IMO) are:

• the registration as service of your custom FOSUBUserProvider (with the necessary parameters)
• set the service for oauth_user_provider in the security.yml with your custom created service

Here are the steps:

1. Routing. In routing.yml I have added all the routes for both bundles.
2. Configuration. I have set the config.yml mostly as it is presented in the HWIOAuthBundle.
3. Security. I have set the security.yml mostly as it is presented in the HWIOAuthBundle (though my routes are using /login pattern, not /connect). Also, the oauth_user_provider is set for my custom service.
4. User. My own User entity, extended from FosUser.
5. UserProvider. My user provider, registered as service, extended from FOSUBUserProvider. This is the one that actually does the User registration in YOUR database with data from PROVIDERS (Facebook, Google, etc.) and in responsible for connecting already logged in users with accounts from PROVIDERS. It does this by overvriting 2 functions (connect(UserInterface $user, UserResponseInterface$response) and loadUserByOAuthUserResponse(UserResponseInterface $response)). See code below. 6. Custom service. My user provider is registered as service. Using this code, when: 1. No user is authenticated on my site: by accessing http://my_app_web_root/login/facebook or http://my_app_web_root/login/google, a user is created in my database (with data as it is saved in the custom FOSUBUserProvider) and it is automatically login-ed to my site. 2. A user is authenticated on my site: by accessing http://my_app_web_root/login/facebook or http://my_app_web_root/login/google, the current user is updated with data from the provider (account linking). I think this is the behavior everybody was expecting :).  #app/config/config.yml hwi_oauth: #this is my custom user provider, created from FOSUBUserProvider - will manage the #automatic user registration on your site, with data from the provider (facebook. google, etc.) #and also, the connecting part (get the token and the user_id) connect: account_connector: my_user_provider # name of the firewall in which this bundle is active, this setting MUST be set firewall_name: main fosub: username_iterations: 30 properties: # these properties will be used/redefined later in the custom FOSUBUserProvider service. facebook: facebook_id google: google_id resource_owners: facebook: type: facebook client_id: "%facebook_app_id%" client_secret: "%facebook_app_secret%" scope: "" google: type: google client_id: "%google_app_id%" client_secret: "%google_app_secret%" scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile" # here you will add one (or more) configurations for resource owners  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; } }  #app/config/routing.yml #FosUserBundle Routes fos_user_security: resource: "@FOSUserBundle/Resources/config/routing/security.xml" fos_user_profile: resource: "@FOSUserBundle/Resources/config/routing/profile.xml" prefix: /profile fos_user_register: resource: "@FOSUserBundle/Resources/config/routing/registration.xml" prefix: /register fos_user_resetting: resource: "@FOSUserBundle/Resources/config/routing/resetting.xml" prefix: /resetting fos_user_change_password: resource: "@FOSUserBundle/Resources/config/routing/change_password.xml" prefix: /profile #HWIOAuthBundle routes hwi_oauth_security: resource: "@HWIOAuthBundle/Resources/config/routing/login.xml" prefix: /login hwi_oauth_connect: resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml" prefix: /login hwi_oauth_redirect: resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml" prefix: /login facebook_login: pattern: /login/check-facebook google_login: pattern: /login/check-google  security: encoders: FOS\UserBundle\Model\UserInterface: sha512 role_hierarchy: ROLE_ADMIN: ROLE_USER ROLE_SUPER_ADMIN: ROLE_USER providers: fos_userbundle: id: fos_user.user_provider.username_email firewalls: dev: pattern: ^/(_(profiler|wdt)|css|images|js)/ security: false main: pattern: ^/ form_login: provider: fos_userbundle csrf_provider: form.csrf_provider login_path: /login check_path: /login_check oauth: resource_owners: facebook: "/login/check-facebook" google: "/login/check-google" login_path: /login failure_path: /login oauth_user_provider: #this is my custom user provider, created from FOSUBUserProvider - will manage the #automatic user registration on your site, with data from the provider (facebook. google, etc.) service: my_user_provider logout: true anonymous: true login: pattern: ^/login$ security: false remember_me: key: "%secret%" lifetime: 31536000 # 365 days in seconds path: / domain: ~ # Defaults to the current domain from $_SERVER access_control: - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin/, role: ROLE_ADMIN }
 #danvbe/UserBundle/Resources/services.yml parameters: my_user_provider.class: danvbe\UserBundle\Security\Core\User\FOSUBUserProvider services: my_user_provider: class: "%my_user_provider.class%" #this is the place where the properties are passed to the UserProvider - see config.yml arguments: [@fos_user.user_manager,{facebook: facebook_id, google: google_id}]


### daFish commented Jan 10, 2013

 This works great and I successfully integrated it in my project. This would make a pretty good example for the documentation.
Owner

### danvbe commented Jan 16, 2013

 Please, use it as you like. I'll leave the gist here, so you can copy the code or just link here... anyway is best for you.

### ZloyPotroh commented Feb 22, 2013

 Perfect, Bro. I use this gist for Yammer Authentication based on oauth2 type and custom OAuth2ResourseOwner. Thank you.

### emgiezet commented Mar 8, 2013

 I love you! You just save my week of work!

### HadesArchitect commented Mar 10, 2013

 Thank you!

### speelgoedkoper commented Apr 9, 2013

 @danvbe Any idea why I might be getting Unrecognized field: facebook_id 500 Internal Server Error - ORMException after following the steps above ? The columns were created OK in the database after running php app\console doctrine:schema:update --force Perhaps relevant: i'm currently testing on an internal sitename.dev domain

### Monomachus commented Apr 9, 2013

 This is all ok, the only thing missing is how you render it in a twig, the buttons for login ?

### quantizer commented Apr 12, 2013

 @Monomachus it's easy, try somethink like Sign in with Facebook  @danvbe thanks, it works :-) If use $user->setPassword($username); passwords saved as plain text, but if use $user->setPlainPassword($username); it saves with salt :-)

### HadesArchitect commented Apr 21, 2013

 You already save a lot of my time, but I'd like to ask, where (and how) I can get a new user's facebook profile photo? I examined all "Response" objects but they conains only user ID, name, many other things, but no photo. I'm going to use a FB photo as a default user picture in my app. Should I make another request to FB?

### skelt commented Apr 26, 2013

 Im getting [Symfony\Component\DependencyInjection\Exception\InvalidArgumentException] Unable to replace alias "my_user_provider" with "hwi_oauth.user.provider.entity.main". any ideas anyone?

### gristoi commented May 29, 2013

 Fantastic Gist, got my app up and running and logging users in via twitter in no time. I do have one question. Once I have the user logged into my site via the oauth proccess how can i then grab their timeline from the twitter api, or pull in their latest tweets using the access token stored against them in the database?

### luishdez commented Jun 4, 2013

 Great gist! Has anyone implemented the remember me feature with this? BTW I'll check first email if exists and fallback to Id, so in that way users can switch between providers and keep the same account if ($email) {$user = $this->userManager->findUserBy(array('emailCanonical' =>$id); } …

### medy36 commented Jun 5, 2013

 thank you for your clair explanations !!!!! i have an error exception related to the FOSUBUserProvider overriding, ErrorException: Catchable Fatal Error: Argument 1 passed to HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider::__construct() must implement interface FOS\UserBundle\Model\UserManagerInterface, string given, called in C:\wamp\www\TrocMaroc\TrocMaroc\app\cache\dev\appDevDebugProjectContainer.php on line 1806 and defined in C:\wamp\www\TrocMaroc\TrocMaroc\vendor\hwi\oauth-bundle\HWI\Bundle\OAuthBundle\Security\Core\User\FOSUBUserProvider.php line 46 would you help me please :)

### medy36 commented Jun 5, 2013

 Sorry for this stupid question, that was only a missed arguments in my service. Thank you very very much!!!!!

### cordoval commented Jun 12, 2013

 i got it somehow working however i see that the username that stores linkedin into the db is very different from the one i have on linkedin. Is this normal?

### headrevision commented Jun 13, 2013

 I am working on an OAuth sign-in & sign-up support for Sylius. See my attempt of a reference implementation: https://github.com/headrevision/Sylius/tree/oauth-login. Anyone interested to contribute to my fork?

### abenbachir commented Jun 16, 2013

 since everyone use this bundle with FosUserBundle, i think a better name for this bundle might be FosSocialBundle

### AhmedSamy commented Jun 18, 2013

 I have tried your steps many times I think i am only one step away, as i m getting error "SSL certificate problem, verify that the CA cert is OK." and it dont know where to set CURLOPT_SSL_VERIFYPEER to false, I m trying with Google btw

### ziiw commented Jun 24, 2013

 Hi ! I attempt to do what you explain but, when I test, I have this error: Error: Class 'HWI\Bundle\OAuthBundle\HWIOAuthBundle' not found I've put the HWI directory in vendors with the name: HWIOAuthBundle. Any idea what's my problem ?

### mranawake commented Jun 27, 2013

 @AhmedSamy - I had the same issue. Add following options under "hwi_oauth" and you should be fine. hwi_oauth: http_client: verify_peer: false @ziiw - Try installing the HWI library using Composer. I did that and got no issues. Finally I have an issue as well, I get following error (I have masked out certain values for this posting): FatalErrorException: Error: Call to undefined method xxx\xxx\Entity\User::setFacebookId() in C:\xxx\Security\Core\User\FOSUBUserProvider.php line 54 Any ideas?

### paolostefan commented Jun 28, 2013

 @mranawake just run php app/console doctrine:generate:entities xxx/xxx/Entity/User This will add setters and getters for the protected properties in your user class.

### Barno commented Jul 1, 2013

 thanks for this implementation ... I have a small problem, when the user is authenticated, and I close and reopen the browser, this is not remembered Thank you again!

### maZahaca commented Jul 3, 2013

 Everything fine! But i have some problem: I logout from system I call myapp/login/google (for example) // In code I goes to FOSUBUserProvider->loadUserByOAuthUserResponse and create a new user. I redirecting to myapp/ page I call again myapp/login/google and goes to myapp/login/service/google (page to connect from HWI) What problems with first call myapp/login/google? Why it redirect to / root?

### umens commented Jul 5, 2013

 @Barno i have the same issue ! Did you find something about this ? i'll tell you if i find something. Or if somebody resolve this, can he tell us the good way.

### Barno commented Jul 9, 2013

 @umens try with this... firewalls: main: remember_me: key: %secret% lifetime: 31536000 # 365 days in seconds path: / domain: ~ # Defaults to the current domain from $_SERVER always_remember_me: true remember_me_parameter: _remember_me pattern: ^/ form_login: provider: fos_userbundle csrf_provider: form.csrf_provider login_path: /login check_path: /login_check oauth: remember_me: true resource_owners: facebook: "/login/check-facebook" #google: "/login/check-google" twitter: "/login/check-twitter" login_path: /login failure_path: /login oauth_user_provider: #this is my custom user provider, created from FOSUBUserProvider - will manage the #automatic user registration on your site, with data from the provider (facebook. google, etc.) service: my_user_provider logout: path: /logout target: / anonymous: true login: pattern: ^/login$ security: false 

### ironbone commented Aug 2, 2013

 When defining the user in the given way the Entity has a lot of fields. I do not need a lot of them - for example credentials_expire_at Is it possible to create the User entity without these fields - and how?

### sandoche commented Aug 5, 2013

 Hello, I installed HWIOAuthBundle and followed this tutorial properly but when I go to : /login/google or /login/yahoo it automatically redirect to /login I didn't get exactly how this bundle is working. Do you know what is missing to make this work ? Thanks,

### sandoche commented Aug 5, 2013

 I found my mistake but now I get this error for every api I configured :/ "No resource owner with name 'yahoo'."

### khalid05 commented Aug 15, 2013

 hi, just add yahoo like security.yml  oauth: remember_me: true resource_owners: yahoo: "/login/check-yahoo" routing.yml yahoo_login: pattern: /login/check-yahoo  services.yml parameters: my_user_provider.class: danvbe\UserBundle\Security\Core\User\FOSUBUserProvider services: my_user_provider: class: "%my_user_provider.class%" arguments: [@fos_user.user_manager,{facebook: facebook_id, google: google_id, yahoo: yahoo_id}] 

### TIIUNDER commented Aug 16, 2013

 I saw, some of you got it working with Twitter. In my case, Facebook works perfect, but not Twitter. If I try to connect to Twitter, I get to the Twiiter Authorization page with the token in the URL. I click on the authorize button and nothing happens, only the URL token disappears in the URL. But I still stay on the Twitter page. Anybody knows a solution for this?

### TIIUNDER commented Aug 17, 2013

 Solved it myself, I set the app in Twitter to "Read-only", now it works :-)

### roxpopescu commented Aug 18, 2013

 Hi, there!! awesome job!!! I just have one question,,Is there a class I can modify so that the user would be permitted to enter a password, which he will use for my website login beside the email that will be retrieved from facebook before submitting the form that persists the data(email from facebook and password entered by the user) to my database?!I would like to let the user submit this form and not do it automatically as it is done now,, I hope I made myself clear enough,,Thanks!!

### inesbd commented Aug 21, 2013

 hi, i have the same problem as Ahmed SSL certificate problem, verify that the CA cert is OK. Details: error:14090086:SSL routines:SSL3_GET_SERVER_CERTIFICATE:certificate verify failed i follow instruction but it doesn't work, Anybody knows a solution for this?

### danut007ro commented Nov 10, 2013

 Any ideas on how I could implement a refreshUser function, so the user won't be loaded from database on every request?
Owner

### gopalbandi commented Aug 11, 2014

Hi,
I'm trying to use this on my template , but I'm getting this error: No resource owner with name 'facebook', when click on that link.
Below are the configured files:
.
Security.yml

security:
encoders:
Phoenix\CoreBundle\Entity\User\User:
algorithm: sha1
encode_as_base64: false
iterations: 1
providers:
users:
id: phoenix.core.manager.user
fos_userbundle:
firewalls:
backend:
anonymous: ~
always_use_default_target_path: true
logout:
frontend:
pattern: ^/
anonymous: ~
csrf_provider: form.csrf_provider
success_handler: phoenix.frontend.security.authentication.success_handler
failure_handler: phoenix.frontend.security.authentication.failure_handler
always_use_default_target_path: false
logout:
path: /user/logout
target: /
remember_me:
key: "%secret%"
path: /
domain: ~
secured_area:
pattern: ^/
anonymous: true
oauth:
resource_owners:
default_target_path: /

            oauth_user_provider:
#this is my custom user provider, created from FOSUBUserProvider - will manage the
service: my_user_provider
pattern:  ^/login$security: false remember_me: key: "%secret%" lifetime: 31536000 # 365 days in seconds path: / domain: ~ # Defaults to the current domain from$_SERVER

access_control:
#Backendbundle
- { path: ^/admin/(login|login_check)$, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/admin, roles: ROLE_ADMIN } #Frontbundle - { path: ^/login/$, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/connect, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }


Config.yml

# FOSRest Configuration

fos_rest:
view:
force_redirects:
html: true
formats:
json: true
templating_formats:
html: true

# FOSUser Configuration

fos_user:
db_driver: orm # other valid values are 'mongodb', 'couchdb' and 'propel'
firewall_name: secured_area
user_class: Phoenix\CoreBundle\Entity\User\User

# Socail Configuration

hwi_oauth:
#this is my custom user provider, created from FOSUBUserProvider - will manage the
#and also, the connecting part (get the token and the user_id)
connect:
account_connector: my_user_provider
# name of the firewall in which this bundle is active, this setting MUST be set
firewall_name: secured_area
fosub:
properties:
# these properties will be used/redefined later in the custom FOSUBUserProvider service.
resource_owners:
scope: email
paths:
email: email
profilepicture: picture.data.url
options:
display: popup #dialog is optimized for popup window
services:
hwi_oauth.user.provider.entity:
class: HWI\Bundle\OAuthBundle\Security\Core\User\OAuthUserProvider

Routing.yml

# FosUserBundle Routes

fos_user_security:
resource: "/config/routing/security.xml"

fos_user_profile:
resource: "/config/routing/profile.xml"
prefix: /profile

fos_user_register:
resource: "/config/routing/registration.xml"
prefix: /register

fos_user_resetting:
resource: "/config/routing/resetting.xml"
prefix: /resetting

prefix: /profile

# HWIOAuthBundle routes

hwi_oauth_security:

hwi_oauth_redirect:
resource: "/config/routing/redirect.xml"

hwi_oauth_connect:
resource: "/config/routing/connect.xml"

It is not working throwing an error "No resource owner with name 'check-facebook'". Any Idea?

### jgudavalli commented Aug 15, 2014

 Hello Danvbe, I am getting a "No resource owner with name 'check-facebook'. 500 Internal Server Error - RuntimeException". I tried several options found on the web, but no luck. you can find the three files config.yml, security.yml and routing.yml at the link below. https://gist.github.com/jgudavalli/da804f4e4fcd83834985 I have followed the process mentioned above in your blog. Please let me know if you find any mistake in my files. Regards, Jyothi

### amitblues123 commented Oct 31, 2014

 @danvbe Above article is working with github oauth but when I am trying with generic resource owner. After redirection from oauth login, user is not getting registered in application. I am getting error "An authentication exception occurred." Please anyone can help me out.

### aequasi commented Nov 25, 2014

 @quantizer @danvbe thanks, it works :-) If use $user->setPassword($username); passwords saved as plain text, but if use $user->setPlainPassword($username); it saves with salt :-) do NOT do this!!! All of your passwords will be the username that is given from the oauth provider! The way it is set now, the password will never be able to be brute forced/guessed because the encoder will never be able to hash into the plain text username.

### Zamoud commented Jan 27, 2015

 Hi I can connect, and save data in my database. If I disconnect the user can't reconnect because I aldrealy have my user registered in my DB so that give me an SQL error duplicate entry. Thank you the community.

### yanict commented Jan 27, 2015

 Hi all, when the user want to cancel the connection (instead of accepting) on the provider side. He gets redirected to : http://www.mysite.com/app_dev.php/login/service/google?error=access_denied and have the following error Catchable Fatal Error: Argument 1 passed to HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\GenericOAuth2ResourceOwner::getUserInformation() must be of the type array, null given, called in C:\apache64\htdocs\mysite\vendor\hwi\oauth-bundle\HWI\Bundle\OAuthBundle\Controller\ConnectController.php on line 186 and defined 500 Internal Server Error - ContextErrorException  does anyone already face that error ? thanks for your help. @Zamoud, do you have implemented both methods : connect and loadUserByOAuthUserResponse ? paste your FOSUBUserProvider.php file ?

### Zamoud commented Feb 4, 2015

 thanks @yanict i reinstall it and it work

### Zamoud commented Feb 4, 2015

 i have the same pb of "get the facebook username" like @Sashkan and @zazzou so $fbid =$user->getFacebookId(); $pic = "https://graph.facebook.com/" .$fbid."/picture"; ==> work $userData = "https://graph.facebook.com/" .$fbid; ==> pb "code": 100 now with real link https://graph.facebook.com/10205810650693432/picture ==> work https://graph.facebook.com/10205810650693432 ==>pb "code": 100 the pb is "FacebookId" is wrong and the real Facebookid is "1472127719" you can test it https://graph.facebook.com/1472127719 (the same image) the first link work because facebook convert the id of OAuth to get the real facebookId this is the link after loaded https://fbcdn-profile-a.akamaihd.net/hprofile-ak-xpa1/v/t1.0-1/p50x50/10888838_10205522960661361_6666071637152803952_n.jpg?oh=3fc95b6409d1f0ecf7daffc2412db2d4&oe=556751F7&__gda__=1432231754_75b5a63dac6c09d918f0e9ddcb60ec9c so we should correct setFacebookId() Thank you sorry for my english

### thomas-bousquet commented Feb 11, 2015

 @danvbe Is there a way to validate the user within this UserProvider ? I had an issue with one Facebook account, i had the email in the response equals to null (old account and the email does not exist anymore, i guess that's why I had an null value...). Anyway, I'd like to make sure that I have all the proper data in order to avoid a 500 error (SQL one in my case : email can not be null). Thanks for your ideas guys !

### kimwue commented Feb 25, 2015

 Could anyone let me know where/in which case the FOSUBUserProvider:connect method is actually used? I can't find any situation its called... Apart from that thanks - works like a charm :)

### KhanMaytok commented Mar 8, 2015

 Hi. I have realized every step and now I can login with faceboook. I go first to login/facebook register the user, then go to /profile And I get numbers Identificado como 801567456598134 | Salir Nombre de usuario: 801567456598134 Email: 801567456598134 Why I can not get the email of the user???

### The-Don-Himself commented Mar 10, 2015

 @ SakyaStelios Please read author's comments carefully, your issue, I suspect, is in the file FOSUBUserProvider.php  //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);  Add $email =$response->getEmail(); before or after $username =$response->getUsername(); in public function loadUserByOAuthUserResponse Thereafter modify $user->setEmail($username); to $user->setEmail($email); To be safe, clear cache

### martijngastkemper commented Mar 12, 2015

 Very nice Gist! The problem I found was the length limit on the access token. I use a custom service with a access token length of more then 255 characters. I changed the column type to text, because the oAuth specs doesn't say anything about the length.

### luismabenitez commented Mar 13, 2015

 My Symfony app can't find the service and I got this error: ServiceNotFoundException in CheckExceptionOnInvalidReferenceBehaviorPass.php line 58: The service "hwi_oauth.security.oauth_utils" has a dependency on a non-existent service "hwi_oauth.resource_ownermap.name".  My services.yml is the next one: parameters: my_user_provider.class: Main\SiteBundle\Services\FOSUBUserProvider services: my_user_provider: class: "%my_user_provider.class%" arguments: [@fos_user.user_manager,{facebook: facebook_id, google: googl e_id}] I'm getting crazy with this, it seems to have a wrong path, but all is right...

### chiqui3d commented Mar 19, 2015

 Hey @danvbe, Already fixed. How I can associate the login with facebook to REGISTRATION_SUCCESS or REGISTRATION_COMPLETED of fosuserbundle event? I've tried in this way, but with errors.  $this->userManager->updateUser($user,true); $dispatcher =$this->container->get('event_dispatcher'); $event = new FOSUserEvents($user); $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS,$event); Can I have help?

### ghost commented Apr 10, 2015

 Hi, I got this error: FatalErrorException in FOSUBUserProvider.php line 78: Compile Error: Cannot redeclare class Webworks\UserBundle\Security\Core\User\FOSUBUserProvider Can't find my mistake...

### durthar commented Apr 18, 2015

 @danvbe Thanks! This was very helpful in integrating FB Login. I'm still a bit skeptical about the int username/email/password. Does anyone have a "best practices" resource for this?

### gmonsalve commented Apr 30, 2015

@danvbe Can you helpme,

I'm trying configured this Bundle but don't understand whappend, paste my implementation, don't error display only "Authentication request could not be processed due to a system problem",

So, how try update my schema tell me that : [Doctrine\Common\Persistence\Mapping\MappingException]

app/config.yml
imports:
- { resource: parameters.yml }
- { resource: security.yml }
- { resource: services.yml }

framework:
#esi: ~
#translator: { fallbacks: ["%locale%"] }
secret: "%secret%"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig','php']
#assets_version: SomeVersionScheme
default_locale: "%locale%"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true

# Twig Configuration

twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"

# Assetic Configuration

assetic:
debug: "%kernel.debug%"
use_controller: false
bundles: [ ]
#java: /usr/bin/java
filters:
cssrewrite: ~
#closure:
# jar: "%kernel.root_dir%/Resources/java/compiler.jar"
#yui_css:
# jar: "%kernel.root_dir%/Resources/java/yuicompressor-2.4.7.jar"

# Doctrine Configuration

doctrine:
dbal:
driver: "%database_driver%"
host: "%database_host%"
port: "%database_port%"
dbname: "%database_name%"
user: "%database_user%"
charset: UTF8
# if using pdo_sqlite as your database driver:
# 1. add the path in parameters.yml
# e.g. database_path: "%kernel.root_dir%/data/data.db3"
# 2. Uncomment database_path in parameters.yml.dist
# 3. Uncomment next line:
# path: "%database_path%"

orm:
auto_generate_proxy_classes: "%kernel.debug%"
auto_mapping: true


# Swiftmailer Configuration

swiftmailer:
transport: "%mailer_transport%"
host: "%mailer_host%"
spool: { type: memory }

fos_user:
db_driver: orm
firewall_name: main
user_class: RegistroBundle\Entity\User

hwi_oauth:
#this is my custom user provider, created from FOSUBUserProvider - will manage the
#and also, the connecting part (get the token and the user_id)
connect:
account_connector: my_user_provider

# name of the firewall in which this bundle is active, this setting MUST be set

firewall_name: main
fosub:
properties:
# these properties will be used/redefined later in the custom FOSUBUserProvider service.
resource_owners:
#     scope:               ""
client_secret:       xxxxxxxxxxxxxxn

###### ################################3

app/security.yml

security:
encoders:
FOS\UserBundle\Model\UserInterface: sha512

role_hierarchy:

providers:
fos_userbundle:

firewalls:
dev:
pattern:  ^/(_(profiler|wdt)|css|images|js)/
security: false

main:
pattern: ^/
provider: fos_userbundle
csrf_provider: form.csrf_provider
oauth:
resource_owners:

oauth_user_provider:
#this is my custom user provider, created from FOSUBUserProvider - will manage the
service: my_user_provider
logout:       true
anonymous:    true

pattern:  ^/login$security: false remember_me: key: "%secret%" lifetime: 31536000 # 365 days in seconds path: / domain: ~ # Defaults to the current domain from$_SERVER

access_control:

### joseadame commented May 7, 2015

 Hi, I login with Google, if the user doesn't exist I save it in the database and login, but if I login again I have an error: Code was alreade redeemed

### feyyazesat commented May 28, 2015

 If anyone encounters with the No resource owner with name 'check-facebook' or 'check-*' problem try to define facebook_login: pattern: /login/check-facebook route before the hwioauth route lines. Otherwise /login/{service} which is defined in /config/routing/redirect.xml overrides these routes.

### zetta commented Jun 15, 2015

 This gist helped me a lot. Thank you!!!!!

### collmomo commented Jun 30, 2015

 This implementation might have a problem, for example: User login using facebook User changes email User logout User login again, UserProvider try to create a new user entity because it looks up using email but the facebook id is still attached to original account and creation of new user fails as well as updating of old user. Any idea on how to solve this ?

### sebastianlp commented Jul 10, 2015

 Hi @youpixxl, do you have a working project example of that implementation?

### Gargantuas commented Aug 4, 2015

 Hello, Very nice gist. But how you manage when a user not accept all permissions with Facebook Connect. I can see in the documentation an option : "auth_type: rerequest" but when i use it i take an error : "The option "auth_type" does not exist." Any idea ? Sorry for my english.

### bretanac93 commented Aug 13, 2015

 dude, this gists is simply amazing, +10 to you, just what I'm looking for.

### xserrat commented Aug 20, 2015

 Thank you @danvbe, excellent gists!

### madEng84 commented Oct 2, 2015

 I have encountered this problem: No resource owner with name 'check-facebook'. As @feyyazesat said, i moved it before hwi_oauth routes but then i have this problem: Unable to find the controller for path "/login/check-facebook". The route is wrongly configured. @danvbe can you help me?

### ewertoncode commented Oct 7, 2015

 Hi! What I can to do in the method loadUserByOAuthUserResponse when the user no return email? I had one case that this it happened... Thank's!

### DaniSancas commented Oct 13, 2015

 As far as I tested, same problem than @madEng84 Also, it would be great if some people publish their own custom GISTs, just to see the way they're implementing their solution (I'd be very pleased) EDIT: I found a solution to the same case @madEng84 has: hwi/HWIOAuthBundle#781 (comment)

### yahuarkuntur commented Oct 13, 2015

 I have created a Gist with my working setup: https://gist.github.com/yahuarkuntur/5c7b4b5f9d05106219b8 Hope this helps anyone 😄

### deyvid5 commented Oct 17, 2015

 I'm getting  Uncaught PHP Exception PropelException: "Unknown column Facebook_id in model FOS\UserBundle\Propel\User" at C:\xampp\htdocs\portalsportowy\vendor\propel\propel1\runtime\lib\query\ModelCriteria.php line 2145  Who can explain me why. I do have: fos_user: db_driver: propel firewall_name: main user_class: Back\UserBundle\Model\User  and overriden the fosuerbundle schema.xml with extra facebookId var.

### malud commented Oct 22, 2015

 @deyvid5 I think you have to update your ORM information und generate your migration out of the resulting model <> db diff and finally migrate your changes. See http://symfony.com/doc/current/bundles/DoctrineMigrationsBundle/index.html for more details.

### tomilsinszki commented Nov 3, 2015

 @danvbe thanks, this was really helpful

### jorgadan commented Nov 3, 2015

 I need realize the login with google, I did all configuration of this gist, but when i do click in "google log in" button I'm redirected from "login/google" to "/login" . < a href="{{ path('hwi_oauth_service_redirect', {'service':'google'}) }}" class="gplus"> Login con Google+  I don't sure about parameters: #parameters.yml parameters: google_app_id: gsdg google_app_secret: gsdg  the missing code is equal to the tutorial. #routing.yml fos_user: resource: "@FOSUserBundle/Resources/config/routing/all.xml" hwi_oauth_security: resource: "@HWIOAuthBundle/Resources/config/routing/login.xml" prefix: /login hwi_oauth_connect: resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml" prefix: /login hwi_oauth_redirect: resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml" prefix: /login google_login: pattern: /login/check-google  some of my config.yml #config.yml hwi_oauth: connect: account_connector: my_user_provider firewall_name: main fosub: username_iterations: 30 properties: google: google_id resource_owners: google: type: google client_id: "%google_app_id%" client_secret: "%google_app_secret%" scope: "https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/userinfo.profile"  Somebody know how it work the routing for access with google?? Am I forgetting something in another file that here is not specified.

### clytemnestra commented Dec 11, 2015

 Works perfectly, just that it creates different accounts for google and facebook for the same email. Username, email and password are also completed with an id, but I guess that's the part where we must interfere with our own code. 👍

### bnd170 commented Dec 20, 2015

 It doens't work for me :( I don't know what im doing wrongly. Im using Symfony 2.7, this doc works on 2.7?

### de-itsnotme commented Sep 8, 2016

 Works fine on localhost but when I upload it to live server, I get the following error: Fatal error: Class 'HWI\Bundle\OAuthBundle\HWIOAuthBundle' not found in /home/xyz/symfony3/app/AppKernel.php on line 21 AppKernel.php has such entry: \$bundles = [ ..., ..., new HWI\Bundle\OAuthBundle\HWIOAuthBundle(), ];  What could be the problem? I'm using Symfony 3.1.1 on Windows for localhost. PS: I have no access to the terminal as it is a shared hosting thus clear:cache is not possible. Alternatives?

### yamennassif commented Sep 13, 2016

 can you check this please ? http://stackoverflow.com/questions/39467905/integrate-fosuserbundle-and-hwioauthbundle

### yamennassif commented Sep 13, 2016

 @de-itsnotme did you composer require hwi/oauth-bundle on server ?

### grekpg commented Nov 10, 2016

 How i can refresh token ? here is my example http://stackoverflow.com/questions/40523329/refresh-oauth2-token-google-api-and-hwioauthbundle

### hariharasudhan94 commented Nov 18, 2016 • edited

 hi i am new to hwiauthbundle i cant figure out why we use connect()

### jeromeheitor commented Dec 3, 2016

 Very Nice and Clear Tutorial. Thank You, you saved time to a lot of people !

### de-itsnotme commented Jan 4, 2017

 Thanks @yamennassif, I don't know the reason but cache clear did solve the issue back then.

### varnitsaini commented Feb 28, 2017 • edited

 @northern, i know its bit late but did you get the answer to redirect based on context of login after successful login? i am facing the same issue and i dont want to use default_target_path. thanks in advance!

### phpdeveloper1 commented Mar 8, 2017 • edited

 I have implemented hwio auth bundle for facebook.But after facebook login I am redirected to failure_path instead of login_path. My security.yml is as follows : main: pattern: ^/ form_login: provider: fos_userbundle csrf_provider: form.csrf_provider check_path: /login_check login_path: /login #use_forward: false #use_referer: false default_target_path: /artist oauth: resource_owners: facebook: "/login/check-facebook" login_path: /login failure_path: /login default_target_path: /artist oauth_user_provider: service: hwi_oauth.user.provider.fosub_bridge logout: target: / anonymous: true switch_user: { role: ROLE_ADMIN } context: primary_auth  My config.yml is as follows: hwi_oauth: connect: account_connector: hwi_oauth.user.provider.fosub_bridge firewall_names: [main] fosub: username_iterations: 30 properties: facebook: facebookId resource_owners: facebook: type: facebook client_id: "%fb_app_id%" client_secret: "%fb_app_secret%" 

### ernestocuzcueta commented Apr 5, 2017

 Hi Thanks for this good bundle. I'm using it from a year ago. But now I need modify the way to work, I only need connect to facebook by example only in the login action, not in the others requests. Is possible only connect to the social network in the login, validate and later not connect more? Thanks for your time Regards Ernesto

### ducho commented Apr 26, 2017

 Hi. Today I have same problem in Symfony3 as @speelgoedkoper. :( Unrecognized field: facebook_id 500 Internal Server Error - ORMException Configuration is same as @danvbe wrote.... Please, any suggestions ?

### ansien12 commented May 11, 2017 • edited

 Getting the error: No oauth code in the request. hwi/HWIOAuthBundle#1217 Any ideas on how to fix this?

### iroegbu commented May 17, 2017

 I had problems using: ... facebook_login: pattern: /login/check-facebook ...  Fixed by changing pattern to path version: Symfony 3.2.8

### AdrienRo commented Jul 29, 2017

 Hi I have this exception using custom provider (no problem width the hwi_oauth.user.provider.fosub_bridge) : No property defined for entity for resource owner 'google'. version: Symfony 3.3

### MatrixOfDeath commented Oct 19, 2017

 Hey thanks for the documentation on this, I'm using Symfony3.3 and i'm meeting an issue, before implementing this SSO with google and facebook. I was already using FosUserbundle and it worked perfectly, to acces the login, my route is /login. Now even if i still see my route, and if i renamed HwiOauth routes too /connect /connect/check etc... I can't seem to access my old working route to login normally (in that template I wanted to add 2 button with google and facebook).