英文:
Symfony how to hide login form if authenticated
问题
如果用户已经经过身份验证,但不知何故又进入了(回到了)/login路由,登录表单会显示出来,看起来就好像用户实际上没有登录。
我如何从那里进行重定向?我尝试了下面的方法...
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->isGranted('IS_AUTHENTICATED_FULLY') || $this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
$user = $this->getUser();
$startpage = $user->getStartpage();
if ($startpage) {
$targetUrl = $this->generateUrl($startpage);
} else {
$targetUrl = $this->generateUrl('user_dashboard');
}
return $this->redirect($targetUrl);
}
return $this->render('security/login_microsoft.html.twig', [
'error' => $authenticationUtils->getLastAuthenticationError(),
'last_username' => $authenticationUtils->getLastUsername(),
]);
}
}
... 但我得到这个错误:
警告:未定义的属性:
App\Controller\SecurityController::$router
英文:
If user is authenticated but somehow lands on (goes back to) the /login route, the login form is displayed, making it appear as though user is in fact not logged in.
How can I redirect from that? I tried this below...
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\Security\Http\Authentication\AuthenticationUtils;
class SecurityController extends AbstractController
{
/**
* @Route("/login", name="app_login")
*/
public function login(AuthenticationUtils $authenticationUtils): Response
{
if ($this->isGranted('IS_AUTHENTICATED_FULLY') || $this->isGranted('IS_AUTHENTICATED_REMEMBERED')) {
$user = $this->getUser();
$startpage = $user->getStartpage();
if ( $startpage ) {
$targetUrl = $this->router->generate($startpage);
} else {
$targetUrl = $this->router->generate('user_dashboard');
}
return $this->redirect($targetUrl);
//return new RedirectResponse($targetUrl);
}
return $this->render('security/login_microsoft.html.twig', [
'error' => $authenticationUtils->getLastAuthenticationError(),
'last_username' => $authenticationUtils->getLastUsername(),
]);
}
... but I get this error:
> Warning: Undefined property:
> App\Controller\SecurityController::$router
答案1
得分: 1
只是为了修复您的错误:
if ( $startpage ) {
$targetUrl = $this->get('router')->generate($startpage);
} else {
$targetUrl = $this->get('router')->generate('user_dashboard');
}
您还可以在您的控制器中注入路由器服务,并通过在控制器内添加以下内容来摆脱AbstractController:
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
public function __construct(
private UrlGeneratorInterface $router,
) {
}
英文:
Just to fix your error:
if ( $startpage ) {
$targetUrl = $this->get('router')->generate($startpage);
} else {
$targetUrl = $this->get('router')->generate('user_dashboard');
}
You can also inject the router service in your Controller and get rid of AbstractController by adding this inside your Controller:
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
public function __construct(
private UrlGeneratorInterface $router,
) {
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论