Log out and redirect to home page

I saw multiple posts on this topic, it seems to come up quite often but none seem to have really resolved it in a straight-forward manner, and/or I don’t really understand them. HELP!

I am in the editor, using just the default Wix login/logout function. when a member logs out of a members-only page, I want them to be redirected back to home page. Do I put some code into the box “Site Code” at the bottom of the editor? Any help would be appreciated, thanks a lot!

Hello Chengchen-Li,

well first thing is —> yes the CODE belongs to the SITE-SECTION, because you want this CODE to work on all of your pages.

Second: Perhaps this can help you…

import wixUsers from 'wix-users';

$w.onReady(function () {
 let user = wixUsers.currentUser;
 let isLoggedIn = user.loggedIn;

 if(!isLoggedIn){console.log("User not logged-in.");}
 else {console.log("User logged-in");}
})

You have just to set the missing redirection.command and import the related API to this command. ----> location

(See the results in CONSOLE)

Thank you so much! This is super helpful. sorry I am very new to this, could you elaborate on how to " set the missing redirection.command and import the related API"?

Import of API… (APIs goes always to the TOP of CODE)

import wixLocation from 'wix-location';

And here you will find the rest…

Thank you! I ran into a problem because if a user is not logged in, the code always takes the user back to the homepage no matter what. How do i modify the if statement to say “if user just decided to log out”, instead of “if user status is not logged in”?

When you decided to do something, it does not mean that you have done it.

ACTION & REACTION

NO ACTION = NO REACTION

So, there must be an ACTION, before a REACTION can follow.

Your ACTION is for example a “BUTTON-PRESS”.

Then REACT to the done ACTION :grin: