Search code examples
phpentity-frameworksymfonysymfony-2.7

Custom Entity login error Symfony2.7


I'm using Symfony 2.7 and I'm having trouble logging users. I use Custom Entity and I have created a custom entity "Operator" with proper variables and methods. I use two login methods in security file. One is in_memory and I use it for admin login which works correctly and the other one is user_db. I use bcrypt algorithm to encrypt the passwords. When I try to log in a user, it displays "Invalid credentials" message. What am I doing wrong? Thank you!

security.yml

security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext
        Application\AdminBundle\Entity\Operator:
            algorithm: bcrypt

providers:
        chain_provider:
            chain:
                providers: [ in_memory, user_db ]

user_db:
            entity:
                class: ApplicationAdminBundle:Operator
                property: username

SecurityController.php

<?php

namespace Application\AdminBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

class SecurityController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
    $authenticationUtils = $this->get('security.authentication_utils');

    $error = $authenticationUtils->getLastAuthenticationError();

    return $this->render('ApplicationAdminBundle:security:login.html.twig',
        array(
            'error'         => $error,
        )
    );
}

/**
* @Route("/login_check", name="login_check")
*/
public function loginCheckAction()
{
}

}

"Operator" Entity and setPassword(), getPassword() methods

public function setPassword($password)
    {
        $this->password = password_hash ($password, PASSWORD_BCRYPT);

        return $this;
    }

    /**
     * Get password
     *
     * @return string 
     */
    public function getPassword()
    {
        return $this->password;
    }

Solution

  • Solution:

    All I had done wrong was the database table because the I had set the password field as varchar(25) and the hashed password could not be fully inserted. I changed it to varchar(255) and everything is ok now. Every user can now login.