GOOGLE ADS

sábado, 23 de abril de 2022

Redirección de Symfony si ya inició sesión desde la página de inicio de sesión

Estoy usando FOSUser Bundle para iniciar sesión. Ahora, si el usuario ya inició sesión, ¿cómo puedo redirigir al usuario a la página de inicio ('/'), si el usuario visita /login url.

Copié SecurityController en la ubicación src\AppBundle\Controller y cambié el método de inicio de sesión, pero no funciona.

método renderLogin()

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);
}

También he agregado esta línea en el controlador de seguridad,

use Symfony\Component\HttpFoundation\RedirectResponse;

Any help is much appreciated.


Solución del problema

Bueno, necesitas hacer algunos cambios en elSecurityController

 /**
* 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);
}
}

Y para redirigir a los usuarios autenticados que intentan visitar la página de registro, debe cambiar el ``

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(),
));
}
}

Es extraño que esto no se haya solucionado en FOSUserBundle, de todos modos, tenía dos Github Gists que manejan este problema:

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

Iniciar sesión: https://gist.github.com/teeyo/121e21b35d71a9ab4a8f321043b6f6cd

No hay comentarios:

Publicar un comentario

Regla de Firestore para acceder a la generación de subcolección Permisos faltantes o insuficientes

Tengo problemas con las reglas de Firestore para permitir el acceso a algunos recursos en una subcolección. Tengo algunos requests document...