Just change your dappUrl in metamask-auth.jsx.
Demo: metamask-auth.ilamanov.repl.co
Demo code: replit.com/@ilamanov/metamask-auth
Code walkthrough: Build a React Component For MetaMask Auth
Just change your dappUrl in metamask-auth.jsx.
Demo: metamask-auth.ilamanov.repl.co
Demo code: replit.com/@ilamanov/metamask-auth
Code walkthrough: Build a React Component For MetaMask Auth
| import React, { useEffect, useState } from "react"; | |
| import styles from "./metamask-auth.module.css"; | |
| function isMobileDevice() { | |
| return 'ontouchstart' in window || 'onmsgesturechange' in window; | |
| } | |
| async function connect(onConnected) { | |
| if (!window.ethereum) { | |
| alert("Get MetaMask!"); | |
| return; | |
| } | |
| const accounts = await window.ethereum.request({ | |
| method: "eth_requestAccounts", | |
| }); | |
| onConnected(accounts[0]); | |
| } | |
| async function checkIfWalletIsConnected(onConnected) { | |
| if (window.ethereum) { | |
| const accounts = await window.ethereum.request({ | |
| method: "eth_accounts", | |
| }); | |
| if (accounts.length > 0) { | |
| const account = accounts[0]; | |
| onConnected(account); | |
| return; | |
| } | |
| if (isMobileDevice()) { | |
| await connect(onConnected); | |
| } | |
| } | |
| } | |
| export default function MetaMaskAuth({ onAddressChanged }) { | |
| const [userAddress, setUserAddress] = useState(""); | |
| useEffect(() => { | |
| checkIfWalletIsConnected(setUserAddress); | |
| }, []); | |
| useEffect(() => { | |
| onAddressChanged(userAddress); | |
| }, [userAddress]); | |
| return userAddress ? ( | |
| <div> | |
| Connected with <Address userAddress={userAddress} /> | |
| </div> | |
| ) : ( | |
| <Connect setUserAddress={setUserAddress}/> | |
| ); | |
| } | |
| function Connect({ setUserAddress }) { | |
| if (isMobileDevice()) { | |
| const dappUrl = "metamask-auth.ilamanov.repl.co"; // TODO enter your dapp URL. For example: https://uniswap.exchange. (don't enter the "https://") | |
| const metamaskAppDeepLink = "https://metamask.app.link/dapp/" + dappUrl; | |
| return ( | |
| <a href={metamaskAppDeepLink}> | |
| <button className={styles.button}> | |
| Connect to MetaMask | |
| </button> | |
| </a> | |
| ); | |
| } | |
| return ( | |
| <button className={styles.button} onClick={() => connect(setUserAddress)}> | |
| Connect to MetaMask | |
| </button> | |
| ); | |
| } | |
| function Address({ userAddress }) { | |
| return ( | |
| <span className={styles.address}>{userAddress.substring(0, 5)}…{userAddress.substring(userAddress.length - 4)}</span> | |
| ); | |
| } |
| .button { | |
| color: white; | |
| background: -webkit-linear-gradient(left, #e5771b, #753d16); | |
| background-size: 200% 200%; | |
| border: 1px solid #dbdbdb; | |
| border-radius: 4px; | |
| border-width: 1px; | |
| padding: 13px; | |
| cursor: pointer; | |
| font-weight: 600; | |
| font-size: 1.2em; | |
| } | |
| .button:hover { | |
| background: -webkit-linear-gradient(left, #e5771b, #e5771b); | |
| } | |
| .address { | |
| background-color: #e5771b; | |
| color: black; | |
| padding: 5px; | |
| border-radius: 5px; | |
| border: none; | |
| } |