[SOLVED] Restore Moralis session after page refresh next js

Hi, is it possible to store useMoralis in local storage of the browser and reuse? So reopen a session, or do you have a function to do this?
My problem is, that when I reload my page, the user gets logged out.

Best,
John

how do you know it logs out? did you check the value of isAuthenticated from useMoralis?

After the page refresh it says: isAuthenticated true.
When querying the account it says null.
Is there any way to store the “session” in local Storage?

But even

you mean account like from useMoralis? if so you might have not called enableWeb3

Hi,

I use this line:
const {
account,
Moralis,
isAuthenticated,
} = useMoralis()

Which line would I have to add before this to enableWeb3?

If I add Moralis.enableWeb3() after the line stated above, I get an error:
Error: Cannot execute Moralis.enableWeb3(), as Moralis Moralis.enableWeb3() already has been called, but is not finished yet

Moralis.enableWeb3() worked for me. I just put it in a useEffect with [isAuthenticated]
and introduced a new variable to check if function has already been called:

const [calledEnableWeb3, setCalledEnableWeb3]=useEffect(false)

useEffect(() => {
!calledEnableWeb3&&(
setCalledEnableWeb3(true),
Moralis.enableWeb3()
)
}, [isAuthenticated])

1 Like

I think it’s bad documented since on the blog (https://moralis.io/how-to-build-a-web3-login-in-5-steps/) and in the examples on the docs (https://docs.moralis.io/moralis-dapp/users/crypto-login/metamask) they don’t mention anything about enableWeb3 or restoring session. For me, it worked by adding this effect:

useEffect(() => {
  const connectorId = window.localStorage.getItem('connectorId') as MoralisType.Web3ProviderType
  if (isAuthenticated && !isWeb3Enabled && !isWeb3EnableLoading) {
    enableWeb3({ provider: connectorId }) }
}, [isAuthenticated, isWeb3Enabled])

Thanks, for the code. I actually agree, that the written documentation sadly is very incomplete. @MoralisTeam: I believe you are actually leaving a lot of potential untapped by not providing better documentation for your software.

Best,
John

1 Like

You can also contribute to the documentation anytime, it is open source.

1 Like

Good to know, will do when i stumble across a bigger flaw next time.

Hi guys, I just wanna clarify something.

Does

await Moralis.enableWeb3()

have to be called every time you use executeFunction()?

or

Does it only have to be called once to enable Web3? If it only has to be initialized once, where should I instantiate it in my react page?

How can I get connectorId ?
How looks like connectorId ?
I am getting the console error after moralis login in my next.js project.
Failed to execute 'setItem' on 'Storage': Setting the value of 'Parse/xyxxyxyyxyxyxyxyxyyxyxyxyxy/currentUser' exceeded the quota.

enableWeb3({ provider: connectorId }) } didn’t help me to restore moralis session.
After page refresh, it is automatically logged out.

You can just use enableWeb3() with the same options you use for authenticate(). You would only get logged out on page refresh if you also clear local storage (where the current user is cached).