<p>There's no need for a plugin here. </p>

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

<p><code>Logged in.</code>  </p>

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

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

<pre><code>{% set message = craft.session.getFlash( 'notice' ) %}

{% if message == "Logged In." %}
    {# Do whatever #}
{% endif %}
</code></pre>

<p>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:</p>

<pre><code>{% if errorMessage is defined %}
    &lt;p&gt;{{ errorMessage }}&lt;/p&gt;
{% endif %}
</code></pre>

<p>This is documented here:
<a href="https://craftcms.com/docs/templating/login-form" rel="nofollow">https://craftcms.com/docs/templating/login-form</a></p>

CC BY-SA 3.0
2016-05-21T23:57:22.803Z
2016-05-23T01:54:04.907Z
2016-05-23T01:54:04.907Z
Jeremy Daalder
<p>You can achieve this using a plugin and the <a href="https://craftcms.com/classreference/services/UserSessionService" rel="nofollow">UserSessionService</a>.</p>

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

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

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

<pre><code>{% set message = craft.session.getFlash( 'message' ) %}

{% if message %}
  {{ message }}
{% endif %}
</code></pre>

<p>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 <a href="https://craftcms.com/docs/templating/login-form" rel="nofollow">docs</a> to see how this is handled.</p>

CC BY-SA 3.0
2016-05-21T22:49:42.710Z
Thank Luke Pearce for the answer. Much Appreciate
William Spark
CC BY-SA 3.0
2016-05-21T13:48:21.933Z
2016-05-22T07:10:37.620Z
2016-05-22T07:10:37.620Z
Luke Pearce
<p>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. </p>

<p>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?</p>

<templating>
How can I display a message after login?
# How can I display a message after login?

(Asked by: username_0 on 2016-05-21)

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?

**Tags:** templating


---

## 2 answers

### Answer by username_2 on 2016-05-21. Score: 6 | Accepted answer

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.

> **Comments:**
> **username_0**: Thank username_2 for the answer. Much Appreciate  

---

### Answer by username_1 on 2016-05-21. Score: 6

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 %}
    {{ errorMessage }}
{% endif %}

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


---

