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_start(form) }}#}
<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">
{#{{ form_widget(form.email, {'id': 'mail', 'attr': {'class': 'form-control', 'placeholder': 'Type here'}}) }}#}
<label for="mail" class="form-control-placeholder">Email</label>
</div>
<div class="form-group input-group">
{#{{ form_widget(form.username, {'id': 'username', 'attr': {'class': 'form-control', 'placeholder': 'Type here'}}) }}#}
<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">
{#{{ form_widget(form.password.pass, {'id': "passwordFirst", 'attr': {'class': 'form-control', 'placeholder': 'Password'}}) }}#}
<label for="passwordFirst" class="form-control-placeholder">First</label>
</div>
<div class="form-group input-group">
{#{{ form_widget(form.password.conf, {'id': "passwordConf", 'attr': {'class': 'form-control', 'placeholder': 'Password'}}) }}#}
<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]">
{#{{ form_row(form.Signup, {'attr':{'class': 'btn btn-success btn-sm'}}) }}#}
</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=