import { createCustomer, loginCustomer } from 'lib/shopify'; import { revalidatePath } from 'next/cache'; import { cookies } from 'next/headers'; import { redirect } from 'next/navigation'; import AuthLayout from '../component/auth-layout'; import FormButton from '../component/form-button'; import FormFooter from '../component/form-footer'; import FormHeader from '../component/form-header'; let emailError: string | null = null; let passwordError: string | null = null; export default function RegisterPage() { async function handleSubmit(data: FormData) { 'use server'; const res = await createCustomer({ variables: { input: { email: data.get('email') as string, password: data.get('password') as string, }, }, }); if (res.body.data.customerCreate.customer) { const loginRes = await loginCustomer({ variables: { input: { email: data.get('email') as string, password: data.get('password') as string, }, }, }); if ( loginRes.body.data.customerAccessTokenCreate.customerAccessToken ?.accessToken ) { cookies().set({ name: 'customerAccessToken', value: loginRes.body.data.customerAccessTokenCreate.customerAccessToken .accessToken, httpOnly: true, path: '/', expires: new Date(Date.now() + 20 * 60 * 1000 + 5 * 1000), }); redirect('/account'); } redirect('/account/login'); } if (res.body.data.customerCreate.customerUserErrors.length > 0) { res.body.data.customerCreate.customerUserErrors.filter((error: any) => { if (error.field.includes('email')) { emailError = error.message; } if (error.field.includes('password')) { passwordError = error.message; } }); } revalidatePath('/account/register'); } return (
{emailError && (

{emailError}  

)}
{passwordError && (

{passwordError}  

)}
); }