Last_login not available in user object in custom rule

Hello!

This is because there would be a caveat: rules execute after a successful authentication, meaning that the last_login value would be the value for the current time and date.

What you could do is set the value to the app_metadata, and grab it from there. Your rule would look something like this:

    user.app_metadata = user.app_metadata || {};

    // Grab the current value of last login and print it
    var previous_login = user.app_metadata.previous_login
    console.log(previous_login)

    // Write the new value to the metadata
    user.app_metadata.previous_login = Date.now()

    // Persist the app_metadata update
    auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
        .then(function(){
            callback(null, user, context);
        })
        .catch(function(err){
            callback(err);
        });
}

You can also try implementing some logic before it prints to actually check that the value exists, and print “First login!” otherwise.

1 Like