Retrieve User's Last Login Within an Action
Last Updated:
Overview
This article explains whether there is a way to retrieve the user's last login timestamp within an action.
Applies To
- Actions
- Last Login
Solution
The
last_login attribute is not made available via the event.user object. Given the post-login action executes after authentication, the last_login value would be updated right before the action is executed.It is necessary to set a custom property in
app_metadata to indicate the last login. This should be done at the end of the login action flow so that it does not overwrite the value if required by other actions. The property can be accessed earlier in the flow to determine the last login.Here is an example action that sets the
lastLogin value (in the user's app_metadata) to the current epoch time:exports.onExecutePostLogin = async (event, api) => {
api.user.setAppMetadata('lastLogin', Date.now());
}
On subsequent logins, it can be accessed via
event.user.app_metadata.lastLogin to determine the last time the user logged in.