0
votes

I installed FOSUserBundle, but my design requires that the login form appears in the Home Page, not a separate login page, but included in the Home Page that is controlled by my HomeBundle:Default:index.

So if you access a page that requires login you would be redirected to my Home Page, where my login form is.

In order to do this: I changed the vendor > friendsofsymfony > user-bundle > FOS > Resources > routing > security.xml

<route id="fos_user_security_login" pattern="/login">
    <default key="_controller">FOSUserBundle:Security:login</default>
</route>

To point to the Controller that shows my HomePage. This works (it shows my login page) but here comes the problem, when I try to login it never logs me in and instead it shows the same page. The name of the inputs are correct and also de "action", same values as the default login form.

If I use the login page by default it logs me in, so the db is ok. I tried to print the errors that the log in process might trow printing the $error that figures out this code:

$request = $this->container->get('request'); /* @var $request \Symfony\Component\HttpFoundation\Request / $session = $request->getSession(); / @var $session \Symfony\Component\HttpFoundation\Session */

    // get the error if any (works with forward and redirect -- see below)
    if ($request->attributes->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $request->attributes->get(SecurityContext::AUTHENTICATION_ERROR);
    } elseif (null !== $session && $session->has(SecurityContext::AUTHENTICATION_ERROR)) {
        $error = $session->get(SecurityContext::AUTHENTICATION_ERROR);
        $session->remove(SecurityContext::AUTHENTICATION_ERROR);
    } else {
        $error = '';
    }

    if ($error) {
        // TODO: this is a potential security risk (see http://trac.symfony-project.org/ticket/9523)
        $error = $error->getMessage();
    }
    // last username entered by the user
    $lastUsername = (null === $session) ? '' : $session->get(SecurityContext::LAST_USERNAME);

    $csrfToken = $this->container->get('form.csrf_provider')->generateCsrfToken('authenticate');

But it seems that there's no error.

3

3 Answers

3
votes

Rewrite the file login.html.twig inside the folder Resources/views/Security/

the base code is :

{% extends "FOSUserBundle::layout.html.twig" %}

{% block fos_user_content %}
{% if error %}
    <div>{{ error|trans({}, 'FOSUserBundle') }}</div>
{% endif %}

<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="{{ last_username }}" required="required" />

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

    <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') }}" />
</form>
{% endblock fos_user_content %}
1
votes

Also don't forget to override the getParent method. As the documentation says.

<?php

namespace Acme\UserBundle;

use Symfony\Component\HttpKernel\Bundle\Bundle;
class AcmeUserBundle extends Bundle
{
    public function getParent()
   {
    return 'FOSUserBundle';
   }
}

?>
0
votes

You can follow instructions in here to overwrite template login.html.twig.

I had done it.