import { useState } from 'react'; import { msg } from '@lingui/core/macro'; import { Trans, useLingui } from '@lingui/react/macro'; import { MailsIcon } from 'lucide-react'; import { Link, redirect, useSearchParams } from 'react-router'; import { authClient } from '@documenso/auth/client'; import { getOptionalSession } from '@documenso/auth/server/lib/utils/get-session'; import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error'; import { prisma } from '@documenso/prisma'; import { Button } from '@documenso/ui/primitives/button'; import { Checkbox } from '@documenso/ui/primitives/checkbox'; import { useToast } from '@documenso/ui/primitives/use-toast'; import { GenericErrorLayout } from '~/components/general/generic-error-layout'; import { appMetaTags } from '~/utils/meta'; import type { Route } from './+types/o.$orgUrl.signin'; export function meta() { return appMetaTags('Sign In'); } export function ErrorBoundary() { return ( Go back } secondaryButton={null} /> ); } export async function loader({ request, params }: Route.LoaderArgs) { const { isAuthenticated, user } = await getOptionalSession(request); const orgUrl = params.orgUrl; const organisation = await prisma.organisation.findFirst({ where: { url: orgUrl, }, select: { name: true, organisationClaim: true, organisationAuthenticationPortal: { select: { enabled: true, }, }, members: { select: { userId: true, }, }, }, }); if ( !organisation || !organisation.organisationAuthenticationPortal.enabled || !organisation.organisationClaim.flags.authenticationPortal ) { throw new AppError(AppErrorCode.NOT_FOUND, { message: 'Organisation not found', }); } // Redirect to organisation if already signed in and a member of the organisation. if (isAuthenticated && user && organisation.members.find((member) => member.userId === user.id)) { throw redirect(`/o/${orgUrl}`); } return { organisationName: organisation.name, orgUrl, }; } export default function OrganisationSignIn({ loaderData }: Route.ComponentProps) { const [searchParams] = useSearchParams(); const { organisationName, orgUrl } = loaderData; const { t } = useLingui(); const { toast } = useToast(); const [isSubmitting, setIsSubmitting] = useState(false); const [isConfirmationChecked, setIsConfirmationChecked] = useState(false); const action = searchParams.get('action'); const onSignInWithOIDCClick = async () => { setIsSubmitting(true); try { await authClient.oidc.org.signIn({ orgUrl, }); } catch (err) { toast({ title: t`An unknown error occurred`, description: t`We encountered an unknown error while attempting to sign you In. Please try again later.`, variant: 'destructive', }); } setIsSubmitting(false); }; if (action === 'verification-required') { return (

Confirmation email sent

To gain access to your account, please confirm your email address by clicking on the confirmation link from your inbox.

); } return (

Welcome to {organisationName}

Sign in to your account


setIsConfirmationChecked(checked === 'indeterminate' ? false : checked) } />
OR
Return to Documenso sign in page here
); }