recognize the session in a login

Good morning, I have the following problem I hope you can help me one more time. First of all I explain why the way I decided to do it this way, I have my site in which it has a login page since I do not need a registration since the users will be directly in the database entered manually in a dataset, the login was built with the following elements and the following code


import wixData from "wix-data"
import wixLocation from "wix-location"
import wixWindow from 'wix-window';

$w.onReady(() => {
    $w("#button1").onClick(async () => {
        let email = $w("#email").value
        let contrasena = $w("#contrasena").value
        await checkLogin(email, contrasena)
    })
})

async function checkLogin(email, contrasena) {
    const query = await wixData
        .query("logingers")
        .eq("title", email)
        .eq("contrasena", contrasena)
        .find()
    if (query.items.length > 0) {
        wixLocation.to("/principal") 
    } else { 
        $w( "#text8" ).show()
        // here I would like my window to appear with the error message
        wixWindow.openLightbox ( 'mensaje error' )
       // console.error("Email/Contrasena wrong!")
    }
}

The pages have a horizontal menu in which the “events” button directs the login and this in turn directs to a “main” page that cannot be accessed from the menu without going through the login.


The big problem I have is that once the user logs in to see the content of the “main” page and continues browsing the site and wants to see the “main” page again by clicking on the “events” button of the The menu again directs you to the login without respecting that you had previously logged in, as shown in the video

Previously they had helped me to implement a kind of timer that would recognize the session for a certain time, once the indicated session time expired again, the login would appear, but as you can see, it does not work, it still does not recognize the session, I show code
import wixStorage from 'wix-storage';
import wixLocation from 'wix-location';
import wixData from 'wix-data';
import wixWindow from 'wix-window';
var onlineState

$w.onReady(() => {
    setInterval(logOut, (5*1000*60)); // --> LOGOUT every 5min
    $w("#button1").onClick(async () => {
        //getting online-status...
        onlineState = wixStorage.session.getItem("onlineState");
        if(onlineState === "true") {
          wixLocation.to("/principal")
        }
        else {
          let email = $w("#email").value
          let contrasena = $w("#contrasena").value
          await checkLogin(email, contrasena)
        }
    })
})

function logOut() {onlineState === "false"; 
  //setting online-status...
  wixStorage.session.setItem("onlineState", "false");
}

async function checkLogin(email, contrasena) {
  wixData.query("logingers")
  .eq("title", email)
  .eq("contrasena", contrasena)
  .find()
  .then((res)=>{
    if (res.items.length > 0) {//setting online-status...
      wixStorage.session.setItem("onlineState", "true");
      wixLocation.to("/principal") 
    } else { 
      wixWindow.openLightbox('loginwarn');
      //console.error("Email/Contrasena wrong!")
    }
  });
}

How can I do to achieve this? Could it be the button on my menu? Could you please help me? :pray::pray:

Ok, an last idea for you.

Let us take this situation here…

The big problem I have is that once the user logs in to see the content of the “main” page and continues browsing the site and wants to see the “main” page again by clicking on the “events” button of the The menu again directs you to the login without respecting that you had previously logged in

Your LOGIN is placed on the → EVENTS-page, right?
When a USER loggs-in → he will be redirected to your → MAIN-page.
The Main-page itself do NOT exists on your Menu (hidden) and can only be reached trough the LOGIN on EVENTS-page.

OK, i think till here everything is clear now.

So all you need is a timer-function → You have already created one.

Place the Timer-Function on your events-page. When an already logged-in USER starts to navigate trough all of your pages and lands on the events-page again, where he will see the LOGIN again…

…you check your TIMER if LOGIN-TIME already ran out.
If NOTRedirect again to the page you want to .
If YESDo nothing . (the USER has to log-in again, to refil his ONLINE-TIME)

This way you have solved your problem.

tansk