HWIOAuthBundle - Can't redirect after successfull connection

2.6k views Asked by At

I can integrate fosuserbundle and hwioauthbundle based on danvbe's documentation. I can login by username/password, facebook and twitter. I also can connect the username to his facebook/twitter account. For example at the user profile page, I try to connect my username with my facebook account. after it connected, I stuck on the page (the url : http://localhost/Projects/symfony/web/app_dev.php/connect/service/facebook?key=12345678) with a message be displayed on the page 'Successfully connected the account 'my account'!'. there is no button to redirect to anywhere. how to redirect to the user profile page? Here are my settings: security.yml:

providers:
    fos_userbundle:
        id: fos_user.user_provider.username_email
firewalls:
    main:
        pattern: ^/
        form_login:
            provider: fos_userbundle
            login_path: /login
            csrf_provider: form.csrf_provider
        logout: true
        anonymous: true
        oauth:
            resource_owners:
                facebook:           "/login/check-facebook"
                twitter:            "/login/check-twitter"
            login_path:        /login
            use_forward:       false
            failure_path:      /login
            default_target_path: /profile

            oauth_user_provider:
                service: tf_user_provider

routing.yml:

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

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

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

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

hwi_oauth_facebook_login:
    pattern: /connect/facebook

facebook_login:
    pattern: /login/check-facebook

twitter_login:
    pattern: /login/check-twitter

config.yml:

fos_user:
    db_driver: orm
    firewall_name: main
    user_class: Acme\UserBundle\Entity\User
    registration:
        form:
            type: tf_user_registration
    profile:
        form:
            type: tf_user_registration
hwi_oauth:
    firewall_name: main
    resource_owners:
        facebook:
            type:          facebook
            client_id:     "id"
            client_secret: "secret"
            scope:         ""
        twitter:
            type:          twitter
            client_id:     "id"
            client_secret: "secret"
    fosub:
        # try 30 times to check if a username is available
        username_iterations: 30
        #mapping between resource owners and properties
        properties:
            facebook: facebookId
            twitter: twitterId
    # if you want to use 'connect' and do not use the FOSUB integration, configure these separately
    connect: 
        account_connector: tf_user_provider

        confirmation: true # should show confirmation page or not

    # optional HTTP Client configuration
    http_client:
        timeout:       100
        verify_peer:   true
        ignore_errors: true
        max_redirects: 5
3

There are 3 answers

0
Mark Shabalin On

Take a look at this:

# an optional setting to configure a query string parameter which can be used to redirect
# the user after authentication, e.g. /connect/facebook?_destination=/my/destination will
# redirect the user to /my/destination after facebook authenticates them.  If this is not
# set then the user will be redirected to the original resource that they requested, or
# the base address if no resource was requested.  This is similar to the behaviour of
# [target_path_parameter for form login](http://symfony.com/doc/2.0/cookbook/security/form_login.html).
# target_path_parameter: _destination

So I put target_path_parameter: _destination, used some JS to add this parameter to URL, and it worked!

0
ionafan2 On

HWIOAuthBundle don't have such functionality.

Simplest solution is javascript redirect in vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/views/Connect/connect_success.html.twig template.

Or this solution (not the best one but work for me fine)

Generate new empty OAuthBundle

php app/console generate:bundle

Override HWIOAuthBundle

public function getParent()
{
    return 'HWIOAuthBundle';
}

Override ConnectController

class ConnectController extends \HWI\Bundle\OAuthBundle\Controller\ConnectController

Override connectServiceAction. Rewrite part where the success connection handled and return RedirectResponse

        // Show confirmation page?
    if (!$this->container->getParameter('hwi_oauth.connect.confirmation')) {
        goto show_confirmation_page;
    }

    // Handle the form
    /** @var $form FormInterface */
    $form = $this->container->get('form.factory')
        ->createBuilder('form')
        ->getForm();

    if ($request->isMethod('POST')) {
        $form->bind($request);

        if ($form->isValid()) {
            show_confirmation_page:

            /** @var $currentToken OAuthToken */
            $currentToken = $this->container->get('security.context')->getToken();
            $currentUser  = $currentToken->getUser();

            $this->container->get('hwi_oauth.account.connector')->connect($currentUser, $userInformation);
            // new logic
            $this->container->get('session')->getFlashBag()->set('success', 'Successfully connected the '. $resourceOwner->getName() .' account');
            return new RedirectResponse($this->generate('connections'));
            // old logic
            //if ($currentToken instanceof OAuthToken) {
            //    // Update user token with new details
            //    $this->authenticateUser($request, $currentUser, $service, $currentToken->getRawToken(), false);
            //}

            //return $this->container->get('templating')->renderResponse('HWIOAuthBundle:Connect:connect_success.html.' . $this->getTemplatingEngine(), array(
            //    'userInformation' => $userInformation,
            //));
        }
    }

The best solution is fork and contribute event logic to HWIOAuthBundle repository ;)

0
Olaf Walkowiak On

Simply copy

vendor/hwi/oauth-bundle/HWI/Bundle/OAuthBundle/Resources/views/ 

content to

app/Resources/HWIOAuthBundle/views/

and edit the files. E.g. my layout.html.twig looks like this

{% set dialogTitle = 'register.connect' | trans([], 'dialogs')%}
{% extends '::base.html.twig' %}
{% block title %}{% trans%}app.title{% endtrans %}{% endblock %}
{% block content %}
  <div class="row">
    <div class="col-sm-12">
      <div class="dialog login">
        <div class="dialog-header">{{dialogTitle}}</div>
        <div class="dialog-body">
          {% block hwi_oauth_content %}
          {% endblock hwi_oauth_content %}
         </div>
      </div>
    </div>
  </div>
{% endblock %}

No prob to add a "continue" button or whatever you need