I want to implement simple login/logout and registration procedure using symfony2 i start by reading tutorial and using symfony demo application but i can't get rid of this error from my log:
[2015-09-11 07:14:37] security.INFO: Authentication request failed. {"exception":"[object] (Symfony\Component\Security\Core\Exception\BadCredentialsException(code: 0): Bad credentials. at /home/saeed/ESN0.1/ESN0.1.0/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/UserAuthenticationProvider.php:90, Symfony\Component\Security\Core\Exception\BadCredentialsException(code: 0): The presented password is invalid. at /home/saeed/ESN0.1/ESN0.1.0/vendor/symfony/symfony/src/Symfony/Component/Security/Core/Authentication/Provider/DaoAuthenticationProvider.php:67)"} []
so it stays on the login page. this is my security.yml:
security:
encoders:
AppBundle\Entity\User:
algorithm: bcrypt
providers:
database_users:
entity: { class: AppBundle:User, property: username }
firewalls:
secured_area:
pattern: ^/
anonymous: true
form_login:
check_path: security_login_check
login_path: security_login_form
logout:
path: security_logout
target: security_login_form
access_control:
- { path: ^/admin, roles: ROLE_ADMIN }
This is the login template div:
<div id="login-form">
<form action="{{ path('security_login_check') }}" method="post">
<fieldset>
<div class="form-row">
<input type="text" id="username" name="_username" class="form-input" onfocus="this.placeholder = ''" onblur="this.placeholder='نام کاربری'" placeholder="نام کاربری" />
</div>
<div class="form-row">
<input type="password" id="password" name="_password" class="form-input" onfocus="this.placeholder = ''" onblur="this.placeholder = 'رمز عبور'" placeholder="رمز عبور"/>
</div>
<button id="login-btn" type="submit">ورود</button>
</fieldset>
</form>
</div>
and the controllers:
/**
* @Route("/login", name="security_login_form")
*/
public function loginAction()
{
// $user = $this->get('security.token_storage')->getToken()->getUser();
if($this->container->get('security.authorization_checker')->isGranted('IS_AUTHENTICATED_FULLY'))
return $this->redirectToRoute('dashboard');
$helper = $this->get('security.authentication_utils');
$isLogin = true;
return $this->render('::login-Register.html.twig', array(
// last username entered by the user (if any)
'last_username' => $helper->getLastUsername(),
// last authentication error (if any)
// 'error' => $helper->getLastAuthenticationError(),
'isLogin' => $isLogin,
));
}
/**
* This is the route the login form submits to.
*
* But, this will never be executed. Symfony will intercept this first
* and handle the login automatically. See form_login in app/config/security.yml
*
* @Route("/login_check", name="security_login_check")
*/
public function loginCheckAction()
{
throw new \Exception('This should never be reached!');
}
Registration controller
/**
*
* @Route("/register",name="register_user")
*/
public function registerAction(Request $request){
if($request->isMethod("POST")){
$userRepo = $this->getDoctrine()->getRepository('AppBundle:User');
$tempUser = $userRepo->find('AppBundle:User' , $request->get("username"));
try{
if(empty($tempUser)){
$plainPassword = $request->get("password");
$user = $userRepo->getUserByRequest($request);
$encoder = $this->container->get('security.password_encoder');
$encoded = $encoder->encodePassword($user, $plainPassword);
$user->setPassword($encoded);
$userRepo->saveUser($user);
$token = new UsernamePasswordToken($user, null, 'main', $user->getRoles());
$this->get('security.token_storage')->setToken($token);
return $this->redirectToRoute('dashboard');
}else{
throw new Exception('user already exist');
}
}catch (AuthenticationException $e){
return $this->render(
'login-Register.html.twig'
);
}
}else{
$isLogin = 2;
return $this->render('::login-Register.html.twig',
array(
'isLogin' => $isLogin,
));
}
}
finally the user entity fileds:
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="username", type="string", length=100,nullable=false,unique=true)
*/
private $username;
/**
* @var string
*
* @ORM\Column(name="password", type="string", length=255,nullable=false)
*/
private $password;
/**
* @var string
*
* @ORM\Column(name="email", type="string", length=100,nullable=false,unique=true)
*/
private $email;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=100)
*/
private $name;
i increase the length of password field to 255 but that does'nt solve my problem either.