src/examples/tour/lounge.ts

Every source

src/examples/tour/lounge.ts
'use client'

import {
  type SynqError,
  browserSessionStorage,
  createSynqClient,
  signInEndingOf,
} from '@synqauth/js'

// The Lounge (AERO-3484, the tour's third step) is its own app in the
// brand, and the brand's token gate guards it: Synq reads the chain at every
// sign-in and refresh, so the app holds no RPC key and runs no chain code.
// It's a public client in the browser: signing in to it, the brand's page
// lets a holder straight through and tells anyone else what the gate takes,
// with a way to add the wallet that holds it. Back here, the sign-in either
// worked or ended at the gate.

export function loungeClient(issuer: string, clientId: string) {
  const redirectUri = `${location.origin}/tour/lounge`
  const storage = browserSessionStorage()
  const lounge = createSynqClient({ issuer, clientId, redirectUri, storage })
  return lounge
}

/** Whether the person is in, after Synq sent the browser back: `open`, `locked`, or another ending. */
export async function loungeEntry(lounge: ReturnType<typeof loungeClient>) {
  const entry = await lounge.handleCallback(location.href).then(
    () => null,
    (e: SynqError) => e,
  )
  const locked = signInEndingOf(entry?.description) === 'token_gate_refused'
  return entry === null ? 'open' : locked ? 'locked' : entry
}