Site code and Dashboard pages

I needed a method to control access of site members only who had logged on in the current session ie if they were a returning logged on member, they are forced to re-sign on. To do this I followed the suggestion found in this old post:

The Site code implemented was:
$w.onReady(async function () {
console.log(“Site on ready”, wixWindow.rendering.env);
if (wixUsers.currentUser.loggedIn && !session.getItem(‘isLoggedInThisSession’)) {
return wixUsers.logout().then(() => {
console.log(“Site (on ready) forced log off”);
});
}
where the Session item “isLoggedInThisSession” was set when the member signed on.

This m ethod t works well on my web pages, but it had one unexpected side effect - it caused one of my Dashboard Pages to endlessly loop.

The Site code loaded followed shortly by the Dashboard Page OnReady function that reads from a collection. The Site code failed at the “return wixUsers.logout().then(() => {” statement above with the fatal error “Cannot sign off Site Owner”. The code crashed and re-loaded the Dashboard Page, causing the Site Code to load, followed by the Page OnReady routine and on and on.

Now, Dashboard pages do not contain headers or footers so the Site code will fail if it tries to access any elements in the header or footer, or as in this case, it crashes on the logout.

Does anyone know if its possible to restrict Site code to running only in a web page environment, or to be able to detect you are in a Dashboard code space.

As a workaround, I’ve reluctantly added the following line before the logged in test:
if (gManagers.includes (wixUsers.currentUser.id)){ console.log(“Found a manager”); return}