我不想使用普通的 firebase 身份验证方法,而是使用 web3(特别是元掩码)来提供注册/登录,而无需电子邮件和密码。问题是,我该如何处理注册?
我想到的一种方法是使用用户钱包地址作为电子邮件,然后将我的域名添加为@部分,例如:[电子邮件受保护]但问题是如何为 firebase 添加密码要使用吗?
有没有办法使用元掩码进行身份验证?
我希望我早点看到这个问题,我们为此开发了一种机制,需要一个运行
firebase-admin
的服务器,该服务器会生成自定义令牌并将其传递给客户端并使用 signInWithCustomToken
登录。
代码在这里https://github.com/novum-insights/sveltekit-unlock-firebase。
这是一个使用 sveltekit 的样板,并通过解锁协议向用户付费。
您可以使用 web3.js 从 MetaMask 获取帐户。
然后像您所说的那样创建一个 Firebase 用户
account + "@example.com"
并始终使用相同的密码,例如 123456
。
然后使用
account + "@example.com"
和密码 123456
登录。
// Get wallet account with web3.js
<script src="https://cdn.jsdelivr.net/npm/web3@latest/dist/web3.min.js"></script>
let account = null;
getWalletAccount();
async function getWalletAccount() {
if (window.ethereum) {
window.web3 = new Web3(window.ethereum);
try {
const accounts = await ethereum.request({ method: "eth_requestAccounts" });
account = accounts[0];
console.log("User's account is: " + account);
} catch (e) {
console.log("Error in eth_requestAccounts", e);
}
}
}
// Create user with Firebase
import { signInWithEmailAndPassword, createUserWithEmailAndPassword } from "https://www.gstatic.com/firebasejs/10.7.1/firebase-auth.js";
let email = account + "@example.com";
let password = "123456";
createUser();
function createUser() {
createUserWithEmailAndPassword(auth, email, password).then(cred => {
console.log(cred);
}).catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
// ..
});
}
// Sign in user with email address (ethereum account + "@example.com") and password
startAuthenticationWithEmailAndPassword();
function startAuthenticationWithEmailAndPassword() {
signInWithEmailAndPassword(auth, email, password)
.then((userCredential) => {
// Signed in
const user = userCredential.user;
// ...
console.log("Signed in with email and password");
console.log("User has uid: " + user.uid);
})
.catch((error) => {
const errorCode = error.code;
const errorMessage = error.message;
});
}