我在使用 FOSFacebookBundle 和 FOSUserBundle 在我的网站上通过 Facebook 订阅时遇到了一些麻烦...
有我的代码:
base.html.twig:
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<!-- TEMPLATE STUFF -->
function goLogIn(){
alert('enter goLogin function');
window.location = "{{ path('_security_check') }}";
}
function onFbInit() {
alert('enter onFbInit');
if (typeof(FB) != 'undefined' && FB != null ) {
FB.Event.subscribe('auth.statusChange', function(response) {
alert('Status Changed');
if (response.session || response.authResponse) {
setTimeout(goLogIn, 500);
} else {
window.location = "{{ path('_security_logout') }}";
}
});
}
}
{{ facebook_initialize({'xfbml': true, 'fbAsyncInit': 'onFbInit();'}) }}
配置.yml
fos_user:
db_driver: orm
firewall_name: main
user_class: Modnar\Bundle\UserBundle\Entity\User
fos_facebook:
file: %kernel.root_dir%/../vendor/facebook/src/base_facebook.php
alias: facebook
app_id: APP_ID // I put my own APP_ID here
secret: APP_SECRET // I put my own APP_SECRET here
cookie: true
permissions: [email, user_birthday, user_location]
services:
my.facebook.user:
class: Modnar\Bundle\UserBundle\Security\User\Provider\FacebookProvider
arguments:
facebook: "@fos_facebook.api"
userManager: "@fos_user.user_manager"
validator: "@validator"
container: "@service_container"
安全.yml
# app/config/security.yml
security:
factories:
- "%kernel.root_dir%/../vendor/bundles/FOS/FacebookBundle/Resources/config/security_factories.xml"
providers:
fos_userbundle:
id: fos_user.user_manager
my_fos_facebook_provider:
id: my.facebook.user
encoders:
FOS\UserBundle\Model\UserInterface: sha512
firewalls:
main:
pattern: ^/
form_login:
provider: fos_userbundle
csrf_provider: form.csrf_provider
fos_facebook:
app_url: "http://apps.facebook.com/APP_NAME/" // I put my own APP_NAME here
server_url: "http://APP_URL/" // I put my own APP_URL here
login_path: /login
check_path: /login_fb_check
default_target_path: /
provider: my_fos_facebook_provider
logout:
handlers: ["fos_facebook.logout_handler"]
anonymous: true
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 }
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: ROLE_ADMIN
路由.yml
_security_check:
pattern: /login_fb_check
_security_logout:
pattern: /logout
此时,Facebook 图标在我的 /login 页面上,我可以连接到 Facebook,但我不会重定向到任何地方。我在 js 上放了一些警报以获取跟踪,并且只显示一个警报:“输入 onFbInit”。也许我在某个地方错过了一步,但我没有找到。
谢谢 !