# Last Login Time

**URL:** <https://forum.keycloak.org/t/last-login-time/2190>\
**Category:** Extending the server\
**Tags:** authentication\
**Created:** [April 16, 2020, 1:18pm UTC](https://forum.keycloak.org/t/last-login-time/2190 "2020-04-16T13:18:00Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![edmaputra](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/edmaputra/32/688_2.png) [@edmaputra](https://forum.keycloak.org/u/edmaputra)\
**Post date:** [April 16, 2020, 1:18pm UTC](https://forum.keycloak.org/t/last-login-time/2190/1 "2020-04-16T13:18:00Z")

</div>

Hi Everyone

I already create a custom event listener, it have a capability to listen to Login Event. So when a user login, this listener will persist into the user’s attribute the time his/her logged in.  
I also build a service use Keycloak Admin client library with Java to retrieve the user data.

The problem is, when i retrieve that user data via the service, it doesn’t show the latest data of the user-attribute.

Let’s say the user A logged in at 11:00 AM. The service show the 11:00 AM. But after user A login again at 11:30 AM, the service show still 11:00 AM.

Is there any configuration or is there a suggestion what best practices to persist this login time via my custom event listener ?

---

<div class="post-metadata">

**Author:** ![edmaputra](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/edmaputra/32/688_2.png) [@edmaputra](https://forum.keycloak.org/u/edmaputra)\
**Post date:** [April 17, 2020, 10:07am UTC](https://forum.keycloak.org/t/last-login-time/2190/2 "2020-04-17T10:07:37Z")

</div>

Already found the solution. User UserModel to update User’s data in database

---

<div class="post-metadata">

**Author:** ![alex88](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/alex88/32/325_2.png) [@alex88](https://forum.keycloak.org/u/alex88)\
**Post date:** [October 12, 2020, 3:32pm UTC](https://forum.keycloak.org/t/last-login-time/2190/3 "2020-10-12T15:32:50Z")

</div>

hi @edmaputra, could you please share how you did that? I’m looking into implementing the same thing

---

<div class="post-metadata">

**Author:** ![edmaputra](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/edmaputra/32/688_2.png) [@edmaputra](https://forum.keycloak.org/u/edmaputra)\
**Post date:** [October 20, 2020, 8:40am UTC](https://forum.keycloak.org/t/last-login-time/2190/4 "2020-10-20T08:40:55Z")

</div>

hi @alex88  
What I’m doing is like this

```auto
UserModel userModel = session.users().getUserById(id, realm);
userModel.setSingleAttribute(LAST_LOGIN_TIME_KEY, String.valueOf(time));

```

where session is KeycloakSession. In my case, i set the last login time value on User’s Attribute.

---

<div class="post-metadata">

**Author:** ![alex88](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/alex88/32/325_2.png) [@alex88](https://forum.keycloak.org/u/alex88)\
**Post date:** [October 20, 2020, 8:52am UTC](https://forum.keycloak.org/t/last-login-time/2190/5 "2020-10-20T08:52:50Z")

</div>

Thanks, I’ll look into how to create a custom event listener first

---

<div class="post-metadata">

**Author:** ![ThoreKr](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/thorekr/32/7713_2.png) [@ThoreKr](https://forum.keycloak.org/u/ThoreKr)\
**Post date:** [October 24, 2020, 6:27pm UTC](https://forum.keycloak.org/t/last-login-time/2190/6 "2020-10-24T18:27:11Z")

</div>

@alex88 @edmaputra do you see a chance to open source your implementation?

---

<div class="post-metadata">

**Author:** ![alex88](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/alex88/32/325_2.png) [@alex88](https://forum.keycloak.org/u/alex88)\
**Post date:** [January 12, 2021, 4:41pm UTC](https://forum.keycloak.org/t/last-login-time/2190/7 "2021-01-12T16:41:23Z")

</div>

Unfortunately I’ve never worked on it as there isn’t a very clear documentation on the subject

---

<div class="post-metadata">

**Author:** ![ThoreKr](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/thorekr/32/7713_2.png) [@ThoreKr](https://forum.keycloak.org/u/ThoreKr)\
**Post date:** [January 12, 2021, 5:51pm UTC](https://forum.keycloak.org/t/last-login-time/2190/8 "2021-01-12T17:51:11Z")

</div>

In the meantime I’ve implemented this: [GitHub - ThoreKr/keycloak-last-login-event-listener: Automatically sets a last-login attribute on a user.](https://github.com/ThoreKr/keycloak-last-login-event-listener)

It updates also on token refresh which one might want to prevent, but in general it works sufficiently well.

---

<div class="post-metadata">

**Author:** ![alex88](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/alex88/32/325_2.png) [@alex88](https://forum.keycloak.org/u/alex88)\
**Post date:** [January 12, 2021, 5:53pm UTC](https://forum.keycloak.org/t/last-login-time/2190/9 "2021-01-12T17:53:05Z")

</div>

Oh awesome thank you! I’ll check it out!

---

<div class="post-metadata">

**Author:** ![alex88](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/alex88/32/325_2.png) [@alex88](https://forum.keycloak.org/u/alex88)\
**Post date:** [January 13, 2021, 9:09am UTC](https://forum.keycloak.org/t/last-login-time/2190/10 "2021-01-13T09:09:59Z")

</div>

Thank you so much that helped me get what I wanted!

Does anyone know if it’s possible to have a listener “enabled” only for some realms? So I don’t update user attributes on all realms but only where I need to?  
I know I can get the user realm from the sessions but I don’t want to hardcode it into the library

Update: Nvm just saw that you need to manually enable the event listener in the realm, neat!

---

<div class="post-metadata">

**Author:** ![pkadian](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/pkadian/32/1354_2.png) [@pkadian](https://forum.keycloak.org/u/pkadian)\
**Post date:** [January 13, 2021, 11:25am UTC](https://forum.keycloak.org/t/last-login-time/2190/11 "2021-01-13T11:25:20Z")

</div>

@Alex  
Enable it on Realm level. In the left side menu, there are events options there you can add your custom event and enable it for your realm only.

---

<div class="post-metadata">

**Author:** ![alex88](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/alex88/32/325_2.png) [@alex88](https://forum.keycloak.org/u/alex88)\
**Post date:** [January 13, 2021, 1:08pm UTC](https://forum.keycloak.org/t/last-login-time/2190/12 "2021-01-13T13:08:40Z")

</div>

thanks I saw that after seeing that the listener wasn’t being notified 🙂

---

<div class="post-metadata">

**Author:** ![pkadian](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/pkadian/32/1354_2.png) [@pkadian](https://forum.keycloak.org/u/pkadian)\
**Post date:** [January 13, 2021, 7:15pm UTC](https://forum.keycloak.org/t/last-login-time/2190/13 "2021-01-13T19:15:44Z")

</div>

@Alex,

This is another repository from there you can get an idea for Keycloak event listener.

> **[thomasdarimont/keycloak-extension-playground](https://github.com/thomasdarimont/keycloak-extension-playground)**
>
> Simple project environment for creating custom Keycloak extensions

---

<div class="post-metadata">

**Author:** ![alex88](https://yyz2.discourse-cdn.com/free1/user_avatar/forum.keycloak.org/alex88/32/325_2.png) [@alex88](https://forum.keycloak.org/u/alex88)\
**Post date:** [January 14, 2021, 10:00am UTC](https://forum.keycloak.org/t/last-login-time/2190/14 "2021-01-14T10:00:27Z")

</div>

oh nice! plenty of extensions to look at!
