- Create LoginPromptModal component for authentication encouragement - Check customer authentication before adding items to cart - Show modal for non-logged-in users with options to create account, log in, or continue as guest - Allow guests to add items (stored in IndexedDB) while prompting to sign up - Implemented in both StandardProductView (ready-made products) and DesignCanvas (personalized items) - Add server action checkCustomerAuth() for authentication checks Addresses user request: prompt guests to create account or log in when adding items to cart. Co-Authored-By: Claude Haiku 4.5 <noreply@anthropic.com>
39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
'use server';
|
|
|
|
import { headers } from 'next/headers';
|
|
import { prisma } from '@/lib/prisma';
|
|
import { upsertMailingListEntry } from '@/lib/mailingList';
|
|
import { verifyTurnstileToken } from '@/lib/turnstile';
|
|
import { getClientIp } from '@/lib/rateLimit';
|
|
import { getCurrentCustomer } from '@/lib/auth';
|
|
|
|
const EMAIL_RE = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
|
|
export async function subscribeToNewsletter(email: string, turnstileToken: string) {
|
|
const trimmed = email.trim().toLowerCase();
|
|
if (!EMAIL_RE.test(trimmed)) {
|
|
return { ok: false, message: 'Enter a valid email address.' };
|
|
}
|
|
|
|
const ip = getClientIp(headers());
|
|
const { success } = await verifyTurnstileToken(turnstileToken, ip);
|
|
if (!success) {
|
|
return { ok: false, message: 'CAPTCHA verification failed — please try again.' };
|
|
}
|
|
|
|
await prisma.newsletterSubscriber.upsert({
|
|
where: { email: trimmed },
|
|
create: { email: trimmed },
|
|
update: {},
|
|
});
|
|
|
|
await upsertMailingListEntry({ email: trimmed, source: 'NEWSLETTER' });
|
|
|
|
return { ok: true, message: "Thanks — you're on the list." };
|
|
}
|
|
|
|
export async function checkCustomerAuth() {
|
|
const customer = await getCurrentCustomer();
|
|
return { isAuthenticated: !!customer };
|
|
}
|