Next.js Discord

Discord Forum

Switching metamask account should change session? Web3, next-auth

Answered
Atlantic menhaden posted this in #help-forum
Open in Discord
Atlantic menhadenOP
Hello, i am using wagmi and next-auth to authenticate with metamask (using app dir). Now when user first comes to website he has to connect with metamask and sign with his private key in metamask. I then store his address in the db and create session token.

How can i make it so every time user switches account in metamask, it will also update session? How to asociate one user account to one session or that jwt?

I want to display the nfts he owns, but if the account is switched, the nfts stay the same from previous account
Answered by fuma
Re-sign in makes more sense for this though
View full answer

11 Replies

Though I’m not familiar with NFT things, as I know, since it’s happened in metamask, Next Auth won’t be able to know anything if the metamask account is switched.

Unless you have a way to check if the user’s account is switched, it’s probably impossible.

Btw if there’s a way to determine, please tell me
@fuma Though I’m not familiar with NFT things, as I know, since it’s happened in metamask, Next Auth won’t be able to know anything if the metamask account is switched. Unless you have a way to check if the user’s account is switched, it’s probably impossible. Btw if there’s a way to determine, please tell me
Atlantic menhadenOP
i am using wagmi to check whether account has switched, so that is not the problem. But what i want to know is what should i do with the session that was previously used with other account? should i destroy it or logout, but i dont want to sign a new message every time user switches accounts
so is there a way to bind session to account address?
Just logout, one session should be bind to one account, no exceptions, it can simplify user management, and easier to maintain
@fuma Just logout, one session should be bind to one account, no exceptions, it can simplify user management, and easier to maintain
Atlantic menhadenOP
okay thats one option. but what if i dont logout? or dont want to logout? bc i want to be able to switch accounts without having to login again. i just want the session that is binded to that address to be active when i switch to that user
Next Auth doesn’t provide a function to update the JWT token, which means the you have to sign in again to revalidate your session
A better way is to fetch the current account of user on demand
@fuma A better way is to fetch the current account of user on demand
Atlantic menhadenOP
what do you mean by that?
Like storing a user id or something similar, and only get the account of user when you need it
Re-sign in makes more sense for this though
Answer
@fuma Re-sign in makes more sense for this though
Atlantic menhadenOP
okay thank you