(Note · · 6 min read)
A connected wallet is not a login, and a balance read in the browser is not proof
Connecting a wallet tells your application one thing: somebody typed an address into a browser. It does not say they control it, and the token balance the page reports is a number the page chose to send you. If either decides what a user gets, the gate is decorative.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
What connecting actually gives you
A wallet connection hands the frontend a public address. Public is the operative word: addresses are visible on-chain, so anyone can claim one. Nothing in the act of connecting demonstrates possession of the private key behind it, which is the only thing that distinguishes an owner from someone who copied an address out of a block explorer.
So treat a connected address exactly as you would treat a username typed into a form: an identity claim, unverified, worth nothing until it is checked.
Proof is a signature over something you issued
The check is a signed message. Your server generates a one-off value, the browser asks the wallet to sign it, and the server recovers which address produced that signature. If it matches the claimed address, the user controls the key.
The detail that matters is where the message comes from. It has to be issued by your server, unique per attempt, and short-lived. A fixed message is a replayable password: capture one valid signature and it works forever. A server-issued single-use value expires, which is the whole point. EIP-4361, usually called Sign-In with Ethereum, standardises the format and is worth using rather than inventing your own.
1. browser -> server : "I claim 0xabc..."
2. server -> browser: nonce (single use, short expiry, stored server-side)
3. wallet signs the message containing that nonce
4. browser -> server : signature
5. server verifies signature -> recovers address -> must equal 0xabc...
6. server marks the nonce used, issues its own session
Everything after this point trusts the SESSION, never the address the client sends.Read the chain yourself
The second failure is quieter. Having verified who the user is, the application asks the browser how many tokens they hold — and the browser is the one place that answer cannot come from. Any value the client computes, a balance included, is a value the client can change. A boolean like hasToken sitting in frontend state is worth precisely nothing.
Query the contract from your own backend, through your own node or provider, for the address you proved. On Meana Raptor, the Web3 ecosystem I built, the merch store gives holders a discount based on the MRT tokens in their wallet — and a discount is money, so the number that decides it has to be one the server fetched, not one it was told.
The same applies to the gate itself. The decision of whether this user may see this page or receive this price belongs on the server, with the frontend merely reflecting it. Hiding a locked section in the interface is presentation, not access control.
Ownership expires, sessions should too
Token gating has a property ordinary authentication does not: the thing being checked can be sold. A user who held the token at login can transfer it a minute later and keep every privilege the session granted, which is an easy way to share paid access with as many people as there are minutes in your session lifetime.
Keep sessions short, and re-check the balance at the moment it matters rather than only at sign-in — before a discounted checkout, before an entitlement is granted, before anything irreversible. For cheap, read-only views a cached balance with a short lifetime is a reasonable trade; for anything that moves money it is not.
The practical failure modes
- Accepting an address from the client on later requests instead of reading it from the session. Then any user can send any address.
- A fixed or client-supplied message to sign, which makes a captured signature permanent.
- Trusting a balance, token list or NFT ownership flag computed in the browser.
- Checking the wrong chain. The same address holds different balances on different networks, so pin the chain id explicitly rather than following whatever the wallet is switched to.
- Forgetting token decimals, so a comparison against a raw integer silently passes or fails for everyone.
- Treating a failed provider call as zero. A node timeout is not a balance of nought; it is an error, and quietly revoking everyone's access is its own incident.
It is the same rule as everywhere else
None of this is specific to blockchains. It is the ordinary rule that the client states what it wants and the server decides what it gets, applied to a context where the identity happens to be a key pair and the entitlement happens to live on a public ledger.
The test is the same one worth applying to any gate: if someone opened the developer tools and edited every value the page holds, what would stop them? If the honest answer is nothing, the gate was never on the server.
Common questions
Does connecting a wallet prove the user owns it?
No. Connecting reveals a public address, and addresses are public by definition, so anyone can claim one. Ownership is only demonstrated by a signature over a message your server issued, verified on your server.
Why does the message being signed have to come from the server?
Because a fixed message is a replayable password: one captured signature would work forever. The server should issue a single-use, short-lived value per attempt, store it, and mark it used once redeemed. EIP-4361, Sign-In with Ethereum, standardises this.
Can I check a token balance in the frontend for token gating?
Not for anything that matters. Any value the client computes it can also change, so a balance or an ownership flag in frontend state is worth nothing. Query the contract from your backend for the address you verified, and keep the access decision on the server.
How often should a token-gated app re-check ownership?
At the moment the entitlement is used, not only at login. Tokens can be sold immediately after signing in, so keep sessions short and re-check before discounted checkouts, granted entitlements or anything irreversible. A briefly cached balance is acceptable for read-only views.
What are the common bugs in token gating?
Accepting an address from the client instead of the session; a fixed message to sign; trusting a browser-computed balance; querying the wrong chain, since the same address holds different balances per network; ignoring token decimals; and treating a failed provider call as a zero balance, which revokes everyone's access during an outage.