1

I know that this question is perfectly answered in FOSUserBundle documentation, but yet I can't solve my problem.

I am trying to make an own login page using FOSUserBundle.

I have a main.html.twig that is EXACTLY the same as the login.html.twig included in the FOSUserBundle source.

The only difference is that main.html.twig is in my own bundle structure and login.html.twig is in FOSUserBundle folder structure.

I reach both /login and /main. I resolve them and the render starts.

But when accessing /main, I get the following error:

Twig_Error_Runtime: Variable "csrf_token" does not exist in "AcmeStoreBundle:Main:main.html.twig" at line 5

The code is known by the FOSUserBundle users, but anyway I paste it here:

<form action="{{ path("fos_user_security_check") }}" method="post">
<input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />

<label for="username">{{ 'security.login.username'|trans({}, 'FOSUserBundle') }}</label>
<input type="text" id="username" name="_username" value="" />

<label for="password">{{ 'security.login.password'|trans({}, 'FOSUserBundle') }}</label>
<input type="password" id="password" name="_password" />

<input type="checkbox" id="remember_me" name="_remember_me" value="on" />
<label for="remember_me">{{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }}</label>

<input type="submit" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}" />

The variable "csrf_token" is somehow not recognized from outside FOSUserBundle. Or something else that I am not getting.

Any clue over there?

j0k
  • 22,600
  • 28
  • 79
  • 90
ElPiter
  • 4,046
  • 9
  • 51
  • 80

1 Answers1

4

Since you are not using FormBuilder, in your action that is responsible for rendering main.html.twig you should generate this token and pass it to the View.

$csrf = $this->get('form.csrf_provider'); //Symfony\Component\Form\Extension\Csrf\CsrfProvider\SessionCsrfProvider by default
$token = $csrf->generateCsrfToken($intention); //Intention should be empty string, if you did not define it in parameters

You should pass $token as csrf_token variable to your View

Check also my answer to similar question

Community
  • 1
  • 1
Vitalii Zurian
  • 17,858
  • 4
  • 64
  • 81