Redirect user on logout of members area (not using a button)

Hey,

I was wondering if there is a solution to redirecting the user out of the members area, due to the fact it directs them to the wix sign in page, I have a custom sign up. I know I can code a button to do this but having the login on the mobile menu works well for my site and there are more pros than cons to keeping the #accountNavBar.

I have played with making a lightbox for the mobile menu and I can code this to work but it’s laggy and a lot of work for what should be a simple solution.

Any help would be most appreciated or…

wixUsers.onLogout( (user) => {  

} ); 

would be even better!

Thank you!

Hey,

For this you need redirect the user to page that not require user login right? Maybe like a home page?
You can try two fuctions, than redirect a user after logout and a verification if the user is logged in.

//REDIRECT AFTER LOGOUT
import wixLocation from ‘wix-location’;
wixUsers.logout()
.then( () => {
wixLocation.to(“/home”)
} )
. catch ( (err) => {
let errorMsg = err; // “No member is logged in”
} );
//--------------------------------------------------------
// DETECT USER
import wixUsers from ‘wix-users’;
import wixLocation from ‘wix-location’;
// PAGE MEMBER CODE
$w.onReady( function () {
if (!wixUsers.currentUser) {
wixLocation.to(“/home”)
}
});

hey @matheus , thanks for your response. The problem is that the code runs straight after the user logs in, so just logs them out straight away, even if this isn’t used in the on.Ready function. I’ve tried to use an if statement but still no dice…

hello i too have been wondering about #accountNavBar as i have an issue when logging out.
i think my problem is the same as @stephen , but from what i notice on my site is when i log out of any member areas it logs out and opens a white page with my custom login light box.

the only way that i can use the site again is if i click out of this light box and then i am redirected to the home page , but i wish for this to be done automatically so that the user doesn’t have to worry about all that.

if you need any more info im happy to help

1 Like

I have the same issue :frowning:

1 Like

@Metroxix

For redirecting on logout with Default Login Bar try something like this…

//place this code on your “site” code page

import wixUsers from ‘wix-users’;
import {session} from ‘wix-storage’;

$w.onReady( function () {

wixUsers.onLogin( () => {

session.setItem("loggedStatus", "user previously logged in for session"); 

console.log(“login success”);

});

//place this code on the login page that you want them to be re-directed from

import wixLocation from ‘wix-location’;
import {session} from ‘wix-storage’;

$w.onReady( function () {

let check = session.getItem(“loggedStatus”);

if (check === “user previously logged in for session”){

//note, you should remove the data from wix-storage as suits for your particular site, this is so

//as you do not prevent people continually being re-directed from a page that they are

//trying to legitimately access.

     session.removeItem("check"); 

//after removing item from wix storage then proceed to re-direct

       wixLocation.to('https://google.com');  //change this to what ever page you want to redirect too 
    } 

else {

            console.log("no previous log in for session") 
        }

@mikemoynihan99 im sorry but im not really following as i am a beginner in all this are there element i need to remove and swap around sorry

@metroxixarts
I’m guessing from you’re question that you have not actually tried to use the code ?

@mikemoynihan99 i used the part of the code in the parts that i think you want them in but im a total noob and i dont really understand any language

@metroxixarts
You don’t need to change any of the code if you don’t want to, it will work as is.

You just copy and paste it as is.

Paste half code on your “site” code page, paste the other half on the login page that you want them to be re-directed from.

@mikemoynihan99 sorry im putting the second half every where i can think of to make this work as is but im not sure if im suppose to us the " light box " page that the custom log in is on , or the page of my wix member area . im just lost in general on this

@metroxixarts
The 2nd part of the code will execute on any page, place it on whatever page you like.

The page you put the 2nd part of the code in will cause a logged in user or previously logged in user to be redirected to the URL specified in the code.