'use client' import { zodResolver } from '@hookform/resolvers/zod' import { ChevronRight } from 'lucide-react' import Link from 'next/link' import { useState } from 'react' import { useForm } from 'react-hook-form' import { BreadcrumbItem, BreadcrumbLink, BreadcrumbList, BreadcrumbPage, BreadcrumbSeparator, Button, Card, CardContent, CardFooter, Form, FormControl, FormField, Input, InputGroup, InputGroupAddon, InputGroupInput, InputGroupText, NavMenu, NavMenuItem, Switch, } from 'ui' import { Admonition } from 'ui-patterns/Admonition' import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout' import { PageBreadcrumbs } from 'ui-patterns/PageBreadcrumbs' import { PageContainer } from 'ui-patterns/PageContainer' import { PageHeader, PageHeaderDescription, PageHeaderMeta, PageHeaderSummary, PageHeaderTitle, } from 'ui-patterns/PageHeader' import { PageNav } from 'ui-patterns/PageNav' import { PageSection, PageSectionContent, PageSectionMeta, PageSectionSummary, PageSectionTitle, } from 'ui-patterns/PageSection' import * as z from 'zod' type SubPageId = 'templates' | 'smtp' const subPages: { id: SubPageId; label: string }[] = [ { id: 'templates', label: 'Templates' }, { id: 'smtp', label: 'SMTP Settings' }, ] const AUTHENTICATION_TEMPLATES = [ { title: 'Confirm signup', purpose: 'Ask users to confirm their email address after signing up', }, { title: 'Invite user', purpose: 'Invite users who do not yet have an account to join your application', }, { title: 'Magic link', purpose: 'Allow users to sign in via a one-time link sent to their email', }, { title: 'Change email address', purpose: 'Ask users to verify their new email address after changing it', }, { title: 'Reset password', purpose: 'Allow users to reset their password if they forget it', }, ] const SECURITY_TEMPLATES = [ { id: 'PASSWORD_CHANGED', title: 'Password changed', purpose: 'Notify users when their password has been changed', enabled: true, }, { id: 'EMAIL_CHANGED', title: 'Email address changed', purpose: 'Notify users when their email address has been changed', enabled: true, }, { id: 'PHONE_CHANGED', title: 'Phone number changed', purpose: 'Notify users when their phone number has been changed', enabled: false, }, ] const NotificationsFormSchema = z.object({ MAILER_NOTIFICATIONS_PASSWORD_CHANGED_ENABLED: z.boolean(), MAILER_NOTIFICATIONS_EMAIL_CHANGED_ENABLED: z.boolean(), MAILER_NOTIFICATIONS_PHONE_CHANGED_ENABLED: z.boolean(), }) const SmtpFormSchema = z.object({ ENABLE_SMTP: z.boolean(), SMTP_ADMIN_EMAIL: z.string().optional(), SMTP_SENDER_NAME: z.string().optional(), SMTP_HOST: z.string().optional(), SMTP_PORT: z.coerce.number().optional(), SMTP_MAX_FREQUENCY: z.coerce.number().optional(), SMTP_USER: z.string().optional(), }) export default function PageLayoutAuthEmails() { const [activePage, setActivePage] = useState('templates') return (
Authentication Emails {subPages.map((page) => ( ))} {activePage === 'templates' && ( setActivePage('smtp')} /> )} {activePage === 'smtp' && }
) } function TemplatesPage({ onNavigateToSmtp }: { onNavigateToSmtp: () => void }) { const notificationsForm = useForm>({ resolver: zodResolver(NotificationsFormSchema), defaultValues: { MAILER_NOTIFICATIONS_PASSWORD_CHANGED_ENABLED: true, MAILER_NOTIFICATIONS_EMAIL_CHANGED_ENABLED: true, MAILER_NOTIFICATIONS_PHONE_CHANGED_ENABLED: false, }, }) const handleSave = (values: z.infer) => { notificationsForm.reset(values) notificationsForm.clearErrors() } return ( <> Templates Configure what emails your users receive and how they are sent Set up SMTP } /> Authentication {AUTHENTICATION_TEMPLATES.map((template) => ( ))} Security
{SECURITY_TEMPLATES.map((template) => { const fieldName = `MAILER_NOTIFICATIONS_${template.id}_ENABLED` as keyof z.infer< typeof NotificationsFormSchema > return (
( )} />
) })} {notificationsForm.formState.isDirty && ( )}
) } function SmtpPage() { const form = useForm>({ resolver: zodResolver(SmtpFormSchema), defaultValues: { ENABLE_SMTP: false, SMTP_ADMIN_EMAIL: '', SMTP_SENDER_NAME: '', SMTP_HOST: '', SMTP_PORT: undefined, SMTP_MAX_FREQUENCY: 60, SMTP_USER: '', }, }) const enableSmtp = form.watch('ENABLE_SMTP') return ( <> SMTP Settings Configure a custom SMTP provider for sending authentication and security emails
undefined)}> ( )} /> {enableSmtp && ( )} {enableSmtp && ( <>

Sender details

Configure the sender information for your emails.

( )} /> ( )} />

SMTP provider settings

Your SMTP credentials will always be encrypted in our database.

( )} /> ( field.onChange(event.target.value)} placeholder="587" /> )} /> ( field.onChange(event.target.value)} /> seconds )} /> ( )} />
)} {form.formState.isDirty && }
) }