Previous employee still logged in by shared email

How can I track which user is currently logged in or recently logged in, even if multiple people share the same email like info@?

Hi @Sam2 - welcome to the forum :waving_hand:

Knack doesn’t currently offer a built-in way to see who is actively logged in or online at any given moment. You can track when a user logs in (for example, by updating a timestamp), but that only gives you a historical view, not real-time presence.

If you need to know which user is currently logged in or recently active, especially when multiple people share the same login (like info@), you’ll likely need to use custom JavaScript. This could include writing data to hidden fields or triggering actions when pages load, but it’s not something that can be done with Knack’s built-in features alone.

I’m not a coder myself, but I know this kind of tracking is possible with the right scripting support, so it may be something to explore with a developer or someone comfortable working with JavaScript.

Thanks!
how can i update a timestamp?
and can it give me backdated info?

Hi @Sam2, as Carl says, there’s no native way to achieve this, but you can set up a simple JavaScript function to do the following:

When a user logs in, or refreshes their browser page, update a logged-in account’s record with the current date/time, and append a connected ‘login history’ record with the account and current date/time if you want to keep a history.

Prerequisites:

  1. A basic understanding of JavaScript
  2. A basic understanding of view-based API requests
  3. A Last login date/time field in your parent Accounts table
  4. A Login history table connected to Accounts and with a Login date/time field

Solution:

  1. Insert an ‘update logged-in Account form on a hidden page. It should have record actions with the following set up.
    Take note of the scene key and view key for this form view (e.g. scene_xx, view_xx).
    Keep in mind that if you want to add a record for every time a user logs in, you may consume a lot of records, so you can remove Record Rule #2 if you don’t need it.

  2. Insert the following code in your JavaScript editor, and replace scene_xx and view_xx with your form’s scene and view key.

let loginRecorded = false;

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

  const accountId = Knack.getUserAttributes().id;
  console.log(accountId);

  if (!accountId) { // Reset loginRecorded if not logged in and do not continue
    loginRecorded = false; 
    return;
  }

  if (loginRecorded) return; // Do not continue if login has already been recorded

  fetch(`https://api.knack.com/v1/pages/scene_xx/views/view_xx/records/${accountId}`, {
    method: 'PUT',
    headers: {
      'X-Knack-Application-Id': Knack.application_id,
      'Authorization': Knack.getUserToken(),
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({})
  });
  loginRecorded = true;
  
});

Hope that helps!

1 Like