Common Logout

Hi guys! I’m very new to Knack and would appreciate some help here.

The app has multiple roles ( multiple protected pages ), I’ve used a “General Login“ (/login) so that users can go through one login page and be redirected based on their role.

But when logging out, I’m sent back to the login page of that page (e.g /role-dashboard-login) rather than going back to the general login. Oh and I can’t find where to do custom JS (only CSS) and as comfortable as I am with JS, I’d love to keep it within Knack unless really necessary.

Thank you guys!

Hi @Sebastien_Duke - Welcome to the forum :waving_hand:

Straight off the bat - I’m not a coder :man_technologist:

What you’re running into depends on whether your app is built in Classic or the new Next Gen version. Given the fact that you’re missing the JavaScript section I think you’re on Next Gen.

  • Next Gen (the new React version): At the moment, only custom CSS is supported – no JavaScript yet. That means it isn’t possible right now to override the logout redirect to always go back to your General Login page.

  • Classic (the existing jQuery version): This is possible. With a little bit of custom JavaScript, you can catch the logout action and send users back to /login instead of the role-specific login page.

If you were on Classic, (according to my good friend, ChatGPT ), you could do something like this. I’m sure you know better than me.

$(document).on('knack-scene-render.any', function(event, scene) {
  $('a.kn-log-out').off('click').on('click', function(e) {
    e.preventDefault();
    Knack.logout();
    window.location.href = '/login'; // Always go back to General Login
  });
});

If you’re on Next Gen, this isn’t possible yet – Knack are currently working on introducing JavaScript.

Not a lot of help I’m afraid :thinking:

You may find this thread of interest.

1 Like

Hi @Sebastien_Duke

Much like what Carl has put above this is the code I have been using on Classic to get the redirect.

/* Change the scene_1 to the scene key of your choice, or replace with any to work on all pages. */
$(document).on('knack-scene-render.any', function(event, scene, data) {
  $("a.kn-log-out").on("click", function() {
    // Some browsers need some wait-time to finish the logout
    setTimeout(function(){
        // replace with your desired URL
        window.location = "https://yoursite.knack.com/*****/";
    }, 500);
  })
});
1 Like

Hi guys, thank you so much for the reply. Yep, I am using the Next Gen page builder. if any of you may know if there is a timeline to when JavaScript might be coming to knack?

No time line has been published, that I’ve seen. I’d imagine it will be over the next few months - maybe :man_shrugging:

1 Like