-
Notifications
You must be signed in to change notification settings - Fork 8
restructure authentication #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,110 @@ | ||
import { Button } from '@/components/ui/button'; | ||
import { availableAccounts } from '@/lib/availableAccounts'; | ||
import { Identity } from '@graphprotocol/hypergraph'; | ||
import { usePrivy } from '@privy-io/react-auth'; | ||
import { Link, createLazyFileRoute } from '@tanstack/react-router'; | ||
import { usePrivy, useWallets } from '@privy-io/react-auth'; | ||
import { createLazyFileRoute, useRouter } from '@tanstack/react-router'; | ||
import { Loader2 } from 'lucide-react'; | ||
import { useEffect, useState } from 'react'; | ||
import { createWalletClient, custom } from 'viem'; | ||
import { mainnet } from 'viem/chains'; | ||
|
||
export const Route = createLazyFileRoute('/login')({ | ||
component: () => <Login />, | ||
}); | ||
|
||
function Login() { | ||
const { ready, authenticated, login } = usePrivy(); | ||
// Disable login when Privy is not ready or the user is already authenticated | ||
const disableLogin = !ready || (ready && authenticated); | ||
const { setIdentityAndSessionToken, authenticated: graphAuthenticated } = Identity.useGraphLogin(); | ||
const { ready: privyReady, login: privyLogin, signMessage, authenticated: privyAuthenticated } = usePrivy(); | ||
const { ready: walletsReady, wallets } = useWallets(); | ||
const { setIdentityAndSessionToken, login: hypergraphLogin } = Identity.useGraphLogin(); | ||
const { navigate } = useRouter(); | ||
const [hypergraphLoginStarted, setHypergraphLoginStarted] = useState(false); | ||
|
||
useEffect(() => { | ||
if ( | ||
!hypergraphLoginStarted && // avoid re-running the effect to often | ||
privyAuthenticated && // privy must be authenticated to run it | ||
walletsReady && // wallets must be ready to run it | ||
wallets.length > 0 // wallets must have at least one wallet to run it | ||
) { | ||
setHypergraphLoginStarted(true); | ||
(async () => { | ||
try { | ||
const embeddedWallet = wallets.find((wallet) => wallet.walletClientType === 'privy') || wallets[0]; | ||
const privyProvider = await embeddedWallet.getEthereumProvider(); | ||
const walletClient = createWalletClient({ | ||
chain: mainnet, | ||
transport: custom(privyProvider), | ||
}); | ||
|
||
const signer: Identity.Signer = { | ||
getAddress: async () => { | ||
const [address] = await walletClient.getAddresses(); | ||
return address; | ||
}, | ||
signMessage: async (message: string) => { | ||
if (embeddedWallet.walletClientType === 'privy') { | ||
const { signature } = await signMessage({ message }); | ||
return signature; | ||
} | ||
const [address] = await walletClient.getAddresses(); | ||
return await walletClient.signMessage({ account: address, message }); | ||
}, | ||
}; | ||
|
||
await hypergraphLogin(signer); | ||
navigate({ to: '/' }); | ||
} catch (error) { | ||
alert('Failed to login'); | ||
console.error(error); | ||
} | ||
})(); | ||
} | ||
}, [hypergraphLoginStarted, walletsReady, wallets, signMessage, hypergraphLogin, navigate, privyAuthenticated]); | ||
|
||
return ( | ||
<div className="flex flex-1 justify-center items-center flex-col gap-4"> | ||
{(!ready || !authenticated) && !graphAuthenticated ? ( | ||
<div> | ||
<Button | ||
disabled={!privyReady || hypergraphLoginStarted} | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
privyLogin({}); | ||
}} | ||
> | ||
Log in {hypergraphLoginStarted && <Loader2 className="ml-2 w-4 h-4 animate-spin" />} | ||
</Button> | ||
|
||
<div> | ||
<Button disabled={disableLogin} onClick={login}> | ||
Log in | ||
<h1>Choose account</h1> | ||
<Button | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
setIdentityAndSessionToken(availableAccounts[0]); | ||
navigate({ to: '/' }); | ||
}} | ||
> | ||
{availableAccounts[0].accountId.substring(0, 6)} | ||
</Button> | ||
<Button | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
setIdentityAndSessionToken(availableAccounts[1]); | ||
navigate({ to: '/' }); | ||
}} | ||
> | ||
{availableAccounts[1].accountId.substring(0, 6)} | ||
</Button> | ||
<Button | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
setIdentityAndSessionToken(availableAccounts[2]); | ||
navigate({ to: '/' }); | ||
}} | ||
> | ||
{availableAccounts[2].accountId.substring(0, 6)} | ||
</Button> | ||
|
||
<div> | ||
<h1>Choose account</h1> | ||
<Button | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
setIdentityAndSessionToken(availableAccounts[0]); | ||
}} | ||
> | ||
{availableAccounts[0].accountId.substring(0, 6)} | ||
</Button> | ||
<Button | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
setIdentityAndSessionToken(availableAccounts[1]); | ||
}} | ||
> | ||
{availableAccounts[1].accountId.substring(0, 6)} | ||
</Button> | ||
<Button | ||
onClick={(event) => { | ||
event.preventDefault(); | ||
setIdentityAndSessionToken(availableAccounts[2]); | ||
}} | ||
> | ||
{availableAccounts[2].accountId.substring(0, 6)} | ||
</Button> | ||
</div> | ||
</div> | ||
) : ( | ||
<Link className="text-blue-500 hover:text-blue-600 underline" to="/"> | ||
Go to Home | ||
</Link> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.