HWIOAuthBundle - FOSUserBundle - Symfony 2 - Redirect to user path after logging in using facebook

The following problem was reflected after the user logged in to the Facebook account: it redirects to the next route /#_=_

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

On the client side, I use the backbone.

+4
source share
4 answers

Redirect using javascript. Add the following page.

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

Accepting the idea of ​​@Prynz, we can continue and create a "redirect to user page" as follows:

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

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

How we will implement the exit from the system itself to avoid redirecting to the specified target .

2) Add the @Prynz 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 importing HWIO :

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

4) Create the appropriate 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 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.

+8
source

You simply add default_target_path: /whatever/path/you/want to the oauth section in the firewall settings

 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 

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

+5
source

If you want, you can redirect users to the current page as follows: Add to your config.yml

 hwi_oauth.target_path_parameter: "target_path" 

In your view, add the urls with:

 &target_path=... 

remember that you can take the name of the current route with

 app.request.get('_route') 
0
source

All Articles