集成Moralis和Web3Auth
Moralis支持使用Web3Auth进行身份验证。这允许用户通过社交登录和web3钱包进行登录。
1.创建一个Web3Auth帐户
要开始,请在此处创建一个帐户并获取可发布的clientId
。
clientId: 'ABC*****************'
2.添加Web3AuthSDK
根据moralis导入项目的方式导入SDK–CDN
、npm
或yarn
。
<script src="https://unpkg.com/@web3auth/web3auth@latest/dist/web3auth.umd.min.js" rel="external nofollow" ></script>
npm install --save @web3auth/web3auth
yarn add @web3auth/web3auth
3.调用鉴权函数
你可以选择使用JS
或者React
const user = await Moralis.authenticate({provider: "web3Auth",clientId: "ABC*****************",})
import { useMoralis } from "react-moralis";function App() { const { authenticate, isAuthenticated, user } = useMoralis(); const login = async () => { if (!isAuthenticated) { await authenticate({provider: "web3Auth",clientId: "ABC*****************",}) .onSuccess: (user) => { console.log(user!.get("ethAddress")); } .catch(function (error) { console.log(error); }); } }}
然后像上面一样调用身份验证,但使用provider
选项和所需的参数。clientId
是唯一必需的参数。
参数
使用web3Auth
提供程序时可以传递给Moralis.authenticate()
的参数
参数 | 值 |
clientId | 来自 web3Auth 仪表板的可发布 clientId。 |
chainId | (可选)要连接的受支持网络的chainId。 默认情况下以太坊主网 0x1 |
appLogo | (可选)logo 的 URL 将显示在模态框的顶部。 默认情况下 Moralis 徽标 |
loginMethodsOrder | (可选)一个字符串数组,其中包含您要允许的社交登录以及它们出现的顺序。 默认值:[“google”、”facebook”、”twitter”、”reddit”、”discord”、”twitch”、”apple”、”line”、”github”、”kakao”、”linkedin”、”weibo” , “微信”, “email_passwordless”] |
theme | (可选)登录模式的主题。 可以是浅色或深色之一。 默认暗 |