Symfony redirect if already loggedin from login page

浪尽此生 提交于 2019-12-25 11:58:54

问题


I'm using FOSUser Bundle to login. Now if user is already loggedin, how can I redirect user to homepage ('/'), if user visit to /login url.

I have copied SecurityController to src\AppBundle\Controller location and changed renderlogin method but it doesn't work.

renderLogin() method

protected function renderLogin(array $data)
{
    if (false === $this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_ANONYMOUSLY')) {
        return new RedirectResponse('/', 403);
    }
    return $this->render('@FOSUser/Security/login.html.twig', $data);
}

I have added this line as well in the security controller,

use Symfony\Component\HttpFoundation\RedirectResponse;

Any help is much appreciated.


回答1:


Well you need to make some changes in the SecurityController

 /**
 * Renders the login template with the given parameters. Overwrite this function in
 * an extended controller to provide additional data for the login template.
 *
 * @param array $data
 *
 * @return Response
 */
protected function renderLogin(array $data)
{
   /**
    * If the user has already logged in (marked as is authenticated fully by symfony's security)
    * then redirect this user back (in my case, to the dashboard, which is the main entry for 
    * my logged in users)
    */
   if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
       return $this->redirectToRoute('homepage');
   }
    return $this->render('@FOSUser/Security/login.html.twig', $data);
}
}

And to redirect authenticated users who try to visit the registration page, you need to change the ``

class RegistrationController extends BaseController
{
/**
 * @param Request $request
 *
 * @return Response
 */
public function registerAction(Request $request)
{
    /**
    * If the user has already logged in (marked as is authenticated fully by symfony's security)
    * then redirect this user back (in my case, to the dashboard, which is the main entry for 
    * my logged in users)
    */
    if ($this->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY')) {
       return $this->redirectToRoute('homepage');
    }
    /** @var $formFactory FactoryInterface */
    $formFactory = $this->get('fos_user.registration.form.factory');
    /** @var $userManager UserManagerInterface */
    $userManager = $this->get('fos_user.user_manager');
    /** @var $dispatcher EventDispatcherInterface */
    $dispatcher = $this->get('event_dispatcher');
    $user = $userManager->createUser();
    $user->setEnabled(true);
    $event = new GetResponseUserEvent($user, $request);
    $dispatcher->dispatch(FOSUserEvents::REGISTRATION_INITIALIZE, $event);
    if (null !== $event->getResponse()) {
        return $event->getResponse();
    }
    $form = $formFactory->createForm();
    $form->setData($user);
    $form->handleRequest($request);
    if ($form->isSubmitted()) {
        if ($form->isValid()) {
            $event = new FormEvent($form, $request);
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_SUCCESS, $event);
            $userManager->updateUser($user);
            if (null === $response = $event->getResponse()) {
                $url = $this->generateUrl('fos_user_registration_confirmed');
                $response = new RedirectResponse($url);
            }
            $dispatcher->dispatch(FOSUserEvents::REGISTRATION_COMPLETED, new FilterUserResponseEvent($user, $request, $response));
            return $response;
        }
        $event = new FormEvent($form, $request);
        $dispatcher->dispatch(FOSUserEvents::REGISTRATION_FAILURE, $event);
        if (null !== $response = $event->getResponse()) {
            return $response;
        }
    }
    return $this->render('@FOSUser/Registration/register.html.twig', array(
        'form' => $form->createView(),
    ));
}
}

It's weird that this has not been fixed in the FOSUserBundle, anyway I had two Github Gists that handles this issue:

Registration : https://gist.github.com/teeyo/147f2d5d21d1beadce133a51b02d9570

Login : https://gist.github.com/teeyo/121e21b35d71a9ab4a8f321043b6f6cd




回答2:


In the FOS securityController (for login) : add this to login action

if($session->get("_security_main"))
{
$route = $this->container->get('router')->generate('site_faim_homepage');
return new RedirectResponse($route);

    }


来源:https://stackoverflow.com/questions/46484181/symfony-redirect-if-already-loggedin-from-login-page

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