Skip to content

Commit cf2ba77

Browse files
committed
5.6: Login/2FA templates, additional account lifecycle info
1 parent 69b2e80 commit cf2ba77

File tree

2 files changed

+155
-1
lines changed

2 files changed

+155
-1
lines changed

docs/5.x/images/login.png

89 KB
Loading

docs/5.x/system/user-management.md

Lines changed: 155 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ related:
88

99
# User Management
1010

11-
[Users](../reference/element-types/users.md) in Craft represent humans that have some relationship with your site or application. They may be control panel users, member accounts, or records that represent people in general. Users implicitly have the ability to create passwords and log in, but must be granted [permissions](#permissions) or added to [groups](#user-groups) to access the control panel or manage content.
11+
[Users](../reference/element-types/users.md) in Craft represent humans that have some relationship with your site or application. They may be control panel users, member accounts, or records that represent people in general. Users implicitly have the ability to create passwords and [log in](#logging-in), but must be granted [permissions](#permissions) or added to [groups](#user-groups) to access the control panel or manage content.
1212

1313
The first user account is created during [installation](../install.md). The number of additional users that can be created (and their capabilities) depends on your Craft [edition](../editions.md):
1414

@@ -87,6 +87,8 @@ In addition, the [users field layout](../reference/element-types/users.md#custom
8787

8888
A combination of [user-specific](../reference/config/general.md#users), [session](../reference/config/general.md#session), and [security](../reference/config/general.md#security) settings are tracked via the [General](../reference/config/general.md) configuration file.
8989

90+
A summary of these settings (as they relate to front-end development) is available in the [Additional Tools](#additional-tools) section, below.
91+
9092
## Admin Accounts
9193

9294
Admin users are special accounts that can do _everything_ within Craft, including some things that don’t have explicit permissions:
@@ -288,6 +290,10 @@ The control panel may require users to reauthorize to perform some actions, like
288290

289291
An elevated session’s duration is governed by the <config5:elevatedSessionDuration> setting.
290292

293+
### Front-end Multi-factor Authentication <Since ver="5.6.0" feature="Multi-factor auth support outside the control panel" />
294+
295+
When **Require two-step verification** is enabled for user groups that _don’t_ have control panel access, those users may be sent to the Craft-provided [front-end login page]() to perform additional authentication.
296+
291297
<a name="public-registration"></a>
292298

293299
## Public Registration <badge type="edition" title="Craft Pro only">Pro</badge>
@@ -312,6 +318,154 @@ Users created via public registration are automatically added to the group desig
312318
Select this group’s [permissions](#permissions) carefully, ensuring that new users don’t immediately get access to tools that can negatively affect other users’ experience.
313319
:::
314320

321+
## Logging In
322+
323+
Any [credentialed](#statuses) user can set a password and log in.
324+
325+
### Control Panel
326+
327+
Users with [control panel](control-panel.md) access should use Craft’s native login form, which is shown when visiting `/admin` (or the path corresponding to your <config5:cpTrigger>). Any time an unauthenticated client tries to access a resource in the control panel, they will be redirected to the login page.
328+
329+
After logging in (and, when configured, completing [two-step verification](#authentication)), they will be returned to the [dashboard](control-panel.md#dashboard), or whichever protected page they originally attempted to access.
330+
331+
::: tip
332+
When a control panel user authenticates and is redirected, their permissions _may_ still prevent them from viewing that resource.
333+
:::
334+
335+
### Front-end <badge type="edition" title="Craft Pro only">Pro</badge>
336+
337+
Sites that support [public registration](#public-registration) or whose administrators create accounts without control panel access are apt to require front-end login forms.
338+
339+
The <config5:loginPath> setting determines where Craft sends users who access pages guarded by the [`{% requireLogin %}`](../reference/twig/tags.md#requirelogin) or [`{% requirePermission %}`](../reference/twig/tags.md#requirepermission) tags, as well as the value of the global [`loginUrl` variable](../reference/twig/global-variables.md#loginurl):
340+
341+
```twig{9}
342+
<header>
343+
<img src="{{ siteUrl('assets/logo.png') }}" alt="{{ siteName }}">
344+
345+
{% if currentUser %}
346+
Welcome, {{ currentUser.fullName ?: currentUser.username }}!
347+
<a href="{{ siteUrl('my-account') }}">My Account</a>
348+
<a href="{{ logoutUrl }}">Sign out</a>
349+
{% else %}
350+
<a href="{{ loginUrl }}">Sign in</a>
351+
{% endif %}
352+
</header>
353+
```
354+
355+
By default, this path is `login`, so `{{ loginUrl }}` would print something like `https://my-project.ddev.site/login`.
356+
357+
When this route is requested, Craft looks for the [corresponding template](#custom-template), `templates/login.twig`. If it can’t be found, a bare-bones internal version will be used: <Since ver="5.6.0" feature="Built-in front-end login template" />
358+
359+
<BrowserShot url="https://my-project.ddev.site/login" :link="false" caption="Logging in to the front end of a Craft website, for which a login template does not exist.">
360+
<img src="../images/login.png" alt="The login template included with Craft" />
361+
</BrowserShot>
362+
363+
You can inject additional styles into this page using the <config5:systemTemplateCss> setting:
364+
365+
```php
366+
use craft\config\GeneralConfig;
367+
368+
return GeneralConfig::create()
369+
// ...
370+
->systemTemplateCss('/assets/styles/system.css')
371+
;
372+
```
373+
374+
Full reference for the CSS variables used on this page are [available in the source](https://github.com/craftcms/cms/blob/5.x/src/web/assets/theme/dist/fe.css). <Since ver="5.6.0" feature="System template stylesheet setting" />
375+
376+
::: tip
377+
This template will also be displayed when users are required to set up a new [two-step verification](#authentication) method (or complete authorization, later).
378+
:::
379+
380+
No template is rendered for requests matching the <config5:logoutPath>; the user’s session is terminated, and they are redirected to the <config5:postLogoutRedirect>.
381+
382+
### Custom Template
383+
384+
Create a new Twig file at `templates/login.twig` (or `templates/{loginPath}.twig`, if you have a custom <config5:loginPath>) with the following content:
385+
386+
```twig
387+
<form method="post">
388+
{{ csrfInput() }}
389+
{{ actionInput('users/login') }}
390+
391+
<label for="login-name">Username/Email</label>
392+
<input type="text" name="loginName" id="login-name">
393+
394+
<label for="password">Password</label>
395+
<input type="password" name="password" id="password">
396+
397+
<button>Sign in</button>
398+
</form>
399+
```
400+
401+
You are free to have this template extend an existing layout, or directly add `<head>` and `<body>` tags.
402+
403+
Visiting your site’s `/login` page in a _private_ browser, you should see the unstyled form. Opening the same page in a browser with an _active_ session may redirect you to the configured <config5:postLoginRedirect> (or the primary site’s homepage, by default).
404+
405+
<See path="../development/forms.md" label="Working with Forms" description="Learn about sending data to Craft with forms." />
406+
407+
::: tip
408+
Additional information about handling login and account creation can be found in our [Front-End User Accounts](kb:front-end-user-accounts) guide.
409+
:::
410+
411+
Any valid username and email combination can be submitted to log in. As mentioned above, if the user needs to set up or authenticate with a two-step verification method, they will be taken to the “system” login view to complete their login.
412+
413+
To help users understand the state of their session (and in some limited circumstances, specific issues with the submitted information), you can output [flashes](../development/forms.md#flashes) from the login action:
414+
415+
```twig
416+
{% set flashes = craft.app.session.getAllFlashes(true) %}
417+
418+
{% if flashes is not empty %}
419+
<ul>
420+
{% for flash in flashes %}
421+
<li>{{ flash }}</li>
422+
{% endfor %}
423+
</ul>
424+
{% endif %}
425+
```
426+
427+
### Custom Routes
428+
429+
The `loginUrl` Twig variable is set based on the corresponding config value. How this route is handled is actually up to your project! When the `loginUrl` path is requested by a client, Craft uses its normal [route resolution process](routing.md), including the normal element URI search, `routes.php` rule matching, and looking in your project’s `templates/` directory.
430+
431+
This means that you can decouple the route from the template by adding a [rule](routing.md) to your `routes.php` file:
432+
433+
```php
434+
return [
435+
// Assuming your `general.php` config sets `loginPath` to `account/sign-in`...
436+
'account/sign-in' => ['template' => '_accounts/login'],
437+
];
438+
```
439+
440+
Then, you would be free to create `templates/_accounts/login.twig`, alongside other templates in a [hidden](../development/templates.md#hidden-templates) directory.
441+
442+
### Additional Tools
443+
444+
These variables are available in all Twig contexts:
445+
446+
- `loginUrl` — Defined by <config5:loginPath>. Used to build a link to the central login page, and where users will be redirected when requesting a protected resource.
447+
- `logoutUrl` — Defined by <config5:logoutPath>. Used to build a link that immediately logs the user out.
448+
- `setPasswordUrl` — Defined by <config5:setPasswordRequestPath>. Used to build a link to a central page where users can request their password be reset. An link is sent to the user’s email address (based on the <config5:setPasswordPath> setting) with `id` and `code` query parameters.
449+
450+
Other URLs are generated when activating accounts or resetting passwords, and are not available for direct use in templates.
451+
452+
Fine-grained control over registration and sign-in workflows are possible with these settings:
453+
454+
- Logging in and out…
455+
- <config5:loginPath> — Determines the `loginUrl` variable. (Default: `login`)
456+
- <config5:logoutPath> — Determines the `logoutUrl` variable, and maps the specified path to the `users/logout` controller action. (Default: `logout`)
457+
- Account activation…
458+
- <config5:setPasswordRequestPath> — Determines the `setPasswordPath` variable. (Default: `setpassword`) Users will visit this page to _request_ a password reset link.
459+
- <config5:setPasswordPath> — Where users will be sent to _set_ a password. These secure, temporary URLs are generated by Craft when sending a password reset email, or when another user with the _Administrate users_ permission requests a password reset link via the control panel.
460+
- <config5:setPasswordSuccessPath> — Where users are redirected after successfully setting a new password.
461+
- Changing and verifying email addresses…
462+
- <config5:verifyEmailPath> — Used when generating a link sent via email to verify access to an email address.
463+
- <config5:verifyEmailSuccessPath> — Similar to `setPasswordSuccessPath`, but for redirection after a user verifies their email address (either upon creating an account, or changing its email address).
464+
- Security and timing…
465+
- <config5:autoLoginAfterAccountActivation> — Control whether users are immediately logged in after setting a password. (Default: `false`)
466+
- <config5:purgePendingUsersDuration> — How long Craft waits before deleting pending, non-activated users. (Default: `0`, or _disabled_)
467+
- <config5:purgeStaleUserSessionDuration> — How long Craft waits before dropping stale sessions from the `sessions` database table. This may (Default: 90 days)
468+
315469
## CLI
316470

317471
Craft’s [command line](cli.md) provides admin-level user management tools. With access to the underlying server, you can create, delete, and impersonate users, get activation URLs, set passwords, and even log out all users.

0 commit comments

Comments
 (0)