src/examples/tour/sign-in.ts

Every source

src/examples/tour/sign-in.ts
'use client'

import { type SignInMethod, useAuth, useSignIn, useSignInMethods } from '@synqauth/react'
import type { SolanaWallet } from '@synqauth/solana'
import { useWalletSignIn } from '@synqauth/solana/react'

// Every way in, on the app's own screen (AERO-3484, the tour's first step;
// AERO-3490, every one the brand has on). A wallet signs in place with one
// signature and no page; a passkey and an emailed code sign in through
// useSignIn() on this screen too; a password and a provider (Google, X,
// Discord, Telegram…) go to Synq's page or the provider straight from here
// and come back to `returnTo`. Which buttons there are is the brand's list,
// useSignInMethods(): each way is a switch in the dashboard, and every one
// opens the same kind of account. `onSignedIn` keeps the person on this
// screen once a passkey or code signed them in, instead of loading
// `returnTo` again.

export function useEveryWayIn(returnTo: string, onSignedIn?: () => void) {
  const wallet = useWalletSignIn()
  const { passkey, emailCode, ...flow } = useSignIn({ returnTo, onSignedIn })
  const { signIn } = useAuth()
  const { data: methods } = useSignInMethods()

  return {
    wallets: wallet.wallets,
    /** The brand's ways in, in its order, with each provider's mark: null until they're read. */
    methods,
    /** One signature in the wallet, never a transaction: with several wallets, the one picked. */
    withWallet: (choice?: SolanaWallet) => wallet.signIn(choice, undefined, { returnTo }),
    withPasskey: () => passkey.signIn(),
    sendCode: (email: string) => emailCode.send(email),
    enterCode: (code: string) => emailCode.verify(code),
    /** The password on Synq's page, with the address typed here filled in. */
    withPassword: (email?: string) =>
      signIn({ method: 'password', returnTo, ...(email ? { loginHint: email } : {}) }),
    /** Straight to the provider, then back to this step. */
    withProvider: (method: SignInMethod) => signIn({ method, returnTo }),
    /** Where the emailed code stands, and what Synq refused. */
    step: flow.step,
    problem: flow.problem,
    walletProblem: wallet.problem,
    /** What only the brand's hosted page can finish (a gate, say): `continueOnHostedPage()` goes there. */
    handover: flow.handover,
    continueOnHostedPage: flow.continueOnHostedPage,
  }
}