Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HWIOAuthBundle - FOSUserBundle - Symfony 2 - Redirect to custom path after login with facebook

Reflected the following problem after the user login with the Facebook account: that is redirected to the following route /#_=_

How can I redirect it to this route instead: / Or to more to this /# ?

On the client side, I use the backbone.

like image 835
Angelo Giuffredi Avatar asked Dec 06 '22 05:12

Angelo Giuffredi


2 Answers

Taking @Prynz's idea, we can get further and create a "redirect to page user comes from" this way:

1) In your firewall, take care to remove the following lines:

# security.yml

# ...
logout: true
logout:
    path: /logout
    target: /

As we will implment the logout ourselves to avoid redirecting to the specified target.

2) Add @Prynz's solution to your security.yml (or config.yml depending on your implementation)

oauth:
    resource_owners:
        google: "/login/check-google"
        facebook: "/login/check-facebook"
        twitter: "/login/check-twitter"
        sensio_connect: "/login/check-sensio-connect"
    login_path: /login
    failure_path: /login
    default_target_path: /welcome # THIS LINE CONTRIBUTES TO THE MAGIC
    oauth_user_provider:
        service: app.oauth_user_provider

3) In your routing, add a new controller (here, LoginController) before HWIO's imports:

fuz_app_login:
    resource: "@FuzAppBundle/Controller/LoginController.php"
    type:     annotation
    prefix:   /

4) Create the corresponding controller:

<?php

namespace Fuz\AppBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;

class LoginController
{

    /**
     * @Route("/login", name="login")
     * @Method({"GET"})
     */
    public function loginAction(Request $request)
    {
        if ($this->getUser())
        {
            // already-logged user accessed /login
            return $this->redirect($request->headers->get('referer'));
        }
        else
        {
            // redirect to the login page
            return $this->forward('HWIOAuthBundle:Connect:connect');
        }
    }

    /**
     * @Route("/logout", name="logout")
     * @Method({"GET"})
     */
    public function logoutAction(Request $request)
    {
        // we do a manual logout just to redirect the user to where he comes from
        $this->container->get('security.context')->setToken(null);
        return $this->redirect($request->headers->get('referer'));
    }

    /**
     * @Route("/connect/{service}", name="connect")
     * @Method({"GET"})
     */
    public function connectAction(Request $request, $service)
    {
        // we overwrite this route to store user's referer in the session
        $this->get('session')->set('referer', $request->headers->get('referer'));
        return $this->forward('HWIOAuthBundle:Connect:redirectToService', array('service' => $service));
    }

    /**
     * @Route("/welcome", name="welcome")
     * @Method({"GET"})
     */
    public function welcomeAction()
    {
        // on login success, we're redirected to this route...
        // time to use the referer we previously stored.
        $referer = $this->get('session')->get('referer');
        if (is_null($referer))
        {
            return new RedirectResponse($this->generateUrl('home'));
        }
        return new RedirectResponse($referer);
    }

}

5) relax.

like image 163
Alain Tiemblo Avatar answered Dec 08 '22 19:12

Alain Tiemblo


You simply add default_target_path: /whatever/path/you/want to the oauth section under the firewall setup

oauth:
    resource_owners:
        facebook: '/login/check-facebook'
        google: '/login/check-google'
        windows: '/login/check-windows'
        twitter: '/login/check-twitter'
    login_path: /login
    failure_path: /login
    default_target_path: /whatever/path/you/want

Have a look at https://github.com/hwi/HWIOAuthBundle/issues/89

like image 28
Prynz Avatar answered Dec 08 '22 19:12

Prynz