Skip to content

Commit

Permalink
doctor forgot password completed
Browse files Browse the repository at this point in the history
  • Loading branch information
sinanptm committed Sep 11, 2024
1 parent 01c23c8 commit 886c3c0
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 78 deletions.
10 changes: 5 additions & 5 deletions client/app/(patient)/signin/reset-password/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ export default function ResetPasswordPage() {
const route = useRouter();
const { mutate: updatePassword, isPending } = useUpdatePassword();
const { control, handleSubmit } = form;
const { otpMail, setCredentials } = useAuth();
const { resetMailPatient, setCredentials } = useAuth();
const onSubmit = async (values: FormValues) => {
updatePassword(
{
newPassword: values.newPassword,
email: otpMail,
email: resetMailPatient,
},
{
onSuccess: () => {
toast({
title: "Password Updated ✅",
description: "Password Updated Successfully",
});
route.push("/signin");
setTimeout(() => {
route.push("/signin");
setCredentials("otpMail", "");
setCredentials("resetMailPatient", "");
}, 2000);
},
onError: (error) => {
Expand All @@ -78,7 +78,7 @@ export default function ResetPasswordPage() {
);
};

if (otpMail) {
if (resetMailPatient) {
return (
<div className="container max-w-md mx-auto mt-48 remove-scrollbar">
<Card>
Expand Down
121 changes: 121 additions & 0 deletions client/app/doctor/@auth/reset-password/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
"use client";

import { useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { Card, CardHeader, CardTitle, CardDescription, CardContent, CardFooter } from "@/components/ui/card";
import CustomFormField from "@/components/common/CustomFormField";
import { FormFieldType } from "@/types/fromTypes";
import SubmitButton from "@/components/common/SubmitButton";
import { useUpdatePassword } from "@/lib/hooks/patient/usePatientAuth";
import { Form } from "@/components/ui/form";
import { useAuth } from "@/lib/hooks/useAuth";
import { notFound, useRouter } from "next/navigation";
import { toast } from "@/components/ui/use-toast";
import { useUpdatePasswordDoctor } from "@/lib/hooks/doctor/useDoctorAuth";

const formSchema = z
.object({
newPassword: z
.string()
.trim()
.min(6, "Password must be at least 6 characters long")
.max(25, "Password must be at most 25 characters long")
.regex(/[A-Z]/, "Password must contain at least one uppercase letter")
.regex(/[a-z]/, "Password must contain at least one lowercase letter")
.regex(/[0-9]/, "Password must contain at least one number")
.regex(/[@$!%*?&#]/, "Password must contain at least one special character"),
confirmPassword: z.string(),
})
.superRefine(({ newPassword, confirmPassword }, ctx) => {
if (newPassword !== confirmPassword) {
ctx.addIssue({
code: "custom",
message: "The passwords do not match",
path: ["confirmPassword"],
});
}
});

type FormValues = z.infer<typeof formSchema>;

export default function ResetPasswordPage() {
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: {
newPassword: "",
confirmPassword: "",
},
});
const route = useRouter();
const { mutate: updatePassword, isPending } = useUpdatePasswordDoctor();
const { control, handleSubmit } = form;
const { resetMailDoctor, setCredentials } = useAuth();
const onSubmit = async (values: FormValues) => {
updatePassword(
{
password: values.newPassword,
email: resetMailDoctor,
},
{
onSuccess: () => {
toast({
title: "Password Updated ✅",
description: "Password Updated Successfully",
});
route.push("/doctor");
setTimeout(() => {
setCredentials("resetMailDoctor", "");
}, 2000);
},
onError: (error) => {
toast({
title: "Updating Failed ❌",
description: error.response?.data.message || "Please try again later.",
variant: "destructive",
});
},
}
);
};

if (resetMailDoctor) {
return (
<div className="container max-w-md mx-auto mt-48 remove-scrollbar">
<Card>
<CardHeader>
<CardTitle>Reset Password</CardTitle>
<CardDescription>Change your account password here.</CardDescription>
</CardHeader>
<Form {...form}>
<form onSubmit={handleSubmit(onSubmit)}>
<CardContent className="space-y-4">
<CustomFormField
control={control}
fieldType={FormFieldType.PASSWORD}
name="newPassword"
placeholder="Your new password"
label="New Password"
/>
<CustomFormField
control={control}
fieldType={FormFieldType.PASSWORD}
name="confirmPassword"
placeholder="Confirm your new password"
label="Confirm New Password"
/>
</CardContent>
<CardFooter>
<SubmitButton isLoading={isPending} variant={"secondary"}>
Update Password
</SubmitButton>
</CardFooter>
</form>
</Form>
</Card>
</div>
);
};

notFound();
}
2 changes: 1 addition & 1 deletion client/app/not-found.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default function NotFound() {
<h2 className="text-3xl font-semibold mb-4">Page Not Found</h2>
<p className="text-xl mb-8">Oops! The page you&apos;re looking for doesn&apos;t exist.</p>
<Button className="bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded-lg transition-colors duration-300">
<Link href={"/doctor"}> Go Home</Link>
<Link href={"/"}> Go Home</Link>
</Button>
</div>
</div>
Expand Down
60 changes: 34 additions & 26 deletions client/components/forms/doctor/ForgetPasswordForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ import { Form } from "@/components/ui/form";
import SubmitButton from "../../common/SubmitButton";
import CustomFormField from "../../common/CustomFormField";
import { FormFieldType } from "@/types/fromTypes";
import { useForgetPassword } from "@/lib/hooks/patient/usePatientAuth";
import { toast } from "../../ui/use-toast";
import { useAuth } from "@/lib/hooks/useAuth";
import { useForgotPasswordDoctor } from "@/lib/hooks/doctor/useDoctorAuth";

type Props = {
isOpen: boolean;
Expand All @@ -43,33 +43,41 @@ const ForgotPasswordModalDoctor = ({ isOpen, setIsOpen }: Props) => {
control,
handleSubmit,
} = form;
const { mutate: forgetPassword,isPending } = useForgetPassword();
const {setCredentials} = useAuth()
const { mutate: forgetPassword, isPending } = useForgotPasswordDoctor();
const { setCredentials } = useAuth()
const onSubmit = async (data: FormValues) => {
forgetPassword(
{ email: data.email },
{
onSuccess: () => {
toast({
title: "Email Sended 📩",
description: "Please Check Your Email for further instructions",
variant: "success",
});
setIsOpen(false)
setCredentials("otpMail",data.email);
form.reset();
},
onError:(error)=>{
if(error.status===404){
toast({
title:"Invalid Email Address",
description:"Please Verify Your Email Address",
variant:"destructive"
});
}
if (isOpen) {
forgetPassword(
{ email: data.email },
{
onSuccess: () => {
toast({
title: "Email Sended 📩",
description: "Please Check Your Email for further instructions",
variant: "success",
});
setIsOpen(false)
setCredentials("resetMailDoctor", data.email);
form.reset();
},
onError: (error) => {
if (error.status === 404) {
toast({
title: "Invalid Email Address",
description: "Please Verify Your Email Address",
variant: "destructive"
});
} else {
toast({
title: "Action Failed",
description: error.response?.data.message || "Unknown Error Occurred",
variant: "destructive"
});
}
}
}
}
);
);
}
};

return (
Expand Down
74 changes: 38 additions & 36 deletions client/components/forms/doctor/SigninForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -64,46 +64,48 @@ const AdminSigninForm = () => {
};

return (
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-6 flex-1"
>
<section className="mb-12 space-y-4">
<h1 className="header">Doctor Signin</h1>
<p className="text-dark-700"> Don&apos;t have an account?{" "}
<Link href={'/doctor/signup'} className="text-sky-600">Signup</Link>
</p>
</section>
<>
<Form {...form}>
<form
onSubmit={form.handleSubmit(onSubmit)}
className="space-y-6 flex-1"
>
<section className="mb-12 space-y-4">
<h1 className="header">Doctor Signin</h1>
<p className="text-dark-700"> Don&apos;t have an account?{" "}
<Link href={'/doctor/signup'} className="text-sky-600">Signup</Link>
</p>
</section>

<CustomFormField
fieldType={FormFieldType.INPUT}
control={form.control}
name="email"
label="Email address *"
placeholder="[email protected]"
iconSrc={"/assets/icons/email.svg"}
/>
<CustomFormField
fieldType={FormFieldType.INPUT}
control={form.control}
name="email"
label="Email address *"
placeholder="[email protected]"
iconSrc={"/assets/icons/email.svg"}
/>

<CustomFormField
control={form.control}
fieldType={FormFieldType.PASSWORD}
name="password"
label="Password *"
placeholder="Enter your password"
/>
<p
className="text-dark-700 text-sm mt-2 cursor-pointer"
onClick={() => setForgetPasswordModelOpen(!isForgetPasswordModelOpen)}>
Forget Password?
</p>
<CustomFormField
control={form.control}
fieldType={FormFieldType.PASSWORD}
name="password"
label="Password *"
placeholder="Enter your password"
/>
<p
className="text-dark-700 text-sm mt-2 cursor-pointer"
onClick={() => setForgetPasswordModelOpen(!isForgetPasswordModelOpen)}>
Forget Password?
</p>

<FormMessage className="shad-error" />
<FormMessage className="shad-error" />

<SubmitButton isLoading={isPending}>Sign In</SubmitButton>
<ForgotPasswordModalDoctor isOpen={isForgetPasswordModelOpen} setIsOpen={setForgetPasswordModelOpen} />
</form>
</Form>
<SubmitButton isLoading={isPending}>Sign In</SubmitButton>
</form>
</Form>
<ForgotPasswordModalDoctor isOpen={isForgetPasswordModelOpen} setIsOpen={setForgetPasswordModelOpen} />
</>
);
};

Expand Down
2 changes: 1 addition & 1 deletion client/components/forms/patient/SigninForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ const LoginForm = () => {
description: "Please check your email for the OTP.",
variant: "default",
});
router.push("/signin/otp-verification");
setCredentials("otpMail", email);
router.push("/signin/otp-verification");
},
onError(error) {
setError(error.response?.data.message || "An error occurred during sign-in.");
Expand Down
2 changes: 1 addition & 1 deletion client/components/models/patient/ForgetPasswordModel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const ForgotPasswordModal = ({ isOpen, setIsOpen }: Props) => {
variant: "success",
});
setIsOpen(false)
setCredentials("otpMail",data.email);
setCredentials("resetMailPatient",data.email);
form.reset();
},
onError:(error)=>{
Expand Down
2 changes: 1 addition & 1 deletion client/lib/api/doctor/authenticationRoutes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@ export const forgotPasswordDoctor = async (email: string) => {
};

export const updatePasswordDoctor = async (email: string, password: string) => {
const response = await axiosInstance.post("/update-password", { email, password });
const response = await axiosInstance.patch("/update-password", { email, password });
return response.data;
};
4 changes: 4 additions & 0 deletions client/lib/providers/auth-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ interface AuthState {
otpMail: string;
otpMailAdmin: string;
otpMailDoctor: string;
resetMailDoctor:string;
resetMailPatient:string;
}

interface AuthContextProps extends AuthState {
Expand All @@ -26,6 +28,8 @@ const initialState: AuthState = {
otpMail: persistedAuthState.otpMail || "",
otpMailAdmin: persistedAuthState.otpMailAdmin || "",
otpMailDoctor: persistedAuthState.otpMailDoctor || "",
resetMailDoctor: persistedAuthState.resetMailDoctor || "",
resetMailPatient: persistedAuthState.resetMailPatient || "",
};

export const AuthContext = createContext<AuthContextProps | undefined>(undefined);
Expand Down
3 changes: 1 addition & 2 deletions client/lib/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}


export const getDefault = (value: any, fallback: string) => {
return value ? value : fallback;
};
};
Loading

1 comment on commit 886c3c0

@vercel
Copy link

@vercel vercel bot commented on 886c3c0 Sep 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.