Skip to content

Commit 36fe3bd

Browse files
committed
use onComplete instead of useEffect
1 parent ad11615 commit 36fe3bd

File tree

1 file changed

+44
-46
lines changed

1 file changed

+44
-46
lines changed

apps/events/src/routes/login.lazy.tsx

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import type { Identity } from '@graphprotocol/hypergraph';
22
import { Auth } from '@graphprotocol/hypergraph-react';
3-
import { usePrivy, useWallets } from '@privy-io/react-auth';
3+
import { useLogin, usePrivy, useWallets } from '@privy-io/react-auth';
44
import { createLazyFileRoute, useRouter } from '@tanstack/react-router';
55
import { Loader2 } from 'lucide-react';
6-
import { useEffect, useState } from 'react';
6+
import { useState } from 'react';
77
import { createWalletClient, custom } from 'viem';
88
import { mainnet } from 'viem/chains';
99

@@ -15,53 +15,51 @@ export const Route = createLazyFileRoute('/login')({
1515
});
1616

1717
function Login() {
18-
const { ready: privyReady, login: privyLogin, signMessage, authenticated: privyAuthenticated } = usePrivy();
19-
const { ready: walletsReady, wallets } = useWallets();
2018
const { setIdentityAndSessionToken, login: hypergraphLogin } = Auth.useHypergraphAuth();
21-
const { navigate } = useRouter();
22-
const [hypergraphLoginStarted, setHypergraphLoginStarted] = useState(false);
23-
24-
useEffect(() => {
25-
if (
26-
!hypergraphLoginStarted && // avoid re-running the effect to often
27-
privyAuthenticated && // privy must be authenticated to run it
28-
walletsReady && // wallets must be ready to run it
29-
wallets.length > 0 // wallets must have at least one wallet to run it
30-
) {
31-
setHypergraphLoginStarted(true);
32-
(async () => {
33-
try {
34-
const embeddedWallet = wallets.find((wallet) => wallet.walletClientType === 'privy') || wallets[0];
35-
const privyProvider = await embeddedWallet.getEthereumProvider();
36-
const walletClient = createWalletClient({
37-
chain: mainnet,
38-
transport: custom(privyProvider),
39-
});
19+
const { ready: privyReady, signMessage } = usePrivy();
20+
const { ready: walletsReady, wallets } = useWallets();
21+
const { login: privyLogin } = useLogin({
22+
onComplete: async ({ user }) => {
23+
try {
24+
if (!walletsReady || !wallets.length) {
25+
throw new Error('Wallets not ready');
26+
}
27+
const wallet = wallets.find((wallet) => wallet.address === user.wallet?.address);
28+
if (!wallet) {
29+
throw new Error('Embedded wallet not found');
30+
}
31+
const privyProvider = await wallet.getEthereumProvider();
32+
const walletClient = createWalletClient({
33+
chain: mainnet,
34+
transport: custom(privyProvider),
35+
});
4036

41-
const signer: Identity.Signer = {
42-
getAddress: async () => {
43-
const [address] = await walletClient.getAddresses();
44-
return address;
45-
},
46-
signMessage: async (message: string) => {
47-
if (embeddedWallet.walletClientType === 'privy') {
48-
const { signature } = await signMessage({ message });
49-
return signature;
50-
}
51-
const [address] = await walletClient.getAddresses();
52-
return await walletClient.signMessage({ account: address, message });
53-
},
54-
};
37+
const signer: Identity.Signer = {
38+
getAddress: async () => {
39+
const [address] = await walletClient.getAddresses();
40+
return address;
41+
},
42+
signMessage: async (message: string) => {
43+
if (wallet.walletClientType === 'privy') {
44+
const { signature } = await signMessage({ message });
45+
return signature;
46+
}
47+
const [address] = await walletClient.getAddresses();
48+
return await walletClient.signMessage({ account: address, message });
49+
},
50+
};
5551

56-
await hypergraphLogin(signer);
57-
navigate({ to: '/' });
58-
} catch (error) {
59-
alert('Failed to login');
60-
console.error(error);
61-
}
62-
})();
63-
}
64-
}, [hypergraphLoginStarted, walletsReady, wallets, signMessage, hypergraphLogin, navigate, privyAuthenticated]);
52+
await hypergraphLogin(signer);
53+
navigate({ to: '/' });
54+
} catch (error) {
55+
setHypergraphLoginStarted(false);
56+
alert('Failed to login');
57+
console.error(error);
58+
}
59+
},
60+
});
61+
const { navigate } = useRouter();
62+
const [hypergraphLoginStarted, setHypergraphLoginStarted] = useState(false);
6563

6664
return (
6765
<div className="flex flex-1 justify-center items-center flex-col gap-4">

0 commit comments

Comments
 (0)