Search code examples
phpsymfonyhwioauthbundle

PHP Symfony3 - HWIOAuthBundle: Facebook Login redirecting problems


At the start, I can say that I had implemented the Facebook Login button and after I'm clicking on it, it's asking me for login details > app allow > and after I'm trying to put my credentials, is not redirecting me on dashboard, and is not logging me in.

How can I make the login to work, and after to redirect me on www.example.com/dashboard

1)config.yml looks like this:

imports:
    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }


# Put parameters here that don't need to change on each machine where the app is deployed
# http://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    locale: de


framework:
    #esi:             ~
    translator:      { fallbacks: ["%locale%"] }
    secret:          "%secret%"
    router:
        resource: "%kernel.root_dir%/config/routing.yml"
        strict_requirements: ~
    form:            ~
    csrf_protection: ~
    validation:      { enable_annotations: true }
    #serializer:      { enable_annotations: true }
    templating:
        engines: ['twig']
    default_locale:  "%locale%"
    trusted_hosts:   ~
    trusted_proxies: ~
    session:
        # http://symfony.com/doc/current/reference/configuration/framework.html#handler-id
        handler_id:  session.handler.native_file
        save_path:   "%kernel.root_dir%/../var/sessions/%kernel.environment%"
    fragments:       ~
    http_method_override: true
    assets: ~


# Twig Configuration
twig:
    debug:            "%kernel.debug%"
    strict_variables: "%kernel.debug%"
    form_themes:
        - 'common/form.html.twig'
        - 'VichUploaderBundle:Form:fields.html.twig'
    globals:
        host: "%host%"


# Doctrine Configuration
doctrine:
    dbal:
        driver:   pdo_mysql
        host:     "%database_host%"
        port:     "%database_port%"
        dbname:   "%database_name%"
        user:     "%database_user%"
        password: "%database_password%"
#        charset:  UTF8

    orm:
        auto_generate_proxy_classes: "%kernel.debug%"
        naming_strategy: doctrine.orm.naming_strategy.underscore
        auto_mapping: true
        dql:
            string_functions:
                MONTH: DoctrineExtensions\Query\Mysql\Month
                YEAR: DoctrineExtensions\Query\Mysql\Year


# Swiftmailer Configuration
swiftmailer:
    transport: "%mailer_transport%"
    host:      "%mailer_host%"
    auth_mode: "%mailer_authmode%"
    username:  "%mailer_user%"
    password:  "%mailer_password%"
    spool:     { type: memory }


assetic:
    debug:          '%kernel.debug%'
    use_controller: '%kernel.debug%'
    filters:
      scssphp:
        formatter: 'Leafo\ScssPhp\Formatter\Compressed'


# FOSUserBundle configuration
fos_user:
    db_driver: orm
    firewall_name: main
    user_class: MyDinxx\UserBundle\Entity\User
    from_email:
      address:        [email protected]
      sender_name:    MyDinxx
    registration:
      form:
        type: MyDinxx\UserBundle\Form\RegistrationType # override default registration form
      confirmation:
        enabled: true # enable E-Mail confirmation


# FOSMessageBundle configuration
fos_message:
    db_driver: orm
    thread_class: MyDinxx\UserBundle\Entity\Thread
    message_class: MyDinxx\UserBundle\Entity\Message


# VichUploaderBundle configuration (avatars)
vich_uploader:
    db_driver: orm

    mappings:
        avatar:
            uri_prefix:         /upload/avatar
            upload_destination: '%kernel.root_dir%/../web/upload/avatar'
            namer:
                service: vich_uploader.namer_property
                options: { property: 'id'}

# HWIOAuthBundle
hwi_oauth:
    connect:
        confirmation: true
        registration_form: fos_user.registration.form.factory
    firewall_names: [secured_area]
    fosub:
        username_iterations: 30
        properties:
            facebook: facebookID
    resource_owners:
        facebook:
            type:                facebook
            client_id:           #myClientID(after I've created the app on faceobok developers)
            client_secret:       #myClientSecret(after I created the app on faceobok developers)
            scope: "email"

2)routing.yml looks like this:

AppBundle:
    resource: "@AppBundle/Controller/"
    type:     annotation

FrontendBundle:
    resource: "@FrontendBundle/Controller/"
    type:     annotation

FOSUserBundle:
    resource: "@FOSUserBundle/Resources/config/routing/all.xml"

FOSMessageBundle:
    resource: "@FOSMessageBundle/Resources/config/routing.xml"
    prefix:   /messenger

MyDinxxUserBundle:
    resource: "@UserBundle/Controller/"
    type:     annotation

# HWIOAuthBundle
hwi_oauth_redirect:
    resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
    prefix:   /connect

hwi_oauth_connect:
    resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
    prefix:   /connect

hwi_oauth_login:
    resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
    prefix:   /login

3)security.yml looks like this:

# To get started with security, check out the documentation:
# http://symfony.com/doc/current/book/security.html
security:
    encoders:
        FOS\UserBundle\Model\UserInterface: bcrypt

    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
        ROLE_SUPER_ADMIN: ROLE_ADMIN

    providers:
        fos_userbundle:
            id: fos_user.user_provider.username_email

    firewalls:
        # disables authentication for assets and the profiler, adapt it according to your needs
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        main:
            pattern: ^/
            form_login:
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
            logout: true
            anonymous: true
            remember_me:
                secret:   '%secret%'
                lifetime: 604800 # 1 week in seconds
                path:     /
                # by default, the feature is enabled by checking a
                # checkbox in the login form (see below), uncomment the
                # following line to always enable it.
                #always_remember_me: true

        secured_area:
                    anonymous: ~
                    oauth:
                        resource_owners:
                            facebook:           "/login/check-facebook"
                            google:             "/login/check-google"
                            my_custom_provider: "/login/check-custom"
                            my_github:          "/login/check-github"
                        login_path:        /login
                        use_forward:       false
                        failure_path:      /login

                        oauth_user_provider:
                            service: hwi_oauth.user.provider.fosub_bridge


    access_control:
        - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/register$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/resetting$, role: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/admin, roles: ROLE_ADMIN }
        - { path: ^/shipment, roles: ROLE_USER }

I tried and I think I've made all possible configs.. and remain without ideas. In my database, I have a column named facebook_id (varchar, nullable), and, in my User Entity I've put it as:

/**
* FacebookID of user
*
* @ORM\Column(type="string", name="facebook_id", nullable=true)
*/
protected $facebookID;

Solution

  • My setup looks like this : in case it helps..

     main:
            pattern: ^/
            form_login:
                login_path: /prelogin
                #success_handler: authentication.handler.login_success_handler
                provider: fos_userbundle
                csrf_token_generator: security.csrf.token_manager
                always_use_default_target_path: false
                default_target_path:            /start
                #use_referer: true
                # if you are using Symfony < 2.8, use the following config instead:
                # csrf_provider: form.csrf_provider
            oauth:
    
                failure_path: start
                login_path: /prelogin
                check_path: /connect_check
                default_target_path:            /start
                provider: fos_userbundle
                resource_owners:
                    facebook: "/login/check-facebook"
                    google: "/login/check-google"
                    twitter: "/login/check-twitter"
                    linkedin: "/login/check-linkedin"
                oauth_user_provider:
                    service: app.provider.oauth
            logout:       true
            anonymous:    true
    

    Try playing around with the default_target_path and check_path settings ..

    config.yml part:

    hwi_oauth:
        # name of the firewall in which this bundle is active, this setting MUST be set
        firewall_names: [main]
        connect:
            account_connector: app.provider.oauth
        resource_owners:
            facebook:
                type:                facebook
                client_id:           %facebook_client_id%
                client_secret:       %facebook_client_secret%
                scope:               "email, public_profile"
                infos_url:           "https://graph.facebook.com/me?fields=id,name,email,picture.type(large)"
                paths:
                    email: email
                options:
                    display: popup #dialog is optimized for popup window
                    auth_type: rerequest # Re-asking for Declined Permissions
    

    routing.yml part:

    facebook_login:
        path: /login/check-facebook
    
    hwi_oauth_redirect:
        resource: "@HWIOAuthBundle/Resources/config/routing/redirect.xml"
        prefix:   /connect
    
    hwi_oauth_connect:
        resource: "@HWIOAuthBundle/Resources/config/routing/connect.xml"
        prefix:   /connect
    
    hwi_oauth_login:
        resource: "@HWIOAuthBundle/Resources/config/routing/login.xml"
        prefix:   /login