So I have this weird issue in my app, where after the first time someone connects with Metamask, it all works just fine, but once the user refreshes/disconects etc, anything after the first sign in, I can no longer get his wallet address or tokens balances.
It is important to note that this only happens after the app is being BUILT and served (both locally and on vercel, both have that issue!). No such issues when its being run in dev mode.
The only way for me to again get information about currencies and things like that is for the user to disconnect (from Metamask!) and reconnect, and then it repeats, on the first sign in Iâll get them and then I wonât.
Here is a minimal code to reproduce it:
import { useMoralis, useNativeBalance, useERC20Balances } from 'react-moralis'
import { useEffect } from 'react'
export default function Home() {
const {
authenticate,
enableWeb3,
isAuthenticated,
logout,
user,
account,
} = useMoralis()
const { data: nativeBalance } = useNativeBalance()
const { data: tokensBalance } = useERC20Balances()
console.log(user?.attributes?.ethAddress)
console.log('account', account)
console.log('nativeBalance', nativeBalance)
console.log('tokensBalance', tokensBalance)
useEffect(() => {
const authAndEnable = async () => {
try {
await authenticate()
await enableWeb3()
} catch (e) {
console.log(e)
}
}
if (isAuthenticated) {
authAndEnable()
}
}, [])
return (
<main>
<button onClick={isAuthenticated ? logout : authenticate}>
{isAuthenticated ? 'Disconnect' : 'Connect'}
</button>
</main>
)
}
This code goes into pagse/index.js in a brand new next app, created with:
create-next-app .
The only packages installed are:
npm i moralis react-moralis @walletconnect/web3-provider
npm run build && npm run start
and there you have it.
I have just installed it, so I should be with the latest versions of the packages:
"dependencies": {
"@walletconnect/web3-provider": "^1.7.0",
"moralis": "0.0.176",
"next": "12.0.7",
"react": "17.0.2",
"react-dom": "17.0.2",
"react-moralis": "^0.3.11"
},
Hereâs a short video that I made to show it: https://youtu.be/WKzyMkVEMNQ