Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

8.4 KB
Raw Blame
import { zodResolver } from '@hookform/resolvers/zod'
import type { AuthProvider } from '@spree/admin-sdk'
import { SpreeError } from '@spree/admin-sdk'
import { adminClient, Slot, useAuth } from '@spree/dashboard-core'
import {
  Alert,
  AlertDescription,
  AlertTitle,
  Button,
  Input,
  Label,
  Skeleton,
} from '@spree/dashboard-ui'
import { CircleAlertIcon } from '@spree/dashboard-ui/icons'
import { useQuery } from '@tanstack/react-query'
import { createFileRoute, Link, Navigate } from '@tanstack/react-router'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { AuthShell } from '../components/spree/auth-shell'
import { authCallbackErrorKey, useAuthProviders } from '../hooks/use-auth-providers'
import { type LoginFormValues, loginFormSchema } from '../schemas/auth'

/** Error code the SSO callback redirects back with when it rejects a sign-in. */
type LoginSearch = { error?: string }

export const Route = createFileRoute('/login')({
  component: LoginPage,
  validateSearch: (search: Record<string, unknown>): LoginSearch =>
    typeof search.error === 'string' ? { error: search.error } : {},
})

function LoginPage() {
  const { t } = useTranslation()
  const { isAuthenticated } = useAuth()
  const { error: callbackError } = Route.useSearch()
  const {
    passwordProvider,
    redirectProviders,
    isLoading: isLoadingProviders,
    isError: providersFailed,
  } = useAuthProviders()

  if (isAuthenticated) return <Navigate to="/" replace />

  return (
    <AuthShell>
      <div className="flex flex-col gap-2">
        <h1 className="text-2xl font-bold">{t('admin.auth.login.title')}</h1>
        <p className="text-sm text-muted-foreground">{t('admin.auth.login.subtitle')}</p>
      </div>

      {callbackError && <CallbackError code={callbackError} />}

      <SetupRequiredNotice />

      <ProviderOptions
        passwordProvider={passwordProvider}
        redirectProviders={redirectProviders}
        isLoading={isLoadingProviders}
        failed={providersFailed}
      />

      <Slot name="login.after" />
    </AuthShell>
  )
}

/**
 * A fresh install has no admin account to sign in with — point at the
 * first-run setup screen instead (the setup token from the install output is
 * still required there). Renders nothing on an already-claimed installation.
 */
function SetupRequiredNotice() {
  const { t } = useTranslation()
  const status = useQuery({
    queryKey: ['setup-status'],
    queryFn: () => adminClient.auth.setupStatus(),
    retry: false,
    // See the /setup route: never offer setup from a cached `true`.
    gcTime: 0,
    staleTime: 0,
    refetchOnMount: 'always',
    refetchOnWindowFocus: false,
  })

  if (!status.data?.setup_required) return null

  return (
    <div className="grid gap-2 rounded-md border p-4">
      <p className="font-medium text-sm">{t('admin.setup.required_notice_title')}</p>
      <p className="text-sm text-muted-foreground">{t('admin.setup.required_notice_message')}</p>
      <Link to="/setup" className="link text-sm">
        {t('admin.setup.required_notice_link')}
      </Link>
    </div>
  )
}

function ProviderOptions({
  passwordProvider,
  redirectProviders,
  isLoading,
  failed,
}: {
  passwordProvider?: AuthProvider
  redirectProviders: AuthProvider[]
  isLoading: boolean
  failed: boolean
}) {
  // Falling back to the password form would be the safer default for
  // availability, but it is the wrong one here: on an SSO-only store it renders
  // a form that cannot possibly work.
  if (failed) return <ProvidersUnavailable />
  if (isLoading) return <Skeleton className="h-40" />

  return (
    <div className="grid gap-6">
      {passwordProvider && <PasswordLoginForm />}

      {passwordProvider && redirectProviders.length > 0 && <Divider />}

      {redirectProviders.length > 0 && (
        <div className="grid gap-3">
          {redirectProviders.map((provider) => (
            <SsoButton key={provider.key} provider={provider} />
          ))}
        </div>
      )}
    </div>
  )
}

function PasswordLoginForm() {
  const { t } = useTranslation()
  const { login, isLoading } = useAuth()

  const form = useForm<LoginFormValues>({
    resolver: zodResolver(loginFormSchema),
    defaultValues: { email: '', password: '' },
  })
  const { errors } = form.formState

  const onSubmit = async (data: LoginFormValues) => {
    try {
      await login(data.email, data.password)
    } catch (err) {
      // Surface the server's message (e.g. account lockout) — bad credentials and
      // lockout share a code, so the message is the only distinguishing signal.
      // Fall back to the generic string for network/unexpected errors.
      const message =
        err instanceof SpreeError ? err.message : t('admin.validation.invalid_email_or_password')
      form.setError('root', { message })
    }
  }

  return (
    <form onSubmit={form.handleSubmit(onSubmit)}>
      <div className="grid gap-6">
        {errors.root && <p className="text-sm text-destructive">{errors.root.message}</p>}
        <div className="grid gap-2">
          <Label htmlFor="email">{t('admin.fields.email.label')}</Label>
          <Input
            id="email"
            type="email"
            placeholder={t('admin.fields.login.email.placeholder')}
            aria-invalid={!!errors.email || undefined}
            autoFocus={true}
            {...form.register('email')}
          />
          {errors.email && <p className="text-sm text-destructive">{errors.email.message}</p>}
        </div>
        {/* Password, submit and the recovery link share one grid so the link can
            sit on the label's row visually while coming last in source order.
            That makes tabbing run email → password → Sign in — the path someone
            signing in actually takes — and only then offer the way out to
            recovery. Ordering it visually rather than dropping it from the tab
            sequence keeps it reachable by keyboard. */}
        <div className="grid grid-cols-2 gap-x-2 gap-y-2">
          <Label htmlFor="password" className="col-start-1 row-start-1 self-center">
            {t('admin.fields.password.label')}
          </Label>
          <Input
            id="password"
            type="password"
            className="col-span-2 row-start-2"
            aria-invalid={!!errors.password || undefined}
            {...form.register('password')}
          />
          {errors.password && (
            <p className="col-span-2 text-sm text-destructive">{errors.password.message}</p>
          )}
          {/* Auto-placed rather than pinned to a row: naming a row would reserve
              it even when no error renders, leaving a gap above the button. */}
          <Button type="submit" className="col-span-2 mt-2 w-full" disabled={isLoading}>
            {isLoading ? t('admin.actions.signing_in') : t('admin.actions.sign_in')}
          </Button>
          <Link
            to="/forgot-password"
            className="link col-start-2 row-start-1 justify-self-end self-center text-sm"
          >
            {t('admin.auth.forgot_password.link')}
          </Link>
        </div>
      </div>
    </form>
  )
}

function SsoButton({ provider }: { provider: AuthProvider }) {
  const { t } = useTranslation()

  // A full page navigation, not a fetch: the identity provider needs to own the
  // browser for its own login screen and any MFA prompts.
  return (
    <Button
      type="button"
      variant="outline"
      className="w-full"
      onClick={() => {
        if (provider.authorization_url) window.location.href = provider.authorization_url
      }}
    >
      {t('admin.auth.login.sso_button', { provider: provider.label ?? provider.key })}
    </Button>
  )
}

function Divider() {
  const { t } = useTranslation()

  return (
    <div className="relative text-center text-sm">
      <span className="absolute inset-0 top-1/2 border-t" aria-hidden="true" />
      <span className="relative bg-muted px-2 text-muted-foreground">
        {t('admin.auth.login.divider')}
      </span>
    </div>
  )
}

function ProvidersUnavailable() {
  const { t } = useTranslation()

  return (
    <Alert variant="destructive">
      <CircleAlertIcon />
      <AlertTitle>{t('admin.auth.login.providers_error_title')}</AlertTitle>
      <AlertDescription>{t('admin.auth.login.providers_error_message')}</AlertDescription>
    </Alert>
  )
}

function CallbackError({ code }: { code: string }) {
  const { t } = useTranslation()

  return <p className="text-sm text-destructive">{t(authCallbackErrorKey(code))}</p>
}