# wixUsers.logout on protected page issue

**URL:** <https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441>\
**Category:** Ask the community\
**Tags:** question, code\
**Created:** [December 11, 2017, 11:37pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441 "2017-12-11T23:37:02Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![universinformatiquei](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@universinformatiquei](https://forum.wixstudio.com/u/universinformatiquei)\
**Post date:** [December 11, 2017, 11:37pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/1 "2017-12-11T23:37:02Z")

</div>

I have a website that has protected page. Everything is working fine except the Logout button.

My code is in PAGE section as the logout button is in 1 page only. My code is simple

import wixUsers from ‘wix-users’;  
export function Logout\_onclick() {  
wixUsers.logout();  
}

The issue I have is that my logout button is located in a protected page so when I click on it, it bring me back right away to the login page from Wix. This is an issue for me. I need a code sample where when I click on the logout button, it do a logout and bring me right away to the home screen without going to the Login page from Wix. (Now I have to click on the X to close the Login page to go back to the home page)

Can someone help me 🙂

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/system/32/86885_2.png) [@system](https://forum.wixstudio.com/u/system)\
**Post date:** [December 12, 2017, 10:03am UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/2 "2017-12-12T10:03:00Z")

</div>

Hi 🙂

‘wixUsers.logout()’ return a [Promise](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise) that is fulfilled when the user is logged out.  
Try using it to navigate outside with [wixLocation.to()](https://www.wix.com/code/reference/wix-location.html#to) . Example:

```javascript
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
export function Logout_onclick() {
    wixUsers.logout().then(() => {
        wixLoaction.to('/home');
    });
}

```

Hope this helps,  
Liran.

---

<div class="post-metadata">

**Author:** ![universinformatiquei](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@universinformatiquei](https://forum.wixstudio.com/u/universinformatiquei)\
**Post date:** [December 12, 2017, 12:43pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/3 "2017-12-12T12:43:17Z")

</div>

It’s not working…The problem is that the line WIXLOCATION is execute only when the logout window is closed. You still need to press X to close the login box.  
By default, it bring you back to the home page so I even don’t have to put the wixlocation.to if I don’t want to. I think I need to find a solution that when I press the Logout button, the first thing it does is redirect me to another page like the home page and then do the logout. But I don’t know how to do that.

Any clue ?

thanks 🙂

---

<div class="post-metadata">

**Author:** ![admin-23](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/admin-23/32/708_2.png) [@admin-23](https://forum.wixstudio.com/u/admin-23)\
**Post date:** [December 13, 2017, 12:37pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/4 "2017-12-13T12:37:13Z")

</div>

Hi,

As an alternative you can also create a dedicated logout page with the logout & navigation functions and simply move the user to this page once he clicks the logout button on the secured page.

Add the following code to the logout page:

```javascript

import wixUsers from 'wix-users';
import wixLocation from 'wix-location';

$w.onReady(function () {
        wixUsers.logout().then(() => {
              wixLocation.to('/home');
    });
});

```

---

<div class="post-metadata">

**Author:** ![universinformatiquei](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@universinformatiquei](https://forum.wixstudio.com/u/universinformatiquei)\
**Post date:** [December 13, 2017, 12:47pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/5 "2017-12-13T12:47:53Z")

</div>

Yes I knew this one 🙂 But I am really looking forward to have a simple button that can bring you wherever you want once click. Asking the customer to click a logout button on 2 pages is not very professionnal ☹

thanks to let me know if you find a solution

---

<div class="post-metadata">

**Author:** ![admin-23](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/admin-23/32/708_2.png) [@admin-23](https://forum.wixstudio.com/u/admin-23)\
**Post date:** [December 13, 2017, 3:22pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/6 "2017-12-13T15:22:27Z")

</div>

Hi,

You do not need to ask the user to click two buttons.  
On the first page you add the ‘Logout’ button, with code to move the user to the ‘Logout’ page.  
On the ‘Logout’ page you automatically log the user out (per my code example above) and then redirect him back to the homepage - all in one user click.

---

<div class="post-metadata">

**Author:** ![universinformatiquei](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@universinformatiquei](https://forum.wixstudio.com/u/universinformatiquei)\
**Post date:** [December 13, 2017, 3:39pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/7 "2017-12-13T15:39:43Z")

</div>

haaaa got it…Will try right away 🙂

---

<div class="post-metadata">

**Author:** ![universinformatiquei](https://avatars.discourse-cdn.com/v4/letter/u/b2d939/32.png) [@universinformatiquei](https://forum.wixstudio.com/u/universinformatiquei)\
**Post date:** [December 13, 2017, 3:57pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/8 "2017-12-13T15:57:23Z")

</div>

Yep, got it, it’s working. Thanks 🙂

---

<div class="post-metadata">

**Author:** ![mtilbury](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mtilbury](https://forum.wixstudio.com/u/mtilbury)\
**Post date:** [February 22, 2018, 4:44pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/9 "2018-02-22T16:44:29Z")

</div>

How can I get this to work. I have the following code? I want to mive to the Logout page and then get redirected?

export function loginButton\_click(event) {  
// user is logged in  
if(wixUsers.currentUser.loggedIn) {  
// log the user out  
wixUsers.logout()  
.then( () =\> {

```
    // update buttons accordingly 
    $w("#loginButton").label = "Login"; 
    $w("#profileButton").hide(); 
    $w("#VendorsButton").hide(); 
    $w("#ResourceButton").hide(); 
    $w("#ForumButton").hide();         
    wixLocation.to('/logout');

```

---

<div class="post-metadata">

**Author:** ![phongdaegi](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@phongdaegi](https://forum.wixstudio.com/u/phongdaegi)\
**Post date:** [February 24, 2018, 2:46am UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/10 "2018-02-24T02:46:17Z")

</div>

I log out but do not go to the specified path.

---

<div class="post-metadata">

**Author:** ![admin-23](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/admin-23/32/708_2.png) [@admin-23](https://forum.wixstudio.com/u/admin-23)\
**Post date:** [February 25, 2018, 9:37am UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/11 "2018-02-25T09:37:07Z")

</div>

Hi,

At the moment logout() does not return a promise, therefore once you call it it immediately logs the user out and refreshes the page.

We are working providing better functionality. stay tuned.

---

<div class="post-metadata">

**Author:** ![phongdaegi](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@phongdaegi](https://forum.wixstudio.com/u/phongdaegi)\
**Post date:** [February 25, 2018, 10:13am UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/12 "2018-02-25T10:13:30Z")

</div>

Logout does not go to the specified screen.

wixUsers.logout()  
.then( () =\> {  
[wixLocation.to](http://wixlocation.to/) (‘/logout’); \<\< // Do not move to the logout screen.

---

<div class="post-metadata">

**Author:** ![mtilbury](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mtilbury](https://forum.wixstudio.com/u/mtilbury)\
**Post date:** [February 26, 2018, 9:46am UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/13 "2018-02-26T09:46:33Z")

</div>

This is my exact issue too Phongdaegi - It logs out but stays on the ‘restricted page’.

wixUsers.logout()  
.then( () =\> {  
[wixLocation.to](http://wixlocation.to/) (‘/logout’); \<\< // Do not move to the logout screen.

The wixLocation.to Code doesnt work on any of my code. I was trying to mimic the above code. If the button says “Login” then [wixLocation.to](http://wixlocation.to/) (‘/logout’). when on the Logout page ‘On ready’ the Wixuser.logout and then wixLocation.to (/Home)

In a nutshell. If I logout on a restricted ‘Members Only’ page it errors 403 - error because it is attempting to stay on the same page. when it should go to the home page.

I guess What should happen is that no button should have the logout code. The Logout code should be on the Logout Page. All the Logout button does wixlocation.to (/logout) - but that doesnt work.

---

<div class="post-metadata">

**Author:** ![mtilbury](https://avatars.discourse-cdn.com/v4/letter/m/919ad9/32.png) [@mtilbury](https://forum.wixstudio.com/u/mtilbury)\
**Post date:** [February 26, 2018, 11:39am UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/14 "2018-02-26T11:39:47Z")

</div>

Boom. Fixed it.

export function loginButton\_click(event) {  
// user is logged in  
if(wixUsers.currentUser.loggedIn) {  
// log the user out  
wixLocation.to(“/home”);  
wixUsers.logout()  
.then( () =\> {

```
    // update buttons accordingly 
    $w("#loginButton").label = "Login"; 
    $w("#profileButton").hide(); 
    $w("#VendorsButton").hide(); 
    $w("#ResourceButton").hide(); 
    $w("#ForumButton").hide();         
    $w("#MemberButton").hide(); 

```

The code is case sensitive so /Home needs to match what is in the setting. if its /home it will not work. also the same for wixLocation. it will not pick up WixLocation.

---

<div class="post-metadata">

**Author:** ![phongdaegi](https://avatars.discourse-cdn.com/v4/letter/p/ccd318/32.png) [@phongdaegi](https://forum.wixstudio.com/u/phongdaegi)\
**Post date:** [February 26, 2018, 12:08pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/15 "2018-02-26T12:08:16Z")

</div>

Haha This is a very good shortcut.  
It works well.

---

<div class="post-metadata">

**Author:** ![ryanwilliams](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/ryanwilliams/32/6069_2.png) [@ryanwilliams](https://forum.wixstudio.com/u/ryanwilliams)\
**Post date:** [September 15, 2018, 9:01pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/16 "2018-09-15T21:01:15Z")

</div>

@liran-kurtz-wix Is there a way to redirect after someone selects logout from a restricted page from the #accountnavbar module and not from a button that was added separately?

---

<div class="post-metadata">

**Author:** ![jbroadstock](https://avatars.discourse-cdn.com/v4/letter/j/9de053/32.png) [@jbroadstock](https://forum.wixstudio.com/u/jbroadstock)\
**Post date:** [November 14, 2018, 8:33pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/17 "2018-11-14T20:33:52Z")

</div>

same as Ryan Williams - can I redirect after the #accountnavbar log out is clicked - currently opens Wix Log In page but I have a custom one so this isn’t right

---

<div class="post-metadata">

**Author:** ![stephenmccall](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stephenmccall/32/1773_2.png) [@stephenmccall](https://forum.wixstudio.com/u/stephenmccall)\
**Post date:** [January 12, 2019, 5:50pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/18 "2019-01-12T17:50:55Z")

</div>

Did anyone get a code to use the #accountnavbar log out to either a specific page or the custom log in lightbox?

---

<div class="post-metadata">

**Author:** ![mikemoynihan99](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/mikemoynihan99/32/5930_2.png) [@mikemoynihan99](https://forum.wixstudio.com/u/mikemoynihan99)\
**Post date:** [January 12, 2019, 6:10pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/19 "2019-01-12T18:10:13Z")

</div>

as far as i’m aware you can not create an event for [#accountnavbar](https://www.wix.com/code/home/forum/search/posts%3Fquery=.num.ccountnavbar)

---

<div class="post-metadata">

**Author:** ![stephenmccall](https://sea1.discourse-cdn.com/wix/user_avatar/forum.wixstudio.com/stephenmccall/32/1773_2.png) [@stephenmccall](https://forum.wixstudio.com/u/stephenmccall)\
**Post date:** [January 12, 2019, 8:20pm UTC](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441/20 "2019-01-12T20:20:35Z")

</div>

Hi @Mike do you know if there is any other way to change the page that it redirects to? Or to direct the log out to another page?

[Next page](https://forum.wixstudio.com/t/wixusers-logout-on-protected-page-issue/12441.md?page=2)
