Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Login programmatically does not work #1113

Open
darkopetreski opened this issue Feb 7, 2023 · 2 comments
Open

Login programmatically does not work #1113

darkopetreski opened this issue Feb 7, 2023 · 2 comments

Comments

@darkopetreski
Copy link

darkopetreski commented Feb 7, 2023

As described in the official documentation
https://symfony.com/doc/current/security.html#login-programmatically
I am trying to login a user automatically as suggested in the official documentation for Symfony 6.2:

    use Symfony\Bundle\SecurityBundle\Security;

    public function autoLogin(Security $security): Response 
    {
        $user = ....
        $security->login($user);
        ....
    }

But I get the following error:

"Lexik\\Bundle\\JWTAuthenticationBundle\\Security\\Authenticator\\Token\\JWTPostAuthenticationToken::__construct(): 
Argument #4 ($token) must be of type string, null given, called in 
/project/vendor/lexik/jwt-authentication-bundle/Security/Authenticator/JWTAuthenticator.php on line 272"

I am using this bundle with cookie token extractor.
The login method was introduced in 6.2 so maybe this bundle is not compatible with this method or more setup is needed ?

@darkopetreski
Copy link
Author

darkopetreski commented Feb 11, 2023

For now I have solved the problem by the following code:

    use Lexik\Bundle\JWTAuthenticationBundle\Security\Http\Authentication\AuthenticationSuccessHandler;

    #[Route('/autologin', methods: ['GET'])]
    public function autologin(AuthenticationSuccessHandler $authenticationSuccessHandler): Response 
    {
        $user = ....
        return $authenticationSuccessHandler->handleAuthenticationSuccess($user);
    }

@BooleanType
Copy link

Had the same. @darkopetreski approach works for me too and is even described in bundle's documentation:

public function fooAction(UserInterface $user)
{
    $authenticationSuccessHandler = $this->container->get('lexik_jwt_authentication.handler.authentication_success');

    return $authenticationSuccessHandler->handleAuthenticationSuccess($user);
}

Hope login programmatically will work with $security->login($user); as well one day.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants