BASE_URL="https://build.wield.xyz"
const getSigninMessage = async (address) => { const response = await fetch( `${BASE_URL}/auth/v1/get-account-signin-message?address=${address}&chainId=1`, { method: "GET", headers: { "Content-Type": "application/json", "API-KEY": "your-api-key", }, } ); const data = await response.json(); return data.signature; // This is the message to sign };
const authenticateWithSignature = async (signature, address) => { const response = await fetch(`${BASE_URL}/auth/v2/authenticate`, { method: "POST", headers: { "Content-Type": "application/json", "API-KEY": "your-api-key", }, body: JSON.stringify({ signature, address, chainId: 1, type: "SIGNATURE", // Specify signature authentication }), }); const data = await response.json(); return data.accessToken; };
const authenticateWithFID = async (signature, address, signerId) => { const response = await fetch(`${BASE_URL}/auth/v2/authenticate`, { method: "POST", headers: { "Content-Type": "application/json", "API-KEY": "your-api-key", }, body: JSON.stringify({ signature, address, chainId: 1, id: signerId, // Your signer ID registered on Farcaster type: "FID", // Specify FID authentication }), }); const data = await response.json(); return data.accessToken; };
// Here's a complete example using ethers.js: const authenticateUser = async (wallet, type = "signature") => { try { // Step 1: Get the address const address = await wallet.getAddress(); // Step 2: Get message to sign const message = await getSigninMessage(address); // Step 3: Sign the message const signature = await wallet.signMessage(message); // Step 4: Authenticate if (type === "FID") { return await authenticateWithFID(signature, address, YOUR_SIGNER_ID); } else { return await authenticateWithSignature(signature, address); } } catch (error) { console.error("Authentication failed:", error); throw error; } };
const makeAuthenticatedRequest = async (accessToken) => { const response = await fetch(`${BASE_URL}/your/protected/endpoint`, { method: "POST", headers: { "Content-Type": "application/json", "API-KEY": "your-api-key", Authorization: `Bearer ${accessToken}`, }, body: JSON.stringify({ // Your request data }), }); return await response.json(); };