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)
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.
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:
import wixUsers from 'wix-users';
import wixLocation from 'wix-location';
$w.onReady(function () {
wixUsers.logout().then(() => {
wixLocation.to('/home');
});
});
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
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.
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( () => {
This is my exact issue too Phongdaegi - It logs out but stays on the ‘restricted page’.
wixUsers.logout()
.then( () => { 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 (‘/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.
export function loginButton_click(event) {
// user is logged in
if(wixUsers.currentUser.loggedIn) {
// log the user out
wixLocation.to(“/home”);
wixUsers.logout()
.then( () => {
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.
@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?
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