Hello,
i am currently playing around with this bundle and was wondering if it is possible to trigger the symfony-login after a successful facebook-login (via the facebook login button) per AJAX.
If i understand this example right, in the most cases one would do the following to log the user into the symfony application:
- Make sure that the facebook javascript SDK is loaded
- When the user clicks on the facebook login button (and the user has not authorized the app) call FB.login()
- After a successful login on the facebook-side, trigger the symfony login by issuing a
GET
to the URL defined for the hwi_oauth_service_redirect
-route
- Then the bundles
ConnectController::redirectToServiceAction()
is executed on the serverside which results in a redirect to facebooks oauth endpoint to get the oauth code (which in turn is required to log the user successfully into the symfony application).
The problem i am facing now is that i am working on a single page application where the page must not be reloaded. So my first naive approach was to - in the FB.login-callback - issue the GET
to hwi_oauth_service_redirect
(resp. login/facebook
) per AJAX like the following:
$.ajax({
url: 'login/facebook',
type: 'GET',
dataType: 'json',
crossDomain: true,
})
.done(function(data, textStatus, jqXHR){
console.info(data)
})
.fail(function(jqXHR, textStatus, errorThrown){
console.warn('Could not login facebook user')
});
When i try to do this, unfortunately i run into cross-domain-issues and Firebug shows the following message:
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://www.facebook.com/dialog/oauth?response_type=code&client_id=MY_APP_CLIENT_ID&scope=emall&redirect_uri=MY_REDIRECT_UR/login/check-facebook&display=popup. This can be fixed by moving the resource to the same domain or enabling CORS.
In contrast when i call the related URI (MY_DOMAIN/login/facebook
) by reloading the page as shown in the docs everything works fine and the user is logged in on both sides (facebook and my symfony app).
As i am not sure if am doing everything right or if i am missing something here, my question is: is it possible to trigger the symfony-login as described above (per AJAX) and am i just missing something or is it just not possible to do this?
My configuration of the bundle looks something like the following:
# app/config/config.yml
hwi_oauth:
connect:
account_connector: my_custom_oauth_userprovider_service
firewall_name: main
fosub:
username_iterations: 5
properties:
facebook: facebookId
resource_owners:
facebook:
type: facebook
client_id: MY_APP_CLIENT_ID
client_secret: MY_APP_CLIENT_SECRET
scope: "email"
options:
display: popup
infos_url: "https://graph.facebook.com/me?fields=username,name,email,picture.type(square)"
paths:
email: email
profilepicture: picture.data.url
# app/config/security.yml
security:
providers:
fos_userbundle:
id: fos_user.user_provider.username_email
firewalls:
main:
pattern: ^/
form_login:
success_handler: my_custom.authentication_success_handler
provider: fos_userbundle
csrf_provider: form.csrf_provider
logout:
path: /logout
success_handler: my_custom.security.handler.logout
anonymous: true
# HWIOAuth-related configuration:
oauth:
resource_owners:
facebook: "/login/check-facebook"
login_path: /login
use_forward: false
failure_path: /login
success_handler: my_custom.authentication_success_handler
oauth_user_provider:
service: my_custom.oauth_user_provider
# some configuration for other firewalls...
access_control:
- { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
# more access_control stuff...
# app/config/routing.yml
hwi_oauth_redirect:
resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
prefix: /login
facebook_login:
pattern: /login/facebook
# imported FOS-UserBundle routes and custom routes...
Support request