Question

I am using Symfony FOSUserBundle, SonataUserBundle, FOSFacebookBundle. I have added login button to my app.

I am able to login using facebook but after login the page redirects to demo/secured/login_check and I get the below error.

"The controller must return a response (null given). Did you forget to add a return statement somewhere in your controller?"

I already had look at

Service not triggered : The controller must return a response (null given)

https://github.com/FriendsOfSymfony/FOSFacebookBundle/issues/186

But not sure what changes should I make to my configuration.

Below if my security configuration

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

    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        SONATA:
            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are using acl then this line must be commented

    providers:
        chain_provider:
            chain:
                providers: [fos_userbundle, fo_fos_facebook_provider]      
        fos_userbundle:
            id: fos_user.user_manager
        fo_fos_facebook_provider:
            id: my.facebook.user

    firewalls:
        # Disabling the security for the web debug toolbar, the profiler and Assetic.
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
            anonymous:          true

        main:
            pattern: ^/
            context:             user
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true

        public:
            # since anonymous is allowed users will not be forced to login
            pattern:   ^/.*
            fos_facebook:
                app_url: "http://apps.facebook.com/my-app/"
                server_url: "http://localhost/me/my/symfony/web/app_dev.php/"
                login_path: /login
                check_path: /login_check
                default_target_path: /
                provider: fo_fos_facebook_provider
                redirect_to_facebook_login: false
            anonymous: true

    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users      
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        # Admin login page needs to be access without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

        - { path: ^/secured/.*, role: [IS_AUTHENTICATED_FULLY] }

    acl:
        connection: default        

I tried changing configurations as below to avoid 2 firewall matching same URL pattern.

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

    role_hierarchy:
        ROLE_ADMIN:       [ROLE_USER, ROLE_SONATA_ADMIN]
        ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
        SONATA:
            - ROLE_SONATA_PAGE_ADMIN_PAGE_EDIT  # if you are using acl then this line must be commented

    providers:
        chain_provider:
            chain:
                providers: [fos_userbundle, fo_fos_facebook_provider]      
        fos_userbundle:
            id: fos_user.user_manager
        fo_fos_facebook_provider:
            id: my.facebook.user

    firewalls:
        # Disabling the security for the web debug toolbar, the profiler and Assetic.
        dev:
            pattern:  ^/(_(profiler|wdt)|css|images|js)/
            security: false

        # -> custom firewall for the admin area of the URL
        admin:
            pattern:            /admin(.*)
            context:            user
            form_login:
                provider:       fos_userbundle
                login_path:     /admin/login
                use_forward:    false
                check_path:     /admin/login_check
                failure_path:   null
            logout:
                path:           /admin/logout
            anonymous:          true

        main:
            pattern: ^/
            context:             user
            fos_facebook:
                app_url: "http://apps.facebook.com/my-app/"
                server_url: "http://localhost/me/my/symfony/web/app_dev.php/"
                login_path: /login
                check_path: /login_check
                default_target_path: /
                provider: fo_fos_facebook_provider
                redirect_to_facebook_login: false                
            form_login:
                provider:       fos_userbundle
                login_path:     /login
                use_forward:    false
                check_path:     /login_check
                failure_path:   null
                csrf_provider: form.csrf_provider
            logout:       true
            anonymous:    true

    access_control:
        # URL of FOSUserBundle which need to be available to anonymous users      
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY }
        # Admin login page needs to be access without credential
        - { path: ^/admin/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/logout$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY }

        # Secured part of the site
        # This config requires being logged for the whole site and having the admin role for the admin part.
        # Change these rules to adapt them to your needs
        - { path: ^/admin/, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN] }
        - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }

        - { path: ^/secured/.*, role: [IS_AUTHENTICATED_FULLY] }

    acl:
        connection: default        

But I am now getting different error as bellow

"InvalidConfigurationException: You are not allowed to define new elements for path "security.firewalls". Please define all elements for this path in one config file."

Was it helpful?

Solution 2

I figured it out and sharing my code here

https://github.com/vishalmelmatti/FOSSonataUserFacebookIntegration

Its fully working integration of FOSUserBundle FOSFacebookBundle SonataAdminBundle SonataUserBundle.

OTHER TIPS

As reported on giyhub issue: "You cannot have 2 firewalls with the same pattern: the first firewall matched will be used, so your public firewall cannot be used as main is already a catch-all"

Your main pattern and your public pattern matches the same route. You can try it here: http://www.regular-expressions.info/javascriptexample.html

Case: ^/ subject: /home OK Case: ^/.* subject /home OK

So you have two firewalls that matches exactly the same URL. You have to change the routing rule of your public firewall or main firewall.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top