Last Login and Logout (View Based)

 
A person looking at a computer and writing in a notebook
 
 

Being able to track login and logout times allows businesses to leverage data more efficiently. Whether you’re monitoring employee performance or accruing customer data to improve sales strategies, more data allows you to make informed business decisions based on concrete facts. 

This tracking feature is usually implemented to keep track of the users who use the application and the period of time for 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 logouts that are constantly running within the application, you must use JavaScript.

Below we list a series of steps that will serve as a guide for the implementation of these logs so you can effectively track login and logout times:

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).

 
 
creating a new object in knack screenshot demo
 
 

2. Create a new page with the option of not requiring a login. In the object, select the one created in step 1, and for the views that the new page should have, select the form option.

 
 
new page creation on knack demo screenshot
 
 
select object on knack demo screenshot
 
 
select view on knack demo screenshot
 
 

3. Once the new page is created, it must be indicated that it’s not visible in the application menu. This is done in the settings tab.

 
 
page menu settings on knack demo screenshot
 
 

4. Add the following lines of code within the API & Code section:

 
 
var LOGIN_PAGE = false;
var LOGIN = false;
// Helper functions
function createRecordLog(action) {
  // All view-based requests are accessed through a scene key and view key and
  // use a URL in the following format:
  // https://api.knack.com/v1/pages/scene_key/views/view_key/records
  var user = Knack.session.user;
  return $.ajax({
    type: 'POST',
    headers: {
      // Set your Knack application Id 
      'X-Knack-Application-Id': 'XXXXXXXXXXXXXXXXXXXXXXXXX',
      'Authorization': Knack.getUserToken()
    },
    // Change scene_XXX and view_XXX for the scene and view IDs that were created
    url: 'https://api.knack.com/v1/pages/scene_XXX/views/view_XXX/records',
    data: {
      field_242: {            // Change field_XXX for Email field ID
        email: user.email
      },  
      field_389: action       // Change field_YYY for Action field ID 
    }
  });
}
// Knack events
// 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;
      createRecordLog('Login');
    }
  }
});
// 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 () {
    createRecordLog('Logout');
  });
});

 
 
 
 

Low Code Development Saves Time and Money

We hope this guide on how to track login and logout times has been insightful! Here at Soluntech, we employ Knack and other low code development solutions to maximize the reach of both your budget and our time. Rest assured we never compromise the integrity of the project, but we will use existing tools whenever possible. This allows us to work 10x faster than other developers while also saving your organization a significant amount of money.

Start Your Project Today

Our goal is to form meaningful, long-term relationships with our clients. That starts with a complimentary pre-consultation that allows both parties to ensure that we’re an ideal fit. Whether you need help implementing features to track login and logout times or brand new, custom software developed from scratch, you can depend on us to move forward with urgency and unparalleled skill. We encourage you to view our case studies and our blog to get a feel for our experience. Contact us now so we can turn your vision into a reality.