Skip to content

📈 Open source, privacy-first web analytics — a cookie-less Google Analytics alternative with built-in error tracking and performance monitoring. Self-host it or use our cloud. Get started for free!

2.2 KB
Raw Blame
import { useTranslation } from 'react-i18next'
import type { LoaderFunctionArgs, MetaFunction } from 'react-router'
import { redirect, useLoaderData } from 'react-router'
import type { SitemapFunction } from 'remix-sitemap'

import { serverFetch } from '~/api/api.server'
import { getOgImageUrl, isSelfhosted } from '~/lib/constants'
import StatusPage from '~/ui/StatusPage'
import routes from '~/utils/routes'
import { getDescription, getPreviewImage, getTitle } from '~/utils/seo'

export const meta: MetaFunction = () => {
  // eslint-disable-next-line react-hooks/rules-of-hooks
  const { t } = useTranslation('common')

  return [
    ...getTitle(t('titles.verification')),
    ...getDescription(t('description.default')),
    ...getPreviewImage(
      getOgImageUrl(t('titles.verification'), t('description.default')),
    ),
  ]
}

export const sitemap: SitemapFunction = () => ({
  exclude: true,
})

export interface VerifyEmailLoaderData {
  success: boolean
  error?: string
}

export async function loader({
  request,
  params,
}: LoaderFunctionArgs): Promise<VerifyEmailLoaderData | Response> {
  if (isSelfhosted) {
    return redirect('/login', 302)
  }

  const { id } = params

  if (!id) {
    return { success: false, error: 'Invalid verification link' }
  }

  const result = await serverFetch(request, `v1/auth/verify-email/${id}`, {
    skipAuth: true,
  })

  if (result.error) {
    const errorMessage =
      typeof result.error === 'string' ? result.error : 'Verification failed'
    return { success: false, error: errorMessage }
  }

  return { success: true }
}

export default function VerifyEmailRoute() {
  const data = useLoaderData<VerifyEmailLoaderData>()

  if (data.error) {
    return (
      <StatusPage
        type='error'
        title={data.error}
        actions={[
          { label: 'Dashboard', to: routes.dashboard, primary: true },
          { label: 'Support', to: routes.contact },
        ]}
      />
    )
  }

  return (
    <StatusPage
      type='success'
      title='Email verified successfully'
      actions={[
        {
          label: 'Continue to Onboarding',
          to: routes.onboarding,
          primary: true,
        },
      ]}
    />
  )
}