0

I updated my website from Symfony 3.2 to Symfony 4, by creating a new symfony4 skeleton and moved the source code from symfony3.2 to symfony4.

I had been made the changes as mentioned in Upgrading Symfony4.Also I installed all the required packages.Now I can make routing and successfully got my web page on the display,the problem I am facing is login form doesn't work.

In my login controller for symfony3.2 version,

I used security.encoder_factory service

And when I am running it in symfony4 it results:

The "security.encoder_factory" service or alias has been removed or inlined when the container was compiled. You should either make it public, or stop using the container directly and use dependency injection instead.

My controller code for encoding password is:

public function loginAction(Request $request)
{
    #$defaultEncoder = new MessageDigestPasswordEncoder('bcrypt', true, 5000);
    $defaultEncoder = new MessageDigestPasswordEncoder('bcrypt');

    $encoders = [
        User::class => $defaultEncoder, // Your user class. This line specify you ant sha512 encoder for this user class
        ];

    $encoderFactory = new EncoderFactory($encoders);

    $data = [
        'error' => null,
        '_username' => null,
    ];
    if ($request->isMethod('POST')) {
        $_username = $request->request->get('_username');
        $_password = $request->request->get('_password');

        $data['_username'] = $_username;
        $user = $this->getDoctrine()->getManager()
            ->getRepository("App:User")
            ->findOneBy(array('username' => $_username);
        if (!$user) {
            $data['error']  = 'User-Id does not exist';
            return $this->render(
                'login.html.twig',
                $data
            );
        }

        $encoder = $encoderFactory->getEncoder($user);
        $salt = $user->getSalt();
        #$encoder = $this->encodePassword($_password, $salt);

        if (!$encoder->isPasswordValid($user->getPassword(), $encoder)) {
            $data['error'] = 'User-Id or Password not valid.';
            return $this->render(
                'login.html.twig',
                $data
            );
        } 

        return $this->redirect($this->generateUrl('default__home_page'));
    }

    return $this->render(
        'login.html.twig',
     $data
    );
}

My security for encoding is:

security:
   encoders:
        App\Entity\User: bcrypt
        App\Security\User\WebserviceUser: bcrypt
        Symfony\Component\Security\Core\User\User: bcrypt
        FOS\UserBundle\Model\UserInterface: sha512
# https://symfony.com/doc/current/security.html#where-do-users-come-from-    user-providers
    providers:
        #in_memory: { memory: ~ }
        our_db_provider:
            entity:
                class: App:User
                property: username
    fos_userbundle:
        id: fos_user.user_provider.username_email
firewalls:
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false
    secured_area:
        anonymous: ~
        pattern:    ^/s/login$
        http_basic: ~
        provider: fos_userbundle
        user_checker: security.user_checker

        form_login:
            login_path: login_page
            check_path: login_page
            failure_handler: security.authentication.failure_handler
        guard:
            authenticators:
                - App\Security\LoginControllerAuthenticator
        # activate different ways to authenticate

        # http_basic: ~
        # http://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate

        # form_login: ~
        # http://symfony.com/doc/current/cookbook/security/form_login_setup.html

        logout:
            path:   /logout/
            target: /login/

Is there any alternative way or service to encoding my password as the above code represents.

error:

No encoder has been configured for account "App\Entity\User".

  at vendor/symfony/security/Core/Encoder/EncoderFactory.php:51
  at Symfony\Component\Security\Core\Encoder\EncoderFactory->getEncoder(object(User))
     (src/Controller/Controller.php:65)
  at App\Controller\Controller->loginAction(object(Request))
     (vendor/symfony/http-kernel/HttpKernel.php:149)
  at Symfony\Component\HttpKernel\HttpKernel->handleRaw(object(Request), 1)
     (vendor/symfony/http-kernel/HttpKernel.php:66)
  at Symfony\Component\HttpKernel\HttpKernel->handle(object(Request), 1, true)
     (vendor/symfony/http-kernel/Kernel.php:188)
  at Symfony\Component\HttpKernel\Kernel->handle(object(Request))
     (public/index.php:37)

Thanks in advance.

A.JRJ
  • 331
  • 1
  • 5
  • 16

1 Answers1

0

See doc :

https://symfony.com/doc/current/components/security/authentication.html#the-password-encoder-factory

You can get encoder in you controller this way.

EDIT :

Here an example :

    $defaultEncoder = new MessageDigestPasswordEncoder('sha512', true, 5000);

    $encoders = [
      User::class => $defaultEncoder, // Your user class. This line specify you ant sha512 encoder for this user class
    ];

    $encoderFactory = new EncoderFactory($encoders);

    $user = new User('test', null); // The user you want to authenticate

    $password = $encoderFactory->getEncoder($user)->encodePassword('myPassword', 'mySalt');

EDIT 2 :

You have to defined YOUR user class there :

$encoders = [
    App\Entity\User::class => $defaultEncoder,
    ];

Check you're not using User Symfony class.

  • I didn't got cleared from the docs that why I am here...Can you suggest me how to overwrite the above code – A.JRJ Nov 15 '18 at 10:24
  • I updated the code. If you have any question, tell me. You can post your user provider ? – Thomas Lefetz Nov 15 '18 at 12:01
  • I got error as `No encoder has been configured for account "App\Entity\User"` but I configured this in security.yaml – A.JRJ Nov 15 '18 at 12:47
  • I updated my answer. Check your addind the encoder for YOUR user class. – Thomas Lefetz Nov 15 '18 at 13:18
  • Could it be that the encoder is configured in the yaml using bcrypt, and the MessageDigestPasswordEncoder is created in the controller using sha512? Two different algorithms? – Brent Heigold Feb 16 '19 at 01:46