Crypto Login
Authenticate with Ethereum, Binance Smart Chain, Polygon, Arbitrum, Elrond, and Many Others to Come!

Moralis Authentication

Moralis allows you to authenticate users on any blockchain with just one line of code. All assets, tokens, and NFTs of your users are automatically synced into your Moralis database and are updated in real-time as your users make on-chain transactions.

Ethereum, BSC, and Polygon Login

MetaMask

Authenticating users is simple:
1
Moralis.authenticate().then(function (user) {
2
console.log(user.get('ethAddress'))
3
})
Copied!
This will connect MetaMask and request a signature (no gas required!).
We use the signature as proof the user is owner of account, if no signature is provided, anyone can gain the credentials necessary to read /write to users private data in Moralis Database. The signing is no different than entering a username and password. If a user wants to use the authenticated features of an app they need to β€œlog in”. They choose when to do so by pressing the β€œlogin” button.
It works the same way for all Ethereum Virtual Machine (EVM) compatible chains such as Binance Smart Chain and Polygon (Matic), as they all share the same Ethereum addresses.
As soon as the user is logged in all their assets, tokens, NFTs and past transactions are instantly synced into your Moralis Database. The database updates if the users are moving assets on-chain.
Once the user logs in - all their assets are seen in the database. The database updates if the users move assets on chain.
Logging in your first users on Ethereum, Polygon, Avalanche (or any other chain) using Moralis.

WalletConnect

Practical Demo how to login your users to you dapp with any wallet.
Moralis also supports authentication using WalletConnect. First add the provider by adding the script (make sure to use the latest version, see https://github.com/WalletConnect/walletconnect-monorepo/releases):
1
<script src="https://github.com/WalletConnect/walletconnect-monorepo/releases/download/1.6.2/web3-provider.min.js"></script>
Copied!
Or install the @walletconnect/web3-provider package when you use npm (or another package manager):
1
npm install @walletconnect/web3-provider
Copied!
Then call authenticate like above, but with a provider option:
1
const user = await Moralis.authenticate({ provider: "walletconnect" })
Copied!

Specify the chainId.

You might want to specify the chain id that WalletConnect will use by default. You can do this by providing the chainId as an extra option:
1
const user = await Moralis.authenticate({ provider: "walletconnect", chainId: 56 })
Copied!

Filter Mobile Linking Options

If you would like to reduce the number of mobile linking options or customize its order, you can provide an array of wallet names to the mobileLinks option .
1
const user = await Moralis.authenticate({
2
provider: "walletconnect",
3
mobileLinks: [
4
"rainbow",
5
"metamask",
6
"argent",
7
"trust",
8
"imtoken",
9
"pillar",
10
]
11
})
Copied!

Custom Wallet Login

Although Moralis offers native support for MetaMask and WalletConnect, it's possible to use any Web3 provider. The scope of this guide is to demonstrate how to supply any provider.
This guide will use Tourus and Binance Smart Chain. The Tourus documentation is available at this url: https://docs.tor.us/.
The Moralis class has a method called enable. The first step is to overwrite this method in order to use a custom Moralis wallet provider class.
Import the Provider
1
<script src="https://cdn.jsdelivr.net/npm/@toruslabs/torus-embed"></script>
Copied!
Create the Torus Provider
1
class MoralisTorusProvider {
2
​
3
torus = new Torus({})
4
5
async activate() {
6
​
7
this.provider = await this.torus.init(
8
{
9
enableLogging: true,
10
network: {
11
host: "<YOUR BINANCE SPEEDY NODE>",
12
networkName: "Smart Chain - Testnet",
13
chainId: 97,
14
blockExplorer: "https://testnet.bscscan.com",
15
ticker: 'BNB',
16
tickerName: 'BNB',
17
},
18
})
19
await this.torus.login();
20
21
const MWeb3 = typeof Web3 === 'function' ? Web3 : window.Web3;
22
this.web3 = new MWeb3(this.torus.provider);
23
this.isActivated = true;
24
​
25
return this.web3;
26
}
27
}
Copied!
Create a custom "enable" function
1
const customEnable = async () => {
2
const web3Provider = new MoralisTorusProvider();
3
const web3 = await web3Provider.activate();
4
return web3;
5
}
Copied!
Set customEnable as provider
1
Moralis.setEnableWeb3(customEnable )
Copied!
You are good to go, you can now enable Moralis and connect to Web3 by:
1
window.web3 = await Moralis.enableWeb3();
Copied!
​

Non-EVM Chain Login

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

Elrond

Prerequisites: In order to log in with Elrond, you'll need a ledger that 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 authentication process.
If using the CDN, you must add the "erdjs" script to your <head>.
1
<script src="https://npmcdn.com/@elrondnetwork/[email protected]/out-browser/erdjs.js"></script>
Copied!
Now that all dependencies are in place we can authenticate:
1
Moralis.authenticate({ type: 'erd' }).then(function (user) {
2
console.log(user.get('erdAddress'))
3
})
Copied!
Important: Notice the prefix of the address has changed from eth to erd.

User Object

After the signature is completed the promise will resolve with a User object. The shape of the created user looks like this:
1
{
2
className: "_User"
3
id: String
4
_objCount: 0
5
attributes: {
6
accounts: Array,
7
ACL: Object,
8
authData: Object,
9
createdAt: Date,
10
email: String, // empty
11
ethAddress: String,
12
sessionToken: String,
13
updatedAT: Date,
14
username: String, // random value
15
}
16
}
Copied!
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 the "Objects" and "Intro" 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:
1
{
2
"objectId": String,
3
"transactions_synced": Number,
4
"ACL": ACL,
5
"last_eth_sync": Date,
6
"data": String,
7
"user": Pointer <_User>,
8
"updatedAt": Date,
9
"is_syncing": Boolean,
10
"last_eth_sync_completed": Date,
11
"signature": String,
12
"last_eth_sync_block": Number,
13
"transactions_total": Number,
14
"createdAt": Date,
15
"last_eth_sync_error": String
16
}
Copied!

Chain Specific User Properties

If you enable syncing data from chains other than Ethereum, additional user properties will be created. These will be prefixed with the chain. As an example, 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).
It's 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, then make sure to 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:
1
Moralis.authenticate({signingMessage:"hello"})
Copied!

Changing App Icon in MetaMask

It's possible to change the icon a user sees when interacting with your smart contract. To accomplish this, you'll have to add a favicon to your dApp. Follow the instructions in the MetaMask docs.
Last modified 1mo ago