Iām testing out other wallet providers and ran into the same issue with both Wallet Connect and Magic Link:
Iām using my app with Metamask, then I log out of the app with the metamask user.
When I log in with another wallet provider (WC/Magic) the auth works but when I try run a contract method like buying an NFT, I get a revert saying ācannot buy from yourselfā. It turns out the signer is still the address from metamask!
Since this is for both WC and Magic thereās something not right in setting the alternative provider when metamask is still āconnectedā though not logged in.
Ok I think this is related to me not setting the provider correctly when initiating the new user
It was authenticating fine but doing transactions is where it became funky.
export default class MagicWeb3Connector extends AbstractWeb3Connector {
type = 'MagicLink';
async activate({ email, apiKey, network }) {
let magic = null;
let ether = null;
try {
await this.deactivate();
} catch (error) {
// Do nothing
}
if (!email) {
throw new Error('"email" not provided, please provide Email');
}
if (!apiKey) {
throw new Error('"apiKey" not provided, please provide Api Key');
}
if (!network) {
throw new Error('"network" not provided, please provide network');
}
Thanks for the input, totally sharp, I was looking at an older version. However, Iām using the correct version in my bare bone test setup from https://unpkg.com/moralis/dist/moralis.js
Still throws the same error, caused by callingMoralis.enableWeb3({ provider: "magicLink" }) when a user has been found. Not only after login, but also on reloading the page and user session.
Ok so I was calling enableWeb() right after authenticate(), which is not needed I found out, as authenticate already adds Moralis.web3.
So logging in is fine now, but when I refresh or come back to an existing user session, Iām already authenticated so I need to call enableWeb().
How do I know the provider options for the current user session?
I cannot call it with just await Moralis.enableWeb3({provider: state.provider}), this will always throw āmissing clientIdā or āemailā or any params you need to set when connecting to an alternative provider.
whichever way I look at the code, Moralis.enableWeb3() is always wanting to do a new login and I have to provide all options. So basically there is no session that can be restored on refresh, itās always forcing a new login and forcing those options.
For example for Magic Link thereās magic.user.isLoggedIn() to check a valid session https://magic.crisp.help/en/article/how-are-sessions-handled-with-magic-1h92t2s/
But what the MagicWeb3Connector does is always log in again when calling enableWeb3().
Can 100% confirm now I have have WC working, session is restored on refresh, transactions all good, can switch back and forth between a WC and Metamask user without issues
If the RPC stays healthy, should be fine. I did loose connection one time between the mobile and website and had to logout and login again. Will see later if I can catch that somehow to warn the user.
As for Web3Auth and Magic Link, the main issue remains that I cannot call Moralis.enableWeb3() when a user is found on refresh. Unlike WC, itās asking for all options to be passed, not only the provider name.
As discussed already in this thread, this does not work for both Web3Auth and Magic:
function loginMagic() {
Moralis.authenticate({
provider: 'magicLink',
email: '[email protected]',
apiKey: 'pk_live_xxx',
network: 'rinkeby',
})
.then((user) => {
// all fine
console.log('Magic user', user);
console.log(user.get('ethAddress'))
})
}
// enable web3 if user present
window.addEventListener('load',
async function() {
if (Moralis.User.current()) {
const web3 = await Moralis.enableWeb3({ provider: 'magicLink' })
// throws error missing options
}
}
})
Looking at the code, I would suggest to check for magic.user.isLoggedIn() and call magic.auth.loginWithMagicLink() when no user session has been found?
Hard to debug the rest if I cant refresh properly, but have been able to done some tx.
Itās crazy this Magic Linkā¦ there are no confirmation windows, no wallet(?), it just goes wowā¦Feels funny. Question is, can I see my wallet elsewhere or should I build additional wallet features inside my app?