In this update, a captcha token is introduced in the password sign-in process to improve security. A `useCaptchaToken` hook has been added to the `PasswordSignInContainer` and corresponding adjustments have been made in the `useSignInWithEmailPassword` hook. The SignInWithPasswordCredentials type is now used for credentials instead of a locally defined interface.
49 lines
1.3 KiB
TypeScript
49 lines
1.3 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 const PasswordSignInContainer: React.FC<{
|
|
onSignIn?: (userId?: string) => unknown;
|
|
}> = ({ onSignIn }) => {
|
|
const { captchaToken } = 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
|
|
}
|
|
},
|
|
[captchaToken, onSignIn, signInMutation],
|
|
);
|
|
|
|
return (
|
|
<>
|
|
<AuthErrorAlert error={signInMutation.error} />
|
|
|
|
<PasswordSignInForm onSubmit={onSubmit} loading={isLoading} />
|
|
</>
|
|
);
|
|
};
|