Crypto login

With Web3 Signature

Authenticating new users is as simple as:

Moralis.Web3.authenticate().then(function (user) {
console.log(user.get('ethAddress'))
})

This will launch the Web3 Wallet like Metamask and will request signature, after the signature is completed the promise will resolve with the user as a response, the schema of the created user in the Moralis.User object looks like this:

{
"objectId": String,
"emailVerified": Boolean,
"accounts": Array,
"ACL": ACL,
"updatedAT": Date,
"authData": Object,
"username": String,
"ethAddress": String,
"createdAt": Date,
"password": String,
"email": String
}

It will also create a new entry in the Moralis._EthAddress class, corresponding to the Ethereum Address used for this user, the schema of the created user in the Moralis._EthAddress object looks like this:

{
"objectId": String,
"transactions_synced": Number,
"ACL": ACL,
"last_eth_sync": Date,
"data": String,
"user": Pointer <_User>,
"updatedAt": Date,
"is_syncing": Boolean,
"last_eth_sync_completed": Date,
"signature": String,
"last_eth_sync_block": Number,
"transactions_total": Number,
"createdAt": Date,
"last_eth_sync_error": String
}

How to Authenticate With Metamask, Web3 Development - Ivan on Tech

Linking Addresses

Normally a user has more than one Ethereum address. You will want to maintain the same user session even if the user changes to another active address, this is called Linking. This will add the address to the current user, allowing the user to change their active address and still have the same user profile.

Moralis.Web3.onAccountsChanged( async (accounts) => {
const confirmed = confirm("Link this address to your account?");
if (confirmed) {
await Moralis.Web3.link(accounts[0]);
}
});

Note, calling link() on an address already associated with a user will throw an error. You can see which addresses the user has already linked by querying the user.attributes.accounts array (which will be undefined if the user has not yet linked or authenticated an address).

How to Merge MetaMask Addresses? Web3 Programming Tutorial - Ivan on Tech

Elrond Login

Prerequisites: In order to login with Elrond you will need a Ledger which can be purchased here. Once you have a ledger follow the steps in the Elrond Ledger documentation to add the Elrond App to the device.

Once the above steps are completed we can continue with the actual auth process.

If using the CDN you must add the erdjs script to your head.

<script src="https://npmcdn.com/@elrondnetwork/[email protected]/out-browser/erdjs.js"></script>

Now that all dependencies are in place we can authenticate.

Moralis.authenticate({ type: 'erd' }).then(function (user) {
console.log(user.get('erdAddress'))
})

Important: Notice the prefix of the address has changed from eth to erd

Linking Elrond Address

Elrond accounts can be linked to existing accounts using the following code. This will prompt the user to authenticate on the Ledger device.

Moralis.ERD.link()