feat: implement forgot password functionality with validation and UI components
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
'use server';
|
||||
|
||||
import { fetcher } from '@/lib/fetcher';
|
||||
import {
|
||||
forgotPasswordValidationSchema,
|
||||
ForgotPasswordValidationType,
|
||||
} from '../_validation/forgot-password-validation';
|
||||
|
||||
export async function ForgotPasswordAction(
|
||||
request: ForgotPasswordValidationType
|
||||
) {
|
||||
const validRequest = forgotPasswordValidationSchema.parse(request);
|
||||
|
||||
const { data, error } = await fetcher.POST('/v1/auth/forgot', {
|
||||
body: {
|
||||
email: validRequest.email,
|
||||
},
|
||||
});
|
||||
|
||||
console.log(data, error);
|
||||
|
||||
if (error) throw new Error(error.message);
|
||||
|
||||
return data;
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
'use client';
|
||||
|
||||
import {
|
||||
Button,
|
||||
Form,
|
||||
FormControl,
|
||||
FormField,
|
||||
FormItem,
|
||||
FormLabel,
|
||||
FormMessage,
|
||||
Input,
|
||||
} from '@components';
|
||||
import { Turnstile, TurnstileInstance } from '@marsidev/react-turnstile';
|
||||
import { useRef } from 'react';
|
||||
import { LuLoader } from 'react-icons/lu';
|
||||
import { useFormForgotPassword } from '../_hooks/use-form-forgot-password';
|
||||
import { usePostForgotPassowrd } from '../_hooks/use-post-forgot-password';
|
||||
import { ForgotPasswordValidationType } from '../_validation/forgot-password-validation';
|
||||
|
||||
export function ForgotPasswordForm() {
|
||||
const ref = useRef<TurnstileInstance | null>(null);
|
||||
|
||||
const form = useFormForgotPassword();
|
||||
const { mutate, isPending, error } = usePostForgotPassowrd(form);
|
||||
|
||||
const onSubmit = (values: ForgotPasswordValidationType) => {
|
||||
mutate(values);
|
||||
};
|
||||
|
||||
return (
|
||||
<Form {...form}>
|
||||
<form onSubmit={form.handleSubmit(onSubmit)} className="w-full space-y-4">
|
||||
{error && (
|
||||
<div className="p-2 text-xs bg-red-50 border border-red-200 text-red-800 rounded-sm">
|
||||
{(error as Error).message}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<FormField
|
||||
control={form.control}
|
||||
name="email"
|
||||
render={({ field }) => (
|
||||
<FormItem>
|
||||
<FormLabel>Email</FormLabel>
|
||||
<FormControl>
|
||||
<Input placeholder="emailmu@mail.com" {...field} />
|
||||
</FormControl>
|
||||
<FormMessage />
|
||||
</FormItem>
|
||||
)}
|
||||
/>
|
||||
|
||||
<Turnstile
|
||||
ref={ref}
|
||||
siteKey={String(process.env.NEXT_PUBLIC_TURNSTILE_SITEKEY)}
|
||||
onSuccess={(token) => form.setValue('token', token)}
|
||||
options={{
|
||||
theme: 'light',
|
||||
size: 'flexible',
|
||||
language: 'id',
|
||||
}}
|
||||
/>
|
||||
|
||||
<Button
|
||||
type="submit"
|
||||
disabled={isPending}
|
||||
className="w-full hover:bg-[#5fbaef] bg-[#22a5f1] font-bold"
|
||||
>
|
||||
{isPending ? (
|
||||
<LuLoader className="h-5 w-5 animate-spin" />
|
||||
) : (
|
||||
'Reset password'
|
||||
)}
|
||||
</Button>
|
||||
</form>
|
||||
</Form>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { zodResolver } from '@hookform/resolvers/zod';
|
||||
import { useForm } from 'react-hook-form';
|
||||
import {
|
||||
forgotPasswordValidationSchema,
|
||||
type ForgotPasswordValidationType,
|
||||
} from '../_validation/forgot-password-validation';
|
||||
|
||||
export function useFormForgotPassword() {
|
||||
return useForm<ForgotPasswordValidationType>({
|
||||
resolver: zodResolver(forgotPasswordValidationSchema),
|
||||
defaultValues: {
|
||||
email: '',
|
||||
token: '',
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { useMutation } from '@tanstack/react-query';
|
||||
import { toast } from 'sonner';
|
||||
import { ForgotPasswordAction } from '../_actions/forgot-password-action';
|
||||
import { useFormForgotPassword } from './use-form-forgot-password';
|
||||
|
||||
export function usePostForgotPassowrd(
|
||||
form: ReturnType<typeof useFormForgotPassword>
|
||||
) {
|
||||
return useMutation({
|
||||
mutationFn: ForgotPasswordAction,
|
||||
onSuccess: ({ message }) => {
|
||||
form.reset();
|
||||
toast(message);
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { z } from 'zod';
|
||||
|
||||
export const forgotPasswordValidationSchema = z.object({
|
||||
email: z.string().email({ message: 'Format email tidak valid' }),
|
||||
token: z.string(),
|
||||
});
|
||||
export type ForgotPasswordValidationType = z.infer<
|
||||
typeof forgotPasswordValidationSchema
|
||||
>;
|
||||
@@ -0,0 +1,23 @@
|
||||
import { LogoSimple } from '@/app/_components/logo';
|
||||
import { Metadata } from 'next';
|
||||
import { ForgotPasswordForm } from './_components/forgot-password-form';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'IMPHNEN - Signin',
|
||||
};
|
||||
|
||||
export default function Page() {
|
||||
return (
|
||||
<>
|
||||
<div className="flex flex-col items-center gap-4">
|
||||
<LogoSimple />
|
||||
|
||||
<p className="text-muted-foreground text-center text-sm sm:text-base">
|
||||
Reset password akunmu
|
||||
</p>
|
||||
|
||||
<ForgotPasswordForm />
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
'use client';
|
||||
|
||||
import { Toaster as Sonner } from 'sonner';
|
||||
|
||||
type ToasterProps = React.ComponentProps<typeof Sonner>;
|
||||
|
||||
const Toaster = ({ ...props }: ToasterProps) => {
|
||||
return (
|
||||
<Sonner
|
||||
theme="light"
|
||||
className="toaster group"
|
||||
toastOptions={{
|
||||
classNames: {
|
||||
toast:
|
||||
'group toast group-[.toaster]:bg-background group-[.toaster]:text-foreground group-[.toaster]:border-border group-[.toaster]:shadow-lg',
|
||||
description: 'group-[.toast]:text-muted-foreground',
|
||||
actionButton:
|
||||
'group-[.toast]:bg-primary group-[.toast]:text-primary-foreground',
|
||||
cancelButton:
|
||||
'group-[.toast]:bg-muted group-[.toast]:text-muted-foreground',
|
||||
},
|
||||
}}
|
||||
{...props}
|
||||
/>
|
||||
);
|
||||
};
|
||||
|
||||
export { Toaster };
|
||||
@@ -3,6 +3,7 @@ import '@/styles/globals.css';
|
||||
import { cn } from '@utils';
|
||||
import { type Metadata } from 'next';
|
||||
import { Providers } from './_components/providers';
|
||||
import { Toaster } from './_components/toaster';
|
||||
|
||||
export const metadata: Metadata = {
|
||||
title: 'IMPHNEN - Ingin Menjadi Programmer Handal?',
|
||||
@@ -24,6 +25,7 @@ export default function RootLayout({
|
||||
disableTransitionOnChange
|
||||
>
|
||||
{children}
|
||||
<Toaster />
|
||||
</Providers>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Reference in New Issue
Block a user