Redirect Users on Logout

Is there an easy way to redirect users back to a specific page upon logout? Right now, I use the following:

$(document).on("knack-view-render.view_****", function (event, view, data) { // Login Page to Redirect
  window.location.replace("**destination**"); // Destination Page
});

This works, but I have to have this full code for each and every login page. Is there another option I can use that will allow me to not have to repeat this over and over again?

Thanks!

I use this 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.scene_#’, 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 = “URL-GOES-HERE”;
}, 500);
})
});

I used Pieter’s solution until I noticed today that it’s not working.

try below

$(document).on(“knack-view-render.view_any”, function (event, view, data) { // Login Page to Redirect
if(Knack.getUserAttributes().id==undefined)
location.href=’#home’;
});

Thanks @Pieter, I’m sure that works, but that would be about the same as what I am using now. I was hoping there was a way to do this without having to have to repeat that code over and over again for every separate log in page.

Sunny,

Does not look like that works.

Hello Jason,

Try below code it’s works for me .

$(document).on(‘knack-view-render.any’, function (event, view, data) { // Login Page to Redirect

if(Knack.getUserAttributes().id==undefined && location.href.split("#")[1]!=“home” && location.href.split("#")[1]!=“home/”)

{

console.log(Knack.getUserAttributes().id);

location.href=’#home’;

}

});

working url
https://roberts.knack.com/farmers#home/

Regards,
Sunny Singla
ssingla1985@gmail.com
+919855089359

2 Likes

Sunny, Thank you! This works perfectly. I really appreciate it.

One note: I removed the “console.log(Knack.getUserAttributes().id);” part. It works just fine without it. I took it out because it writes “undefined” to the console log, which allows for an easy one-click to see the entire .js file.

Thanks again!

1 Like

Hi @Sunny_Singla. I just came across an unexpected, minor issue with this: it works a little too well… Is there any way to exclude a specific view? The issue we are now having is that the reset password view redirects as well. So, if someone needs their password reset, and I do that from the builder, they cannot change their password because the reset password page will redirect them back to home.

Any suggestions? It’s not the end of the world, I normally manually change their passwords and just let them change it after login, but I was curious if there was a way to exclude a view.

Thanks again. This really works well.

Hello Jason,

Just add if condition → if (view.key!=“view_xyz”){} then this code works in iff condition as well

Regards,
Sunny Singla

1 Like

Sunny,

I did not realize that the password reset page does not have a view number (at least not one that I can find). Since it does have a scene, I changed everything to knack-scene-render.any, and the view.key to scene.key and everything works great. Thank you again.

1 Like

Hi Sunny, it is not working for me due to i also have “public pages” and when i run the app to the public pages, it goes to the login.
I have tried with your suggestion excluding view but without success. ( if (view.key!=“view_xyz”){} )

Have you any suggestions for my case? or may be, could you show me exactly where should i include the “if” to exclude a view.

Thanks in advance.
Pablo.

Hello Pablo,

In this case, you need to add multiple scene IDs in the array (which are public pages). and check if that scene id does not exist and id= undefined. then redirect. just an extra check of public scenes ids. for any professional help, you can also email me

Regards,
Sunny Singla
info@sandt-consultancyservices.com