Craft CMS Front-End Registration
We review the code needed to create a login form for Craft CMS front-end members.
Now that we’re registered, let’s review the login form. We won’t code this one together since it’s a basic login form and we’re redoing some of what we’ve already done in the previous video on the registration form.
At the top, you’ll see we have the condtional to check for a currentUser
; if there is one then we redirect the user to the dashboard since they don’t need to be logged in. From there they can log out, if needed.
We’re also importing the errors macro and doing some error response checking and then displaying the errors.
{% import '_macros/forms' as forms %}
{% set user = user ?? null %}
{{ user ? forms.errorsList(user.getErrors('loginName')) }}
For this form we’re also just doing a standard POST request via the normal browser means.
{% if errorMessage is defined %}
<div class="rounded-lg bg-red-50 p-4 mt-4 border-2 border-red-100">
<div class="flex">
<div class="ml-3">
<p class="text-sm font-medium text-red-800">
{{ errorMessage }}
</p>
</div>
</div>
</div>
{% endif %}
After a successful login, we want to always redirect the logged-in user to the dashboard. We can enforce that in a setting rather than putting it in code. In the general.php
config file, let’s add:
'postLoginRedirect' => '/dashboard',
And now we’re ready to test our new form!
Craft CMS Front-End Registration is made up of the following videos: