2

everyone and thank you for your future help

I am creating a blog and I want to put the login and registration page on the same view and for that I did all my treatment in login method

and return in the same view the 2 forms registration and connection

I already tried to make a render but it does not work and I even removed the token button no works


public function login(AuthenticationUtils $authenticationUtils, Request $request, UserPasswordEncoderInterface $passwordEncoder, MailerService $mailerService, \Swift_Mailer $mailer): Response
    {
        // get the login error if there is one
        $error = $authenticationUtils->getLastAuthenticationError();
        // last username entered by the user
        $lastUsername = $authenticationUtils->getLastUsername();

        $user = new User();
        $form = $this->createForm(RegistrationFormType::class, $user);
        $form->handleRequest($request);

        if ($form->isSubmitted() && $form->isValid()) {
            // encode the plain password
            $user->setPassword(
                $passwordEncoder->encodePassword(
                    $user,
                    $form->get('password')->getData()
                )
            );

            $user->setConfirmationToken($this->generateToken());

            $entityManager = $this->getDoctrine()->getManager();
            $entityManager->persist($user);
            $entityManager->flush();

            $token = $user->getConfirmationToken();
            $email = $user->getEmail();
            $username = $user->getUsername();

            $mailerService->sendToken($mailer, $token, $email, $username, 'registration.html.twig');

            return $this->redirectToRoute('app_login');
        }

        return $this->render('security/login.html.twig', ['last_username' => $lastUsername, 'error' => $error, 'registrationForm' => $form->createView()]);
    }

template :


    <div class="container">

        <div class="row">
            <div class="col-md-6">
                <div class="section-row">
                    <div class="section-title">
                        <h1 class="title text-center">Connexion</h1>
                    </div>
                    {% if error %}
                        <div class="alert alert-danger">{{ error.messageKey|trans(error.messageData, 'security') }}</div>
                    {% endif %}
                    <form method="post">
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input class="input" value="{{ last_username }}" type="text" name="username" placeholder="Nom d'utilisateur">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <input class="input" type="password" name="password" placeholder="Mot de passe">
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>
                                        <input type="checkbox" name="_remember_me"> Se souvenir de moi
                                    </label>
                                </div>
                            </div>
                            <div class="col-md-12">
                                <div class="form-group">
                                    <a href="{{ path('forgotten_password') }}">Mot de passe oublier</a>
                                </div>
                            </div>
                            <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
                            <div class="col-md-12">
                                <button class="primary-button" type="submit">
                                    Se connecter
                                </button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
            <div class="col-md-6">
                <h1>Register</h1>

                {{ form_start(registrationForm) }}
                {{ form_row(registrationForm.username) }}
                {{ form_row(registrationForm.password.first) }}
                {{ form_row(registrationForm.password.second) }}

                <button class="btn">Register</button>
                {{ form_end(registrationForm) }}

            </div>
        </div>

    </div>

the problem when I want to register it does not work it sends me back that the token is invalid while for this connect it works properly Thanks you for you help my friends !

Crown Backend
  • 170
  • 1
  • 4
  • 20
  • could be a caching issue on the GET request ... – Jakumi Apr 17 '19 at 04:31
  • Hello thanks for you response, that is to say ? I also put the post method in the controller? – Crown Backend Apr 17 '19 at 08:49
  • tbh, I'm not sure, if you're just getting served an old csrf token because of caching. However, I believe you don't need a csrf token for your login form: https://stackoverflow.com/questions/6412813/do-login-forms-need-tokens-against-csrf-attacks – Jakumi Apr 17 '19 at 08:57
  • I have already tried to remove the csrf token but it still does not work: / – Crown Backend Apr 17 '19 at 09:22
  • I feel like you have done something weird ... it looks like the registration from somehow handles the login ... – Jakumi Apr 17 '19 at 09:52
  • maybe the record takes the login token? because I have 2 token on the same page that it's login and registration – Crown Backend Apr 17 '19 at 12:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/191979/discussion-between-jakumi-and-crown-backend). – Jakumi Apr 17 '19 at 12:32

0 Answers0