Initialization issue

I am having problems with the following code, I would appreciate any help.

//Init-----------------

const serverUrl = serverURL;

const appId = _appId;

Moralis.start({ serverUrl, appId });

login();
//--------------------

//-----------------------------------------Async function using Moralis.executeFunction(options);
async function _new() {
let _Number = str;

let options = {
contractAddress: “contractAddress”,
functionName: “new”,
abi:abi1,
params: {
number: _Number,
}
}
try {
const exec = await Moralis.executeFunction(options);
const result = await exec.wait();
window.location.href = ‘/New.html’;
}
catch(err) {
console.error(err);
}
}

Error: Missing web3 instance, make sure to call Moralis.enableWeb3() or Moralis.authenticate()

Are you using these latest Morlais and web3 scripts

    <script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
    <script src="https://unpkg.com/moralis@latest/dist/moralis.js"></script>

I had an older moarlis version, I updated that, it was worth a shot. I still get the same error. Originalyy I was initiating moralis inside of the await function and it was working.

My most immediate issue was this…

if (!user) {
user = await Moralis.authenticate({provider: “web3Auth”,
clientId: “BDL2JqjjLfCr0FXUYYrq3gEIhPG05WS9FANqwl78PbU23mXdYC-”,
chainId: “0x89”,
})

somehow user was still valid on refresh so I was not actually calling moralis.authenticate(). As a hackjob I am now calling logout(), login() at the beginning of my function.

Obviously I don’t quite understand how the login, logout, authenticate serverStart() stuff should flow… I would really appreciate any tips about how to structure my login/logout properly. The error is resolved. THANKS!

Previously logged-in user data is stored in local storage. So, check if the current user exists and hide the login or logout button accordingly.

if (Moralis.User.current() != null) {
//show logout button and hide login button
}
else {
//show login button and hide logout button
}