Help
cancel
Showing results for 
Search instead for 
Did you mean: 
Copilot Lvl 2
Message 1 of 2

Login form not being submitted or even checked, but registration functioning

Trying to make login and registration form stay on one view and when submitted to go to the function for registration or login, but my login does not function as expected. It returns me to the home page with the data typed in the url. I do not even get error when submitting wrong credentials.

I have already changed some bits of the code on suggestion of a person from stackoverflow (replaced the generated registration form with hand written one, and the registration still works). But the login remains a problem.

My controller:

class DefaultController extends Controller
{
/**
 * @Route("/",name="home")
 */
public function homeAction(Request $request, UserPasswordEncoderInterface $passEncoder, AuthenticationUtils $authenticationUtils)
{    $greeting = $this->getDoctrine()->getRepository(UserGreetings::class)->randomGreet();    $user = new User();    $error = null;    $form = $this->createForm(UserType::class, $user);
    if($request->isMethod('POST')) {
        try {            $form->handleRequest($request);
            if ($form->isSubmitted()) {                $encryptedPassword = $passEncoder->encodePassword($user, $user->getPassword());                $user->setPassword($encryptedPassword);                $user->setIsActive();                $user->setRoles();                $em = $this->getDoctrine()->getManager();                $em->persist($user);                $em->flush();                $this->addFlash('message', 'You registered successfully! Now you can login. :)');
                return $this->redirectToRoute('home');

            }
        } catch (\Exception $exc) {            $error = 'Error in adding user: ' . $exc->getCode() . ' ' . $exc->getMessage();
        }
    }
    



    
    return $this->render('default/home.html.twig',
        ['form' => $form->createView(), 'error' => $error, 'greeting' => $greeting, 'name' => null, 'errorLog' =>null]);
}


/**
 * @Route("/log", name="log")
 * @param Request $request
 * @param AuthenticationUtils $authenticationUtils
 * @return \Symfony\Component\HttpFoundation\Response
 */
public function login(Request $request ,AuthenticationUtils $authenticationUtils)
{    $greeting = $this->getDoctrine()->getRepository(UserGreetings::class)->randomGreet();    $authError = $authenticationUtils->getLastAuthenticationError();    $userNameLast = $authenticationUtils->getLastUsername();

    return $this->render('default/home.html.twig', array('name' => $userNameLast, 'errorLog' =>$authError, 'greeting' => $greeting));
}

}

My view:

{% block body %}

<div class="card card-body" id="regged">

    <form action="{{ path('home') }}" method="post">
    <legend class="m-b-1 text-sm-center">Register</legend>
    <div class="form-group input-group">
        <input type="email" name="app_bundle_user_type[email]" id="mail">

        <label for="mail" class="form-control-placeholder">Email</label>
    </div>
    <div class="form-group input-group">
       
        <input type="text" name="app_bundle_user_type[username]" id="username">
        <label for="username" class="form-control-placeholder">Username</label>
    </div>
    <div class="form-group input-group">
        <input type="password" name="app_bundle_user_type[password][pass]" id="passwordFirst">

        <label for="passwordFirst" class="form-control-placeholder">First</label>
    </div>
    <div class="form-group input-group">

        <input type="password" name="app_bundle_user_type[password][conf]" id="passwordConf">
        <label for="passwordConf" class="form-control-placeholder">Confirm</label>
    </div>
    <div class="text-center">
        <input type="submit" name="app_bundle_user_type[Signup]">

    </div>
    </form>
</div>

<div class="card card-body" id="loged">
    {% if errorLog %}
        <p>
            {{ errorLog.messageKey|trans(errorLog.messageData, 'security') }}
        </p>
    {% endif %}
    <form action="{{ path('log') }}">
        <p>
            <input type="text" id="username" name="_username" placeholder="username..." value="{{ name }}"><br/><br/>
            <input type="password" id="password" name="_password" placeholder="password...">
            <input type="hidden" name="_target_path" value="{{ path('profile') }}">
            <input type="hidden" name="csrf_token" value="">
        </p>
        <button type="submit">Sign in</button>

    </form>
</div>
{% endblock %}

My security.yml(changed accordingly the annotation of the login function):

main:        anonymous: ~        form_login:          login_path: log
          check_path: log
        logout:          path: /logout
          target: /

I expect on submit of the login form with right credentials to be redirected to the profile page. Right now it returns URL with the submitted information and redirects to the home page(where is the login and registration form). Strangely enough my registration form works with no problem. Example of the returned URL after submittion of the form: http://localhost:8000/log?_username=pesho&_password=123&_target_path=%2Fprofile&csrf_token=

1 Reply
Community Manager
Message 2 of 2

Re: Login form not being submitted or even checked, but registration functioning

Hi @ivanpetrov95,

 

This post was moved to a different board that fits your topic of discussion a bit better. This means you’ll get better engagement on your post, and it keeps our community organized so users can more easily find information.

 

As you’ll notice, your topic is now here in the Programming help board. No action is needed on your part; you can continue the conversation as normal here.

 

Cheers!

Mark helpful posts with Accept as Solution to help other users locate important info. Don't forget to give Kudos for great content!