Store public address in mongodb

I have an application using NodeJS, React and Mongodb.
Authentication is handled via a Rest API.
I would like to link a users public address to their account.

How can I securely connect metamask/wallet connect and store the users public address in the database?

That would be done by default if you use Moralis sdk at authentication or when using .link

1 Like

@Kooee

as cryptokid suggested ideal way to do it is to do with Moralis SDK.

when you call the authenticate() it automatically saves users public address in database

Will this work if Iā€™m using my own mongodb?
Will I need to remove my own auth and replace it with moralis auth?

I would like to do email and password authentication and address link once authenticated.

you can use both databases, and you can also connect directly to your Moralis Server mongo db database if you want:

https://docs.moralis.io/moralis-server/database/direct_access

How do I do MetaMask to my own database?

you may need to implement your own authentication functionality in that case

Refer to initial question. I have my own auth system, would like to know secure way of writing eth address to mongodb

if you get a signed message from the user, then you can validate the signature and add that eth address in your database

Would I use web3.js to do that or does moralis have a function?

Are these what I need?

web3.eth.accounts.signTransaction(tx, privateKey [, callback]);

web3.eth.accounts.recoverTransaction(rawTransaction);

you can use web3 for that, it seems like you are close to what you need, it may not be those exact functions as you will have to sign a message and not a transaction, but it seems that you are close

you can also look in GitHub on how moralis does the authentication