0

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.

user2625638
  • 35
  • 1
  • 6
  • your solution won't work if you're trying to login user programmatically right after the registration process.. – xurshid29 Sep 11 '15 at 06:12
  • so how can i login right after registration and what is the relation between loging after registration and this problem? – user2625638 Sep 11 '15 at 06:25
  • Login process, registration process, programmatically logging users, they are all different operations. Usually, registration process just registers (adds users to db), login process sets the token, and you can read some instructions about manual authentication [here](http://stackoverflow.com/questions/29216670/symfony2-programmatically-authenticate-user) – xurshid29 Sep 11 '15 at 06:41
  • According to Symfony 2 examples you can register and instantly be logged in. Check this post http://stackoverflow.com/a/5957398/1830720 or check the security part from symfony documentation. – Rodolfo Velasco Sep 11 '15 at 17:32

0 Answers0