I am developing a login with Symfony 3.4. When I try to enter with said login, it generates the error Invalid credentials. . I went ahead and implemented the login following the following tutorial link . In my database I have the following:
Whose sql structure is as follows:
I implemented my user class:
/**
* Usuario
*
* @ORM\Table(name="usuario")
* @ORM\Entity(repositoryClass="ComensalesBundle\Repository \UsuarioRepository")
*/
class Usuario implements UserInterface
{
/**
* @var int
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="nombreUsuario", type="string", length=255)
*/
private $nombreUsuario;
/**
* @var string
*
* @ORM\Column(name="contrasenia", type="string", length=255)
*/
private $contrasenia;
/**
* @var string
*
* @ORM\Column(name="rolUsuario", type="string", length=255)
*/
private $rolUsuario;
/**
* Get id
*
* @return int
*/
public function getId()
{
return $this->id;
}
/**
* Set nombreUsuario
*
* @param string $nombreUsuario
*
* @return Usuario
*/
public function setNombreUsuario($nombreUsuario)
{
$this->nombreUsuario = $nombreUsuario;
return $this;
}
/**
* Get nombreUsuario
*
* @return string
*/
public function getNombreUsuario()
{
return $this->nombreUsuario;
}
/**
* Set contrasenia
*
* @param string $contrasenia
*
* @return Usuario
*/
public function setContrasenia($contrasenia)
{
$this->contrasenia = $contrasenia;
return $this;
}
/**
* Get contrasenia
*
* @return string
*/
public function getContrasenia()
{
return $this->contrasenia;
}
/**
* Set rolUsuario
*
* @param string $rolUsuario
*
* @return Usuario
*/
public function setRolUsuario($rolUsuario)
{
$this->rolUsuario = $rolUsuario;
return $this;
}
/**
* Get rolUsuario
*
* @return string
*/
public function getRolUsuario()
{
return $this->rolUsuario;
}
public function getUsername()
{
return $this->nombreUsuario;
}
public function getSalt()
{
return null;
}
public function getRoles()
{
// En este caso definimos un rol fijo, en el caso de que tengamos un campo role en la tabla de la BBDD tendríamos que hacer $this->getRole()
return array('ROLE_USER');
}
public function eraseCredentials()
{
}
public function getPassword()
{
return $this->contrasenia;
}
// NO PERSISTIDO EN LA BD
private $plainPassword;
public function getPlainPassword()
{
return $this->plainPassword;
}
public function setPlainPassword($plainPassword)
{
$this->plainPassword = $plainPassword;
}
}
On the other hand, I configured my service.yml
providers:
user_provider:
entity:
class: ComensalesBundle:Usuario
property: nombreUsuario
encoders:
ComensalesBundle\Entity\Usuario:
algorithm: bcrypt
firewalls:
# disables authentication for assets and the profiler, adapt it according to your needs
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
anonymous: ~
form_login:
login_path: login
check_path: login
logout:
path: /logout
target: /login
access_control:
- { path: ^/registro, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/, roles: ROLE_USER }
The controller that shows me the form
namespace ComensalesBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
class DefaultController extends Controller
{
/**
* @Route("/login", name="login")
*/
public function loginAction(Request $request)
{
// Recupera el servicio de autenticación
$authenticationUtils = $this->get('security.authentication_utils');
// Recupera, si existe, el último error al intentar hacer login
$error = $authenticationUtils->getLastAuthenticationError();
// Recupera el último nombre de usuario introducido
$lastUsername = $authenticationUtils->getLastUsername();
// Renderiza la plantilla, enviándole, si existen, el último error y nombre de usuario
return $this->render('login.html.twig', array(
'last_username' => $lastUsername,
'error' => $error,
));
}
/*
* @Route("/logout", name="logout")
*/
public function logoutAction(Request $request)
{
// UNREACHABLE CODE
}
}
The template does not have many secrets:
{% extends 'base.html.twig' %}
{% block body %}
{# Muestra el error en caso de existir#}
{% if error %}
<div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
{# Input para el campo email #}
<label for="username">Email:</label>
<input type="text" id="username" name="_username" value="{{ last_username }}" />
{# Input para el campo contraseña #}
<label for="password">Contraseña:</label>
<input type="password" id="password" name="_password" />
{# Ruta a la que redirige si hay éxito #}
<input type="hidden" name="{{path('ventas_panel')}}" value="/" />
<button type="submit">Entrar</button>
</form>
{# Enlace al registro #}
{# <p><a href="{{ path('register') }}">Registro</a></p>#}
{% endblock %}
When I try to enter, with the user and pass registered in the database, it generates a credential error. It occurs to me that the error may be a data type or conversion problem. If you can guide me I would appreciate it.
Edit: I copy a part of the log: security.INFO: Authentication request failed. {"exception":"[object] (Symfony\Component\Security\Core\Exception\BadCredentialsException(code: 0): Bad credentials. Maybe it's relevant to help me. Thanks!
After working with this, I realized that I was not encrypting passwords with the bcypt algorithm as specified in security.yml .
You can see that when you see the following tutorial tutorial where in several steps it shows the configurations of the files.
If you want to create the users directly in the database, the password must be encrypted, so it is recommended to use the following app: bcypt calculator
Saving the encrypted password solved the problem. Now it remains to continue working with the project. Sometimes the lack of experience plays tricks on us. Cheers