Symfony2: how to implement user login and registration - get rid of FOSUSerBundle

I am trying to get rid of FOSUserBundle because I wanted to implement a role object related to many relationships with my user.

So my question is: will the walkthrough replace this FOSUserBundle? If not, can someone guide me, at least through the elements that I need to implement, because I feel like I always forgot something.

So far, I:

  • The class user implements AdvancedUserInterface, \ Serializable
  • The UserProvider class implements UserProviderInterface
  • A controller with the public function registerAction (Request $ request) for processing user storage in the database.

Here I can save the user, and I see that the salt, password, username and email address are saved, but after processing the request, I get nothing and login is impossible.

Should I also implement:

  • Custom Authentication Provider
  • or custom API keys

If so, which one?

And then?

My registration code: why is the return statement not called?

/**
 * @Route("/register_user", name="register_user")
 * @Method({"GET","POST"})
 * @param Request $request
 * @param bool $extendLayout
 * @return null|\Symfony\Component\HttpFoundation\RedirectResponse|\Symfony\Component\HttpFoundation\Response
 */
public function registerAction(Request $request, $extendLayout=true)
{
    $formManager = $this->get('form_manager');
    $user = new User();
    $user->setEnabled(true);

    $form = $formManager->createForm(new UserRegistrationType(), $user, 'POST', 'register_user',null, false);

    if ($request->getMethod() == 'POST')
    {
        if ($formManager->handleRequestAndValidate($form))
        {

            $userSecurityManager = $this->get('user_security_manager');
            $userSecurityManager->updateUser($user, true);

            //This return won't get called, is there an eventlistener on the register event ?
            return $this->redirect($this->generateUrl('registration_confirmed'));

        }
    }

    $layout = $extendLayout == true ? 'User/Registration/register.html.twig' : 'User/Registration/register_content.html.twig';

    return $this->render($layout, array(
            'form' => $form->createView(),
        ));
}

My login form is sent to the login_check route, which, in my opinion, is handled by symfony, and with the help of the user I create, the login fails. Why?

My login form:

<div class="fmu_panel fmu_login">

    <h3>Vous êtes inscrit</h3>

    <div class="panel-content">
        <form action="{{ path("login_check") }}" id="signup-form_id" method="post">
            <input type="hidden" name="_csrf_token" value="{{ csrf_token }}" />

            {% if error %}
                <div class="form-group">
                <div class="alert alert-danger" role="alert">
                    {#{{ error.messageKey|trans(error.messageData, 'security') }}#}
                    Mot de passe ou nom d'utilisateur incorrect.
                </div>
                </div>
            {% endif %}

            <div class="form-group w-icon">
                <input type="text" id="username" name="_username" value="{{ last_username }}" required="required" class="form-control" placeholder="E-mail ou pseudo" />
                <i class="fa fa-user"></i>
            </div> <!-- / Username -->

            <div class="form-group w-icon">
                <input type="password" id="password" name="_password" required="required" class="form-control" placeholder="Mot de passe"  />
                <i class="fa fa-lock"></i>
            </div> <!-- / Password -->

            <div class="form-group">
                <label for="_remember_me" class="checkbox-inline">
                    <input type="checkbox" name="_remember_me" id="remember_me" checked>
                    Se souvenir de moi
                </label>
            </div>

            <div class="form-actions">
                <input type="submit" value="S'identifier" class="btn btn-primary" name="_submit" id="fmu_signin">
                <a href="#" class="forgot-password btn btn-danger pull-right" id="fmu_forgot_password_link">Mot de passe oublié ?</a>
            </div>

{#
            <div class="social">
                <a href="{{ path('hwi') }}twitter" class="btn btn-default">Se connecter avec <span>Facebook</span></a>
            </div>
#}

        </form>

        <div id="fmu_password_reset">

            {{ render(controller('AppBundle:User/UserSecurity:requestNewPassword')) }}

        </div>
    </div>
</div>



<script>

// Show/Hide password reset form on click

$(function(){
    $('#fmu_forgot_password_link').on('click', function (e) {
        e.preventDefault();
        $('#fmu_password_reset').fadeIn(400);
        $('#signup-form_id').fadeOut(400);
    });
    $('#fmu_password_reset .close').click(function (e) {
        e.preventDefault();
        $('#fmu_password_reset').fadeOut(400);
        $('#signup-form_id').fadeIn(400);
    });

});
</script>
+4
source share
1 answer

: .


:

  • ( )
  • Symfony Security (, )
  • ()

security.yml :

# app/config/security.yml
security:
    encoders:
        AppBundle\Entity\User:
            algorithm: bcrypt

    # ...

    providers:
        our_db_provider:
            entity:
                class: AppBundle:User
                property: username
                # if you're using multiple entity managers
                # manager_name: customer

    firewalls:
        default:
            pattern:    ^/
            http_basic: ~
            provider: our_db_provider

    # ...

, .

0

Source: https://habr.com/ru/post/1570466/


All Articles