email - Symfony2 custom user provider can't retrieve user -
i followed guide implement own custom user login. unfortunately says bad credentials
during login. exception comes line 72 of symfony\component\security\core\authentication\provider\userauthenticationprovider
. exception gets thrown because can't retrieve user.
what changed custom needs users not have username. login email address. think no problem implement.
security.yml:
security: encoders: acme\userbundle\entity\user: plaintext role_hierarchy: role_admin: role_user role_super_admin: [role_user, role_admin, role_allowed_to_switch] providers: administrators: entity: { class: acmeuserbundle:user } firewalls: secured_area: pattern: ^/ anonymous: ~ form_login: ~ access_control: - { path: ^/login, roles: is_authenticated_anonymously } - { path: ^/register, roles: is_authenticated_anonymously } - { path: ^/, roles: role_user }
userrepository:
class userrepository extends entityrepository implements userproviderinterface { public function loaduserbyusername($username) { $q = $this ->createquerybuilder('u') ->where('u.email = :email') ->setparameter('email', $username) ->getquery(); try { // query::getsingleresult() method throws exception // if there no record matching criteria. $user = $q->getsingleresult(); } catch (noresultexception $e) { $message = sprintf( 'unable find active admin acmeuserbundle:user object identified "%s".', $username ); throw new usernamenotfoundexception($message, 0, $e); } return $user; } public function refreshuser(userinterface $user) { $class = get_class($user); if (!$this->supportsclass($class)) { throw new unsupporteduserexception( sprintf( 'instances of "%s" not supported.', $class ) ); } return $this->find($user->getid()); } public function supportsclass($class) { return $this->getentityname() === $class || is_subclass_of($class, $this->getentityname()); } }
login.twig.html:
{% if error %} <div>{{ error.message }}</div> {% endif %} <form action="{{ path('login_check') }}" method="post"> <legend>login</legend> <label for="email">email:</label> <input type="email" id="email" name="_email" value="{{ email }}" <label for="password">password:</label> <input type="password" id="password" name="_password" /> <button type="submit">login</button> </form>
what have done wrong here? in userrepository
queries email username, why can't find user? have speculation has csrf_token
? how can add controller , twig file? problem @ oder else did wrong?
by default symfony security uses _username
, _password
parameters authenticate user via form submit. can see in security configuration reference
form_login: username_parameter: _username password_parameter: _password
so need place _username
field name insteadof _email
.
Comments
Post a Comment