The update includes the implementation of captcha support during the sign-in and sign-up process for user accounts. The process ensures a better level of security against bot-based attacks. Also, the code has been refactored to separate error and success alerts and unnecessary useEffect hooks have been removed. Moreover, some logic concerning the authentication rendering has been simplified.
86 lines
2.2 KiB
TypeScript
86 lines
2.2 KiB
TypeScript
'use client';
|
|
|
|
import { useCallback, useRef, useState } from 'react';
|
|
|
|
import { Check } from 'lucide-react';
|
|
|
|
import { useSignUpWithEmailAndPassword } from '@kit/supabase/hooks/use-sign-up-with-email-password';
|
|
import { Alert, AlertDescription, AlertTitle } from '@kit/ui/alert';
|
|
import { If } from '@kit/ui/if';
|
|
import { Trans } from '@kit/ui/trans';
|
|
|
|
import { AuthErrorAlert } from './auth-error-alert';
|
|
import { PasswordSignUpForm } from './password-sign-up-form';
|
|
|
|
interface EmailPasswordSignUpContainerProps {
|
|
onSignUp?: (userId?: string) => unknown;
|
|
emailRedirectTo: string;
|
|
captchaToken?: string;
|
|
}
|
|
|
|
export function EmailPasswordSignUpContainer({
|
|
onSignUp,
|
|
emailRedirectTo,
|
|
captchaToken,
|
|
}: EmailPasswordSignUpContainerProps) {
|
|
const signUpMutation = useSignUpWithEmailAndPassword();
|
|
const redirecting = useRef(false);
|
|
const loading = signUpMutation.isPending || redirecting.current;
|
|
const [showVerifyEmailAlert, setShowVerifyEmailAlert] = useState(false);
|
|
|
|
const onSignupRequested = useCallback(
|
|
async (credentials: { email: string; password: string }) => {
|
|
if (loading) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const data = await signUpMutation.mutateAsync({
|
|
...credentials,
|
|
emailRedirectTo,
|
|
captchaToken,
|
|
});
|
|
|
|
setShowVerifyEmailAlert(true);
|
|
|
|
if (onSignUp) {
|
|
onSignUp(data.user?.id);
|
|
}
|
|
} catch (error) {
|
|
console.error(error);
|
|
}
|
|
},
|
|
[emailRedirectTo, loading, onSignUp, signUpMutation],
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<If condition={showVerifyEmailAlert}>
|
|
<SuccessAlert />
|
|
</If>
|
|
|
|
<If condition={!showVerifyEmailAlert}>
|
|
<AuthErrorAlert error={signUpMutation.error} />
|
|
|
|
<PasswordSignUpForm onSubmit={onSignupRequested} loading={loading} />
|
|
</If>
|
|
</>
|
|
);
|
|
}
|
|
|
|
function SuccessAlert() {
|
|
return (
|
|
<Alert variant={'success'}>
|
|
<Check className={'w-4'} />
|
|
|
|
<AlertTitle>
|
|
<Trans i18nKey={'auth:emailConfirmationAlertHeading'} />
|
|
</AlertTitle>
|
|
|
|
<AlertDescription data-test={'email-confirmation-alert'}>
|
|
<Trans i18nKey={'auth:emailConfirmationAlertBody'} />
|
|
</AlertDescription>
|
|
</Alert>
|
|
);
|
|
}
|