Symfony如何在已认证时隐藏登录表单。

huangapple go评论65阅读模式
英文:

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,
) {
}

huangapple
  • 本文由 发表于 2023年6月8日 04:37:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76426958.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定