Crypto login

Authenticate with Ethereum, Binance Smart Chain, Polygon, Elrond, and many others to come! Link multiple addresses from multiple chains to the same user profile.

Ethereum, BSC, Polygon Login

Authenticating users is simple:

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

This will connect Metamask and request a signature (no gas required!)

It works the same way for all Ethereum Virtual Machine (EVM) compatible chains- like Binanace Smart Chain and Polygon (Matic)- as they all share the same Ethereum addresses.

Non EVM Chain Login

Authenticating with non Ethereum Virtual Machine (EVM) compatible chains requires a bit more setup.

Elrond

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 Addresses

It's possible to link multiple addresses from multiple chains to the same user profile! This is done with the Moralis.Web3.link() function. Once an address is linked to the user, Moralis will pull all transactions, ERC20 tokens, and NFTs for those addresses as well. This way all user transactions can be gathered together in one place.

Ethereum, BSC, Polygon

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

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.

await Moralis.ERD.link(address)

User Object

After the signature is completed the promise will resolve with a User object. The shape of the created user looks like this:

{
className: "_User"
id: String
_objCount: 0
attributes: {
accounts: Array,
ACL: Object,
authData: Object,
createdAt: Date,
email: String, // empty
ethAddress: String,
sessionToken: String,
updatedAT: Date,
username: String, // random value
}
}

For new users the username will be a randomly generated alphanumeric string and the email property will not exist. This can be set or changed by the app. See Objects and User docs.

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 entry 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
}

Chain specific user properties

If you enable syncing data from chains other than Ethereum some additional user properties will be created. These will be prefixed with the chain. I.e. for Binance Smart Chain the prefix is bsc. There will be an additional set of properties for each chain.

  • User.bscAddress (String)

  • User.bscAccounts (Array)

The same for chain specific class tables on the Moralis Server. An additional set of classes will be created with the corresponding chain prefixes.

  • BscTransactions

  • BscTokenBalance

  • BscTokenTransfers

  • BscNFTOwners

  • BscNFTTransfers

Chain Prefixes

Chain

Prefix

Ethereum Mainnet, Ropsten, Goerli, Rinkeby, Kovan

Eth

Binance Smart Chain Mainnet, Testnet

Bsc

Polygon (Matic) Mainnet, Mumbai Testnet

Polygon

Elrond

Erd

Manually Deleting Users

During testing if you manually delete a user also be sure the delete the corresponding rows in the following collections (where "xxx" is the chain prefix). Not doing so may cause unexpected behavior when authenticating via Web3 from the address that was just deleted.

  • Session

  • xxxBalance

  • _xxxAddress

Changing sign in message

The message the user sees when signing in with Web3 can be changed by doing the following in the browser.

Moralis.Web3.getSigningData = () => 'My custom message'

​