Journal

Articles and tips for Craft CMS and web development


How do I return to the current page in Craft CMS after logging in?

17 October 2019

Craft CMS 3 will not automatically direct the user back the page containing the login form after successfully logging in. To achieve this you need to add a hashed craft.app.request.absoluteUrl to a hidden redirect input. You could alternatively place another URL in the redirect field e.g. /blog.

<form method="post" accept-charset="UTF-8">
{{ csrfInput() }}
{{ actionInput('users/login') }}

    <label for="loginName">Username or email</label>
    <input id="loginName" type="text" name="loginName" 
    value="{{ craft.app.user.rememberedUsername }}" placeholder="Username or email" required>

    <label for="password">Password</label>
    <input id="password" type="password" name="password" placeholder="Password" required>

    <input type="submit" value="Login">

    {% if errorMessage is defined %}
       <p>{{ errorMessage }}</p>
    {% endif %}

    <input type="hidden" name="redirect" value="{{ craft.app.request.absoluteUrl|hash}}">
</form>