From 549520919c482e72cdf7adae5ba852d1b6cbe5aa Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Mon, 18 Mar 2024 12:08:53 +0000 Subject: feature(web): Add the ability to change passwords --- .../dashboard/settings/ChangePassword.tsx | 132 +++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 apps/web/components/dashboard/settings/ChangePassword.tsx (limited to 'apps/web/components/dashboard/settings') diff --git a/apps/web/components/dashboard/settings/ChangePassword.tsx b/apps/web/components/dashboard/settings/ChangePassword.tsx new file mode 100644 index 00000000..d976f3e4 --- /dev/null +++ b/apps/web/components/dashboard/settings/ChangePassword.tsx @@ -0,0 +1,132 @@ +"use client"; + +import type { z } from "zod"; +import { ActionButton } from "@/components/ui/action-button"; +import { + Form, + FormControl, + FormField, + FormItem, + FormLabel, + FormMessage, +} from "@/components/ui/form"; +import { Input } from "@/components/ui/input"; +import { toast } from "@/components/ui/use-toast"; +import { api } from "@/lib/trpc"; +import { zodResolver } from "@hookform/resolvers/zod"; +import { useForm } from "react-hook-form"; + +import { zChangePasswordSchema } from "@hoarder/trpc/types/users"; + +export function ChangePassword() { + const form = useForm>({ + resolver: zodResolver(zChangePasswordSchema), + defaultValues: { + currentPassword: "", + newPassword: "", + newPasswordConfirm: "", + }, + }); + + const mutator = api.users.changePassword.useMutation({ + onSuccess: () => { + toast({ description: "Password changed successfully" }); + form.reset(); + }, + onError: (e) => { + if (e.data?.code == "UNAUTHORIZED") { + toast({ + description: "Your current password is incorrect", + variant: "destructive", + }); + } else { + toast({ description: "Something went wrong", variant: "destructive" }); + } + }, + }); + + async function onSubmit(value: z.infer) { + mutator.mutate({ + currentPassword: value.currentPassword, + newPassword: value.newPassword, + }); + } + + return ( +
+ Change Password +
+
+ + { + return ( + + Current Password + + + + + + ); + }} + /> + { + return ( + + New Password + + + + + + ); + }} + /> + { + return ( + + Confirm New Password + + + + + + ); + }} + /> + + Save + + + +
+ ); +} -- cgit v1.2.3-70-g09d2