When a user logs in, I want to display the message "Login Success" or an error message "Username or Password is incorrect" if a user enters the wrong details.

What is the best method for displaying these messages when a user logs in and logs out? I am not sure how to write the correct way using getFlash?

2 Answers 2


You can achieve this using a plugin and the UserSessionService.

craft()->on( 'userSession.onLogin', function( Event $event ){
  // Sets the flash message on log in
  craft()->userSession->setFlash( 'message', "You've been logged in" );

craft()->on( 'userSession.onLogout', function( Event $event ){
  // Sets the flash message on log out
  craft()->userSession->setFlash( 'message', "You've been logged out" );

And then in your template you can see of a flash message exists and print it out.

{% set message = craft.session.getFlash( 'message' ) %}

{% if message %}
  {{ message }}
{% endif %}

The message for the incorrect login details will be handled by Crafts login controller. Take a look at the example front end log in page in the docs to see how this is handled.

  • Thank Luke Pearce for the answer. Much Appreciate May 21, 2016 at 22:49

There's no need for a plugin here.

Craft sets a notice flash message by default on login/logout that you can check for:

Logged in.

(If there is an error it will return the error (and your form with field errors) to the page as well.

If you want to check explicitly for these messages anywhere else (e.g. where your login form redirects to) - you could:

{% set message = craft.session.getFlash( 'notice' ) %}

{% if message == "Logged In." %}
    {# Do whatever #}
{% endif %}

In terms of error handling, generally you will stay on the login page and if there was an error print that our under the form so they can try again. In simplest form, you just need to do this:

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

This is documented here: https://craftcms.com/docs/templating/login-form

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.