简体   繁体   中英

Login with Symfony3

My security.yml

# https://symfony.com/doc/current/security.html#b-configuring-how-users-are-loaded
providers:
    my_entity_provider:
                entity:
                    class:              AppBundle:User
                    property:           email

firewalls:
    # disables authentication for assets and the profiler, adapt it according to your needs
    dev:
        pattern: ^/(_(profiler|wdt)|css|images|js)/
        security: false

    main:
        provider: my_entity_provider
        anonymous: ~
        form_login:
          username_parameter: _email
          login_path: login
          check_path: login
        # activate different ways to authenticate

        # https://symfony.com/doc/current/security.html#a-configuring-how-your-users-will-authenticate
        #http_basic: ~

        # https://symfony.com/doc/current/security/form_login_setup.html
        #form_login: ~

My controller:

class SecurityController extends Controller{
/**
 * @Route("/login", name="login")
 */
public function loginAction(Request $request, AuthenticationUtils $authUtils)
{
    // get the login error if there is one
    $error = $authUtils->getLastAuthenticationError();

    // last username entered by the user
    $lastUsername = $authUtils->getLastUsername();

    return $this->render('AppBundle:Security:login.html.twig', array(
        'last_username' => $lastUsername,
        'error'         => $error,
    ));
}

My twig:

{% if error %}
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}
<form action="{{ path('login') }}" method="post">
    <label for="email">Email:</label>
    <input type="text" id="username" name="_username" value="{{ last_username }}" />
    <label for="password">Password:</label>
    <input type="password" id="password" name="_password" />
    <button type="submit">login</button>
</form>

So i try to login with email and password but i always get bad credentils. Can some one tell me what am i doing wrong?

your username_parameter doesnt correspond with your input field of your twig.

check also https://symfony.com/doc/current/reference/configuration/security.html#username-parameter

如果您遵循此文档检查,则您也在查询中使用电子邮件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM