了解如何使用 B3 Global Accounts 实现身份验证
import { SignInWithB3 } from "@b3dotfun/sdk/global-account/react";
function GoogleAuth() {
return (
<SignInWithB3
provider={{ strategy: "google" }}
partnerId="your-partner-id"
onLoginSuccess={(globalAccount) => {
console.log("Google auth successful:", globalAccount);
}}
onLoginError={(error) => {
console.error("Authentication failed:", error);
}}
/>
);
}
import { SignInWithB3 } from "@b3dotfun/sdk/global-account/react";
function DiscordAuth() {
return (
<SignInWithB3
provider={{ strategy: "discord" }}
partnerId="your-partner-id"
onLoginSuccess={(globalAccount) => {
console.log("Discord auth successful:", globalAccount);
}}
/>
);
}
import { SignInWithB3 } from "@b3dotfun/sdk/global-account/react";
const b3Chain = {
id: 8333,
name: "B3",
nativeCurrency: { name: "Ether", symbol: "ETH", decimals: 18 },
rpc: "https://mainnet-rpc.b3.fun",
};
function SessionKeyAuth() {
return (
<SignInWithB3
provider={{ strategy: "google" }}
chain={b3Chain}
partnerId="your-partner-id"
sessionKeyAddress="0x..." // MetaMask 或其他钱包地址
onLoginSuccess={(globalAccount) => {
console.log("Session key authenticated:", globalAccount);
}}
/>
);
}
function AdvancedSessionAuth() {
const sessionKeyConfig = {
permissions: {
approvedTargets: ["0x..."], // 特定合约地址
startDate: new Date(),
endDate: new Date(Date.now() + 30 * 24 * 60 * 60 * 1000), // 30 天
nativeTokenLimitPerTransaction: 0.0001, // 每笔交易的 ETH 限额
}
};
return (
<SignInWithB3
provider={{ strategy: "google" }}
chain={b3Chain}
partnerId="your-partner-id"
sessionKeyAddress="0x..."
sessionKeyConfig={sessionKeyConfig}
onLoginSuccess={(globalAccount) => {
console.log("Advanced session key setup:", globalAccount);
}}
/>
);
}
import { authenticate } from "@b3dotfun/sdk/global-account/app";
async function authenticateUser(accessToken: string, identityToken: string) {
try {
const authResult = await authenticate(accessToken, identityToken, {
// 额外配置
});
if (authResult) {
console.log("Authentication successful:", authResult);
return authResult;
} else {
console.log("Authentication failed");
return null;
}
} catch (error) {
console.error("Authentication error:", error);
throw error;
}
}
// 对于 React Native 应用程序
import { authenticate } from "@b3dotfun/sdk/global-account/app";
async function authenticateInReactNative() {
const result = await authenticate("access-token", "identity-token");
return result;
}
import { useB3 } from "@b3dotfun/sdk/global-account/react";
function AuthStatus() {
const {
account,
isAuthenticated,
isLoading,
signOut
} = useB3();
if (isLoading) return <div>Loading...</div>;
return (
<div>
{isAuthenticated ? (
<div>
<p>Welcome, {account?.displayName}!</p>
<button onClick={signOut}>Sign Out</button>
</div>
) : (
<p>Please sign in</p>
)}
</div>
);
}
import { useAccountWallet } from "@b3dotfun/sdk/global-account/react";
function WalletInfo() {
const { wallet, isConnected } = useAccountWallet();
return (
<div>
{isConnected && (
<div>
<p>Wallet Address: {wallet?.address}</p>
<p>Chain ID: {wallet?.chainId}</p>
</div>
)}
</div>
);
}
function AuthWithErrorHandling() {
const [authError, setAuthError] = useState<string | null>(null);
return (
<div>
<SignInWithB3
provider={{ strategy: "google" }}
partnerId="your-partner-id"
onLoginSuccess={(globalAccount) => {
setAuthError(null);
console.log("Success:", globalAccount);
}}
onLoginError={(error) => {
setAuthError(error.message);
console.error("Auth error:", error);
}}
/>
{authError && (
<div className="error">
Authentication failed: {authError}
</div>
)}
</div>
);
}