How to change the authentication error message?

When the user attempts to login with wrong username/password an error message “Invalid credentials” appear!

How can I change the getLastAuthenticationError() error message !

For example!
instead of “Invalid credentials” I want it to be “error message 123”

Code of the controlleur :

/**
 * @Route("/login",name="security_login")
 */
public function login(AuthenticationUtils  $authenticationUtils)
{
     // get the login error if there is one
$error = $authenticationUtils->getLastAuthenticationError();


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

    return $this->render('123/login.html.twig',[
        'lastUsername'=>$lastUsername,
        'error' => $error]);
}

Code of security.yaml

security:
    encoders:
        AppEntityUser:
            algorithm: bcrypt

    # https://symfony.com/doc/current/security.html#where-do-users-come-from-user-providers
    providers:
        in_memory: { memory: null }
        in_database:
            entity: 
                class: AppEntityUser  
                property: username
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: lazy
            provider: in_database
            form_login: 
                login_path: security_login
                check_path: security_login

            logout:
                path:   security_logout
                target: user_index

code of the view :

{% if error %}
    <div>{{ error.messageKey|trans(error.messageData, 'security') }}</div>
{% endif %}

<form action="{{ path('security_login') }}" method="post">
    <div class="form-group">
        <input placeholder="username" requied name="_username" value ="{{lastUsername}}"
         type="text" class="form-control">
    </div>
    <div class="form-group">
        <input placeholder="Mode de passe..." requied name="_password"
         type="password" class="form-control">
    </div>
     <div class="form-group">
       <button type="submit" class="btn btn-success">Connexion</button>
    </div>
</form>

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *