Skip to content

Create login form

You can create a login form for your users. Follow the instruction below to create a template with login form. If you want to configure more options for example, password expiration, see other user management templates.

First, make sure you have configured login methods.

If you only want to change a template, in the config/packages/views.yaml add the following configuration:

1
2
3
4
5
ezpublish:
    system:
        my_siteaccess:
            user:
                login_template: '@ezdesign/Security/login.html.twig'

To add a link redirecting to the login form, in the page layout template, provide the following code:

1
<a href="{{ path('login') }}">Log in</a>

Next, add the template defined in the event.

In templates/themes/<theme_name>/login, create an expired_credentials.html.twig file:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{% extends '@ezdesign/Security/base.html.twig' %}

{%- block content -%}
    <h2 class="ez-login__header">
        {{ 'authentication.credentials_expired'|trans|desc('Your password has expired') }}
    </h2>
    <p>
        {{ 'authentication.credentials_expired.message'|trans|desc(
            'For security reasons, your password has expired and needs to be changed. An email has been sent to you with instructions.'
        ) }}
    </p>
    <p>
        <a href="{{ path('ezplatform.user.forgot_password') }}" class="btn btn-primary ez-btn ez-btn--login">
            {{ 'authentication.credentials_expired.reset_password'|trans|desc('Reset password') }}
        </a>
    </p>
{%- endblock -%}

Customize login form

You can use a custom template for example to display information about password expiration or to customize other user management templates.

In case of more advanced template customization, you can use a subscriber, for example in src/EventSubscriber/LoginFormViewSubscriber.php:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<?php

declare(strict_types=1);

namespace App\EventSubscriber;

use eZ\Publish\Core\MVC\Symfony\Event\PreContentViewEvent;
use eZ\Publish\Core\MVC\Symfony\MVCEvents;
use eZ\Publish\Core\MVC\Symfony\View\LoginFormView;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\Security\Core\Exception\CredentialsExpiredException;

final class LoginFormViewSubscriber implements EventSubscriberInterface
{
    /**
     * Returns an array of events this subscriber wants to listen to.
     *
     * @return string[]
     */
    public static function getSubscribedEvents(): array
    {
        return [
            MVCEvents::PRE_CONTENT_VIEW => 'onPreContentView',
        ];
    }

    public function onPreContentView(PreContentViewEvent $event): void
    {
        $view = $event->getContentView();

        if (!($view instanceof LoginFormView)) {
            return ;
        }

        $view->addParameters([
            'foo' => 'foo',
            'bar' => 'bar'
        ]);

        if ($view->getLastAuthenticationException() instanceof CredentialsExpiredException) {
            // View with instruction to unlock account
            $view->setTemplateIdentifier('login/expired_credentials.html.twig');
        }
    }
}

In the provided example, in line 23, the PRE_CONTENT_VIEW event is used. You can also pass additional parameters to the view (line 35). In this case, at the instance of exception (line 40), the subscriber displays the expired_credentials.html.twig template (line 42).

Remember to provide a template and point to it in the subscriber (in this case, in templates/login/expired_credentials.html.twig):

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
{% extends '@ezdesign/Security/base.html.twig' %}

{%- block content -%}
    <h2 class="ez-login__header">
        {{ 'authentication.credentials_expired'|trans|desc('Your password has expired') }}
    </h2>
    <p>
        {{ 'authentication.credentials_expired.message'|trans|desc(
            'For security reasons, your password has expired and needs to be changed. An email has been sent to you with instructions.'
        ) }}
    </p>
    <p>
        <a href="{{ path('ezplatform.user.forgot_password') }}" class="btn btn-primary ez-btn ez-btn--login">
            {{ 'authentication.credentials_expired.reset_password'|trans|desc('Reset password') }}
        </a>
    </p>
{%- endblock -%}

For more information, see Templates documentation.