Keep login email on all site pages

I’m having trouble with:
Is there a way to keep login information in a global variable so that the pages of the site display in a different way depending on whether the customer is logged in or is just a visitor ?

Working in
Wix Editor

Site link

What I’m trying to do
I would like that logged in visitors have one option more on several pages of my site. The option is hidden, and if the user is a registered user, then the option appears (the option is for instance a button which opens a popup).

What I’ve tried so far
I have tried to create a session variable, to use the masterPage.js, and other ways, but I don’t succeed to keep the information when I navigate from page to page

Extra context

Here is an example : the ? button has to be displayed only if a member is connected. I am able to display it one time when I connect myself, but then, when I go to another page and when I come back, it disappears.

Today, as I can’t manage it, I display it all the time…

Looking for several posts in this forum, I have mixed two methods to success in my project

First, in my home page , the variable userEmail must be declared at the very beginning to be used anywhere in the code

Then

1) I have to insert an authentication async onConnect event to record the loginEmail

2) I have to check the loginEmail whether I authenticate or not, because I can go back to my home page after surfing on other pages.

Here is the results :

import { authentication } from 'wix-members';
import { currentMember } from 'wix-members-frontend';

let userEmail;

currentMember   //check if a userEmail has been logged in (#2)
    .getMember()
    .then((member) => {

        userEmail = member.loginEmail;
        if (userEmail) {
            $w('#countPuzz').show();

        } else {
            $w('#countPuzz').hide();
        }
        return member;
    })

    .catch((error) => {
        console.error(error);
    })

//authentication on login event (#1)
authentication.onLogin(async (member) => {
    $w('#countPuzz').show();

    currentMember // it must be repeated to edit the variable "userEmail"
        .getMember()
        .then((member) => {

            userEmail = member.loginEmail;
            return member;

        })

        .catch((error) => {
            console.error(error);
        })

});

And, further, the code

        **if** ((userEmail == …..

works in all cases

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.