|
| 1 | +import { GoogleOAuthProvider, GoogleLogin } from "@react-oauth/google"; |
| 2 | +import * as lit from "./lit"; |
| 3 | +import { useState } from "react"; |
| 4 | + |
| 5 | +function App() { |
| 6 | + const [isAuthenticated, setIsAuthenticated] = useState(false); |
| 7 | + const [credentialResponse, setCredentialResponse] = useState<any>(null); |
| 8 | + const [pkpInfo, setPkpInfo] = useState<any>(null); |
| 9 | + |
| 10 | + const handleGoogleSuccess = (response: any) => { |
| 11 | + console.log("Google login successful:", response); |
| 12 | + setIsAuthenticated(true); |
| 13 | + setCredentialResponse(response); |
| 14 | + }; |
| 15 | + |
| 16 | + const handleGoogleError = () => { |
| 17 | + console.log("Google login failed"); |
| 18 | + setIsAuthenticated(false); |
| 19 | + setCredentialResponse(null); |
| 20 | + }; |
| 21 | + |
| 22 | + const handleMintPKP = async () => { |
| 23 | + if (!credentialResponse) { |
| 24 | + console.error("No credential response available"); |
| 25 | + return; |
| 26 | + } |
| 27 | + try { |
| 28 | + const pkp = await lit.mintPkpUsingGoogleAndLitRelayer(credentialResponse); |
| 29 | + console.log("Minted PKP:", pkp); |
| 30 | + setPkpInfo(pkp); |
| 31 | + } catch (error) { |
| 32 | + console.error("Error minting PKP:", error); |
| 33 | + setPkpInfo(null); |
| 34 | + } |
| 35 | + }; |
| 36 | + |
| 37 | + return ( |
| 38 | + <GoogleOAuthProvider clientId={import.meta.env.VITE_GOOGLE_CLIENT_ID}> |
| 39 | + <div className="card"> |
| 40 | + <hr /> |
| 41 | + <h3>Lit Relayer Google oAuth Example</h3> |
| 42 | + {!isAuthenticated ? ( |
| 43 | + <GoogleLogin |
| 44 | + onSuccess={handleGoogleSuccess} |
| 45 | + onError={handleGoogleError} |
| 46 | + /> |
| 47 | + ) : ( |
| 48 | + <> |
| 49 | + <p>Authenticated successfully!</p> |
| 50 | + <button onClick={handleMintPKP}>Mint PKP</button> |
| 51 | + {pkpInfo && ( |
| 52 | + <div> |
| 53 | + <h4>PKP Information:</h4> |
| 54 | + <p>Public Key: {pkpInfo.publicKey}</p> |
| 55 | + <p>ETH Address: {pkpInfo.ethAddress}</p> |
| 56 | + <p>Token ID: {pkpInfo.tokenId}</p> |
| 57 | + </div> |
| 58 | + )} |
| 59 | + </> |
| 60 | + )} |
| 61 | + <h5>Check the browser console!</h5> |
| 62 | + <hr /> |
| 63 | + </div> |
| 64 | + </GoogleOAuthProvider> |
| 65 | + ); |
| 66 | +} |
| 67 | + |
| 68 | +export default App; |
0 commit comments