# Redirect Users on Logout

**URL:** https://forums.knack.com/t/redirect-users-on-logout/11033
**Category:** Ask Developers
**Created:** [September 3, 2021, 12:03pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033 "2021-09-03T12:03:32Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://forums.knack.com/u/jason)
#### Post date: [September 3, 2021, 12:03pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/1 "2021-09-03T12:03:32Z")

</div>

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

```auto
$(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!

---

<div class="post-metadata">

### Author: ![Pieter](https://avatars.discourse-cdn.com/v4/letter/p/82dd89/32.png) [@Pieter](https://forums.knack.com/u/Pieter)
#### Post date: [September 13, 2021, 9:56am UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/2 "2021-09-13T09:56:57Z")

</div>

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

---

<div class="post-metadata">

### Author: ![GaryStark10384](https://avatars.discourse-cdn.com/v4/letter/g/e95f7d/32.png) [@GaryStark10384](https://forums.knack.com/u/GaryStark10384)
#### Post date: [October 21, 2021, 6:50pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/3 "2021-10-21T18:50:20Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Sunny\_Singla](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/sunny_singla/32/1007_2.png) [@Sunny\_Singla](https://forums.knack.com/u/Sunny_Singla)
#### Post date: [October 24, 2021, 4:55pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/4 "2021-10-24T16:55:26Z")

</div>

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’;  
});

---

<div class="post-metadata">

### Author: ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://forums.knack.com/u/jason)
#### Post date: [December 7, 2021, 1:36pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/5 "2021-12-07T13:36:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://forums.knack.com/u/jason)
#### Post date: [December 7, 2021, 1:36pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/6 "2021-12-07T13:36:52Z")

</div>

Sunny,

Does not look like that works.

---

<div class="post-metadata">

### Author: ![Sunny\_Singla](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/sunny_singla/32/1007_2.png) [@Sunny\_Singla](https://forums.knack.com/u/Sunny_Singla)
#### Post date: [December 7, 2021, 2:05pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/7 "2021-12-07T14:05:37Z")

</div>

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/](https://roberts.knack.com/farmers#home/)

Regards,  
Sunny Singla  
[ssingla1985@gmail.com](mailto:ssingla1985@gmail.com)  
+919855089359

---

<div class="post-metadata">

### Author: ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://forums.knack.com/u/jason)
#### Post date: [December 7, 2021, 3:05pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/8 "2021-12-07T15:05:02Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://forums.knack.com/u/jason)
#### Post date: [April 11, 2022, 12:13pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/9 "2022-04-11T12:13:44Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Sunny\_Singla](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/sunny_singla/32/1007_2.png) [@Sunny\_Singla](https://forums.knack.com/u/Sunny_Singla)
#### Post date: [April 11, 2022, 1:19pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/10 "2022-04-11T13:19:29Z")

</div>

Hello Jason,

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

Regards,  
Sunny Singla

---

<div class="post-metadata">

### Author: ![jason](https://avatars.discourse-cdn.com/v4/letter/j/dc4da7/32.png) [@jason](https://forums.knack.com/u/jason)
#### Post date: [April 11, 2022, 2:22pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/11 "2022-04-11T14:22:59Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Pablo1](https://avatars.discourse-cdn.com/v4/letter/p/958977/32.png) [@Pablo1](https://forums.knack.com/u/Pablo1)
#### Post date: [July 28, 2022, 10:22pm UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/12 "2022-07-28T22:22:37Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Sunny\_Singla](https://sea2.discourse-cdn.com/flex020/user_avatar/forums.knack.com/sunny_singla/32/1007_2.png) [@Sunny\_Singla](https://forums.knack.com/u/Sunny_Singla)
#### Post date: [July 29, 2022, 1:39am UTC](https://forums.knack.com/t/redirect-users-on-logout/11033/13 "2022-07-29T01:39:09Z")

</div>

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](mailto:info@sandt-consultancyservices.com)

> **[Database consultant – Software database consultant](http://sandt-consultancyservices.com/)**
>
> Knack Database consultant
