Last Login and Logout (View Based)

Hello folks! 

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. Create a new page with the option of not requiring login, in the object select the one created in step 1 and for the views that the new page should have, select form option.




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


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:


  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: '',

    data: {

      field_242: {            // Change field_XXX for Email field ID



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


      LOGIN = true;



  } else {

    if (LOGIN) {

      LOGIN = false;





// Logout actions

$(document).on('knack-scene-render.any', function (event, scene) {

  // Check is already authenticated

  if (!Knack.session.user) {



  $('.kn-log-out').on('click', function () {





Did you find this insightful?

Please let us know!

upvote! good job Soluntech

Thanks Soluntech, very useful, works like a charm.

nice add and something that surprised me that is missing in Knack. Thank you