Last Login and Logout (Object Based)

Want to know the last time a user logged in to your Knack app?
This feature is usually implemented to keep track of the users who use the application, especially the period of time in which a session lasts.
Knack applications allow you to implement authentication forms simply and quickly, however when establishing a control or audit on the logins or log outs that are constantly running within the application, you must resort to the help of JavaScript for that end. Below we list a series of steps that will serve as a guide for the implementation of these logs:
1. Create a new object in the database: Although the object can have any number of fields, it is recommended that it have at least three fields: Email, Date / Time and Action (Multiple Choice = Login or Logout).
2. Add the following lines of code within the API & Code section:
var LOGIN_PAGE = false; var LOGIN = false;
// Set Knack Info for your app
var KNACK_HEADERS = {
'X-Knack-Application-ID': 'XXXXXXXXXXXXXXXXXXXX',
'X-Knack-REST-API-Key': 'YYYYYYYYYYYYYYYYYYYYY'
};
// Change object_XX for Login Log object ID
var objectId = 'object_XX';
// Login actions
$(document).on('knack-scene-render.any', function (event, scene) {
LOGIN_PAGE = ($('.kn-login').length == 1) ? true : false;
if (LOGIN_PAGE) {
LOGIN = false;
$('input[type="submit"]').click(function (e) {
e.preventDefault();
LOGIN = true;
$('form').submit();
});
} else {
if (LOGIN) {
LOGIN = false;
var user = Knack.session.user;
// Create record log
$.ajax({
type: 'POST',
headers: KNACK_HEADERS,
url: Knack.api_url + '/v1/objects/' + objectId + '/records',
data: {
field_XXX: user.email, // Change field_XXX for Email field ID
field_YYY: 'Login' // Change field_YYY for Action field ID
}
});
}
}
});
// Logout actions
$(document).on('knack-scene-render.any', function (event, scene) {
// Check is already authenticated
if (!Knack.session.user) {
return;
}
$('.kn-log-out').on('click', function () {
var user = Knack.session.user;
// Create record log
$.ajax({
type: 'POST',
headers: KNACK_HEADERS,
url: Knack.api_url + '/v1/objects/' + objectId + '/records',
data: {
field_XXX: user.email, // Change field_XXX for Email field ID
field_YYY: 'Logout' // Change field_YYY for Action field ID
}
});
});
});
And on this object you can use different native Knack components such as tables, reports or graphs.
We’re sure this will come in handy on all your projects.
Did you like it?
Please let us know!
Soluntech Team
AI-Native Engineering Firm
Soluntech's team writes about software, AI systems, validation, and operational technology from the perspective of disciplined engineering and long-term system ownership.
Continue with related insights.
Explore more thinking connected to this topic, from software decisions to AI systems and operational execution.

Decision Intelligence: Enhancing Business Strategy with AI
Decision Intelligence: Enhancing Business Strategy with AI
Soluntech Team
AI-Native Engineering Firm

Conquer the Challenge of Product-Market Fit as a Founder
Product Market Fit Flow Chart
Soluntech Team
AI-Native Engineering Firm
The Metaverse: It Doesn’t Exist Yet, but Companies Are Already Doing Business in It
Photo by Michelangelo Buonarroti from Pexels
Soluntech Team
AI-Native Engineering Firm