'use client'; import { signIn } from 'next-auth/react'; import { usePathname, useSearchParams } from 'next/navigation'; import { useEffect, useState } from 'react'; import { ROUTES } from '@theme/routes'; import { LoginFormType, FormType, PzSignInOptions } from '@theme/types'; import { Button, Input, Link } from '@theme/components'; import { SubmitHandler, useForm } from 'react-hook-form'; import * as yup from 'yup'; import { yupResolver } from '@hookform/resolvers/yup'; import clsx from 'clsx'; import { useGetBasketQuery } from '@akinon/next/data/client/basket'; import { useCaptcha, useLocalization } from '@akinon/next/hooks'; import { AuthError } from '@akinon/next/types'; import { Image } from '@akinon/next/components/image'; import PluginModule, { Component } from '@akinon/next/components/plugin-module'; import { twMerge } from 'tailwind-merge'; const oauthProviders: Array<{ key: string; label?: string; image?: string; localeKey?: string; }> = [ { key: 'google', label: 'Google' }, { key: 'facebook', label: 'Facebook' }, { key: 'apple', label: 'Apple' } ]; const loginFormSchema = (t) => yup.object().shape({ email: yup .string() .email(t('auth.login.form.error.email_valid')) .required(t('auth.login.form.error.required')), password: yup.string().required(t('auth.login.form.error.required')) }); export const Login = ({ setActiveTab }: { setActiveTab?: (tab: string) => void; }) => { const { t, locale } = useLocalization(); const { refetch: refetchBasketData } = useGetBasketQuery(); const { register, handleSubmit, formState: { errors }, setError } = useForm({ resolver: yupResolver(loginFormSchema(t)) }); const pathname = usePathname(); const searchParams = useSearchParams(); const [formError, setFormError] = useState(null); const [isCheckout, setIsCheckout] = useState(false); const { CaptchaView, validated: captchaValidated, isVisible: isCaptchaVisible, validate: validateCaptcha } = useCaptcha(); const onSubmit: SubmitHandler = async (data) => { setFormError(null); const loginResponse = await signIn('default', { redirect: false, callbackUrl: searchParams.get('callbackUrl') ?? '/', captchaValidated, ...data, formType: FormType.login } as PzSignInOptions & { redirect: false }); if (loginResponse.error) { const errors: AuthError[] = JSON.parse(loginResponse.code); if (errors.find((error) => error.type === 'captcha')) { if (await validateCaptcha()) { onSubmit(data); } return; } const fieldErrors = errors.find((error) => error.type === 'field_errors') ?.data as { name: string; value: string[]; }[]; const nonFieldErrors = errors.find( (error) => error.type === 'non_field_errors' )?.data as string[]; fieldErrors?.forEach((item) => { let parsedValue: Record | string[] = []; if (typeof item.value === 'string') { try { parsedValue = JSON.parse(item.value); } catch { parsedValue = [item.value]; } } else { parsedValue = item.value; } if (Array.isArray(parsedValue)) { setError(item.name as keyof LoginFormType, { type: 'custom', message: parsedValue.join(', ') }); } else { Object.keys(parsedValue).forEach((key) => { const fieldName = key as keyof LoginFormType; const errorMessages = parsedValue[key] as string[]; setError(fieldName, { type: 'custom', message: errorMessages.join(', ') }); }); } }); if (nonFieldErrors?.length) { setFormError(nonFieldErrors.join(', ')); } return; } if (loginResponse.url) { window.location.href = loginResponse.url; refetchBasketData(); } }; useEffect(() => { if (pathname.includes('/orders/checkout')) { setIsCheckout(true); } }, [pathname]); return (
{t('auth.login.form.forgot_password')} {formError && (

{formError}

)}

{t('auth.login.form.or')}

{oauthProviders.map((provider) => ( ))} {isCheckout && ( )}
); };