src/examples/sign-in/Wallet.tsx

Every source

src/examples/sign-in/Wallet.tsx
'use client'

import { usePlugin, useSession } from '@synqauth/react'
import type { SolanaWallet } from '@synqauth/solana'
import { useSolanaWallets } from '@synqauth/solana/react'
import { useState } from 'react'

import { siws } from '../../components/Providers'

// Wallet sign-in in the page itself: no hosted page. useSolanaWallets finds
// the browser's wallets through the Wallet Standard; the SIWS plugin asks the
// issuer for the request, the wallet signs it here, and the app's server
// pushes the signature to Synq, which checks it. The plugin is passed to
// <SynqProvider plugins>, and its server half to synqAuth({ plugins }).
export function SignInWithWallet() {
  const { isSignedIn, user } = useSession()
  const wallets = useSolanaWallets()
  const actions = usePlugin(siws)
  const [problem, setProblem] = useState<string | null>(null)
  if (isSignedIn) return <p>Signed in as {user?.sub}</p>
  if (wallets.length === 0) return <p>No Solana wallet in this browser.</p>

  async function signIn(wallet: SolanaWallet) {
    const { error } = await actions.signIn(wallet, { returnTo: '/build/sign-in?variant=wallet' })
    if (error)
      setProblem(error.code === 'wallet_rejected' ? 'You declined in your wallet.' : error.message)
  }

  return (
    <>
      <ul aria-label="Sign in with a wallet">
        {wallets.map((wallet) => (
          <li key={wallet.name}>
            <button type="button" onClick={() => void signIn(wallet)}>
              {wallet.name}
            </button>
          </li>
        ))}
      </ul>
      {problem && <p role="alert">{problem}</p>}
    </>
  )
}