3

I use Symfony 2.8.16 with FOSUserBundle 1.3.7 fos user management. In "app/Resources", I have a "FOSUserBundle" folder to surcharge FOSUserBundle template with my template. Currently, I have a login page on the mywebsite.com/login route.

On the other public pages of my website, I want to put a small login form at the top of each page.

How can I do this?

In app/Resources/config/routing.yml

index: path: /index defaults: { _controller: MyBundle:MyController:MyAction } ################## # FOS User Bundle ################## fos_user_security: resource: "@FOSUserBundle/Resources/config/routing/security.xml" 

In app/Resources/FOSUserBundle/views/security/login.html.twig

{% extends ": MyLayout.html.twig" %} {% block fos_user_content %} {% if error %} <div class="alert alert-danger">{{ error|trans({}, 'FOSUserBundle') }}</div> {% endif %} <form action="{{ path("fos_user_security_check") }}" method="post"> <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" /> <div class="row"> <div class="form-group col-m-6"> <input type="text" class="_username" id="_username" name="_username" value="{{ last_username }}" placeholder="Email ou identifiant" required="required" /> </div> <div class="form-group col-m-6"> <input type="password" class="_password" id="_password" name="_password" placeholder="Mot de passe" required="required" /> </div> </div> <div class="form_action"> <button type="submit" class="button button-default-outline" id="_submit" name="_submit" value="{{ 'security.login.submit'|trans({}, 'FOSUserBundle') }}">Connexion</button> <div class="__sub"> <label class="remember"> <input type="checkbox" id="remember_me" name="_remember_me" checked> {{ 'security.login.remember_me'|trans({}, 'FOSUserBundle') }} <span class="separator">|</span> <a href="#" class="forgot">Mot de passe oublié ?</a> </label> </div> </div> </form> {% endblock fos_user_content %} 

1 Answer 1

5

You can add a login form on your layout that submits in the fos login check :

 <form action="{{ path('fos_user_security_check') }}" method="post"> <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}"> <input type="text" name="_username" /> <input type="password" name="_password" /> <input type="checkbox" name="_remember_me" checked /> <input type="submit" value="Submit" /> </form> 

You can, of course, change the style, add info and labels but this inputs must be there.

Sign up to request clarification or add additional context in comments.

4 Comments

But I know that the login form generated by FosUserBundle contain a csrf token generated by FOSUserBundle. But this method doesn't let FOSUserBundle generated it's csrf token
FOSUserBundle checks the csrf token wih the intention "authenticate" so this will work
Is this the properly method to make multiple login form ? And can I use the same method to the other FOSUserBundle form and views ?
I don't know for the other forms, I'm not that familiar with FOSUserBundle, sorry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.