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

烂漫一生 提交于 2019-12-05 19:04:40

Redirect using javascript. Add following to the page.

<script>
    // Handle facebook callback
    if (window.location.hash && window.location.hash == '#_=_') {
        window.location.hash = '';
    }
</script>

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.

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

If you want, you can redirect the users to the current page doing this way: Add in your config.yml

hwi_oauth.target_path_parameter: "target_path"

In your view append the urls with:

&target_path=...

remember you can take the current route name with

app.request.get('_route')
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!