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

![](upload://4qv17WOpYohRWWPlVVBV6KYdaFa.png)

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.

![](upload://tpTqVvwWcJsGNmaneU7CqVDKJOd.png)

![](upload://uOO4CPwefGMEkboO2uOUKr9Ztzr.png)


![](upload://2s0BmYABTlMcMmdvEgA1TeKUv6d.png)

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.

![](upload://v5CnAkhxTRYLKjVqlxGgJNTYcP.png)

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');

  });

});

![](upload://iGvB1Fr3uUkdUf0ClGU3nzqGS9k.png)

Did you find this insightful?

Please let us know!

Thanks Soluntech, very useful, works like a charm.

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

Does anyone know how to make his work using Single Sign On?
As the submit isn’t happening on the page (the SSO button redirects instead), the createRecordLog code never executes.
Any thoughts or suggestions? I’m not a coder, just good at using copy and paste!