For the past few days I've been struggling with logging in a database-user using Symfony/Doctrine, and I'm pretty stuck by now (I'm new to Symfony, btw). I got the login working using the in_memory provider (to start with), but now I want to login using a database-user and I just don't know what's wrong.
I went over the documentation over and over and I think I got everything right. I don't get errors, it just says "Invalid credentials." when I try to login. I have to feeling that it has something to do with security.yml but I've tried about all I could find. Below is my code;
security.yml;
security:
providers:
provider_users:
entity:
class: AppBundle:User
property: username
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
main:
pattern: ^/
anonymous: ~
provider: provider_users
form_login:
login_path: login
check_path: login
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
User.php;
<?php
// src/AppBundle/Entity/User.php
namespace AppBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Doctrine\ORM\Mapping as ORM;
/**
* AppBundle\Entity\User
*
* @ORM\Table(name="users")
* @ORM\Entity(repositoryClass="AppBundle\Entity\UserRepository")
*/
class User implements UserInterface, \Serializable
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id()
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="username", type="string", length=25, unique=true)
*/
private $username;
/**
* @ORM\Column(name="salt", type="string", length=40)
*/
private $salt;
/**
* @ORM\Column(name="password", type="string", length=40)
*/
private $password;
/**
* @ORM\Column(name="email", type="string", length=60, unique=true)
*/
private $email;
/**
* @ORM\Column(name="roles", type="string")
*/
private $roles;
/**
* @ORM\Column(name="is_active", type="boolean")
*/
private $isActive;
public function __construct()
{
$this->isActive = true;
$this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
}
public function eraseCredentials()
{
//
}
/** @see \Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
));
}
/** @see \Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
) = unserialize($serialized);
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/**
* Get username
*
* @return string
*/
public function getUsername()
{
return $this->username;
}
/**
* Set salt
*
* @param string $salt
*
* @return User
*/
public function setSalt($salt)
{
$this->salt = $salt;
return $this;
}
/**
* Get salt
*
* @return string
*/
public function getSalt()
{
return $this->salt;
}
/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/**
* Get password
*
* @return string
*/
public function getPassword()
{
return $this->password;
}
/**
* Set email
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;
return $this;
}
/**
* Get email
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
/**
* Set roles
*
* @param string $roles
*
* @return User
*/
public function setRoles($roles)
{
$this->roles = json_encode($roles);
return $this;
}
/**
* Get roles
*
* @return string[]
*/
public function getRoles()
{
return json_decode($this->roles);
}
/**
* Set isActive
*
* @param boolean $isActive
*
* @return User
*/
public function setIsActive($isActive)
{
$this->isActive = $isActive;
return $this;
}
/**
* Get isActive
*
* @return boolean
*/
public function getIsActive()
{
return $this->isActive;
}
}
I also tested if I could just get the user "manually" using getRepository()->findAll(), and that looks okay;
array (size=1)
0 =>
object(AppBundle\Entity\User)[323]
private 'id' => int 20
private 'username' => string 'user' (length=4)
private 'salt' => string 'mqshzqa9syok0kw8ss4cscc84k4k804' (length=31)
private 'password' => string 'user1' (length=5)
private 'email' => string 'user@localhost.com' (length=18)
private 'roles' => string '' (length=0)
private 'isActive' => boolean true
success!
Any suggestions? Thanks in advance!