Files
myeasycms-v2/packages/features/auth/src/components/password-sign-in-container.tsx
giancarlo d7d3693f41 Refactor function components across multiple files
Function components have been refactored across the codebase. Single export-const arrow function components have been adapted into traditional function declarations. This change provides better stack trace in case of errors and better function and argument names on runtime debugging.
2024-05-05 13:31:40 +07:00

53 lines
1.4 KiB
TypeScript

'use client';
import { useCallback } from 'react';
import type { z } from 'zod';
import { useSignInWithEmailPassword } from '@kit/supabase/hooks/use-sign-in-with-email-password';
import { useCaptchaToken } from '../captcha/client';
import type { PasswordSignInSchema } from '../schemas/password-sign-in.schema';
import { AuthErrorAlert } from './auth-error-alert';
import { PasswordSignInForm } from './password-sign-in-form';
export function PasswordSignInContainer({
onSignIn,
}: {
onSignIn?: (userId?: string) => unknown;
}) {
const { captchaToken, resetCaptchaToken } = useCaptchaToken();
const signInMutation = useSignInWithEmailPassword();
const isLoading = signInMutation.isPending;
const onSubmit = useCallback(
async (credentials: z.infer<typeof PasswordSignInSchema>) => {
try {
const data = await signInMutation.mutateAsync({
...credentials,
options: { captchaToken },
});
if (onSignIn) {
const userId = data?.user?.id;
onSignIn(userId);
}
} catch (e) {
// wrong credentials, do nothing
} finally {
resetCaptchaToken();
}
},
[captchaToken, onSignIn, resetCaptchaToken, signInMutation],
);
return (
<>
<AuthErrorAlert error={signInMutation.error} />
<PasswordSignInForm onSubmit={onSubmit} loading={isLoading} />
</>
);
}