All files / src/features/authentication SignInPage.tsx

87.5% Statements 7/8
66.66% Branches 2/3
80% Functions 4/5
87.5% Lines 7/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47                                      32x   32x 20x       11x         12x           1x     1x            
// /sign-in: the email step, then the code step, in one page.
import { useState } from 'react'
import { CodeStep } from './CodeStep'
import { EmailStep } from './EmailStep'
 
type Step =
  | { name: 'email'; email: string }
  | { name: 'code'; email: string; sentAt: number }
 
interface Props {
  /** Defaults to a full page load of `/`, so it renders in the account's language. */
  onSignedIn?: () => void
}
 
export function SignInPage({
  onSignedIn = () => {
    window.location.assign('/')
  },
}: Props) {
  const [step, setStep] = useState<Step>({ name: 'email', email: '' })
 
  if (step.name === 'email') {
    return (
      <EmailStep
        defaultEmail={step.email}
        onSent={(email) => {
          setStep({ name: 'code', email, sentAt: Date.now() })
        }}
      />
    )
  }
  return (
    <CodeStep
      key={step.sentAt}
      email={step.email}
      sentAt={step.sentAt}
      onResent={(sentAt) => {
        setStep({ ...step, sentAt })
      }}
      onChangeEmail={() => {
        setStep({ name: 'email', email: step.email })
      }}
      onSignedIn={onSignedIn}
    />
  )
}