Symfony redirect after login with condition on user

前端 未结 2 1443
故里飘歌
故里飘歌 2021-01-13 12:02

My symfony3 login page redirects to home by default, as stated inside my security.yml file.

However, I want it to redirect to my \"Edit profile\" page i

2条回答
  •  悲&欢浪女
    2021-01-13 12:20

    I assume you are using the Guard Authentication system. ( http://symfony.com/doc/current/security/guard_authentication.html )

    Then you should have a class extending the AbstractGuardAuthenticator class.

    In that class, there is a method called onAuthenticationSuccess, in here you can put some logic for redirecting the request. If you return null here, it will just continue, and use the route configured in your security.yml.

    You will need to pass the @router service to the Authenticator class through dependencyInjection.

    Assuming you passed the router service, your method will look something like this:

    public function onAuthenticationSuccess(Request $request, TokenInterface $token, $providerKey)
    {
        /** @var User $user */
        $user = $token->getUser();
    
        if ($user->hasCompleteProfile() == false) {
            $url = $this->router->generate('edit_profile');
    
            return new RedirectResponse($url);
        }
    
        // Continue with default behaviour
        return null;
    }
    

提交回复
热议问题