-
-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature(web): Add the ability to change passwords
- Loading branch information
1 parent
60467f1
commit 5495209
Showing
4 changed files
with
182 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
apps/web/components/dashboard/settings/ChangePassword.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<z.infer<typeof zChangePasswordSchema>>({ | ||
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<typeof zChangePasswordSchema>) { | ||
mutator.mutate({ | ||
currentPassword: value.currentPassword, | ||
newPassword: value.newPassword, | ||
}); | ||
} | ||
|
||
return ( | ||
<div className="w-full pt-4"> | ||
<span className="text-xl">Change Password</span> | ||
<hr className="my-2" /> | ||
<Form {...form}> | ||
<form | ||
onSubmit={form.handleSubmit(onSubmit)} | ||
className="flex w-1/2 flex-col gap-2 pt-4" | ||
> | ||
<FormField | ||
control={form.control} | ||
name="currentPassword" | ||
render={({ field }) => { | ||
return ( | ||
<FormItem className="flex-1"> | ||
<FormLabel>Current Password</FormLabel> | ||
<FormControl> | ||
<Input | ||
type="password" | ||
placeholder="Current Password" | ||
{...field} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
); | ||
}} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="newPassword" | ||
render={({ field }) => { | ||
return ( | ||
<FormItem className="flex-1"> | ||
<FormLabel>New Password</FormLabel> | ||
<FormControl> | ||
<Input | ||
type="password" | ||
placeholder="New Password" | ||
{...field} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
); | ||
}} | ||
/> | ||
<FormField | ||
control={form.control} | ||
name="newPasswordConfirm" | ||
render={({ field }) => { | ||
return ( | ||
<FormItem className="flex-1"> | ||
<FormLabel>Confirm New Password</FormLabel> | ||
<FormControl> | ||
<Input | ||
type="Password" | ||
placeholder="Confirm New Password" | ||
{...field} | ||
/> | ||
</FormControl> | ||
<FormMessage /> | ||
</FormItem> | ||
); | ||
}} | ||
/> | ||
<ActionButton | ||
className="h-full" | ||
type="submit" | ||
loading={mutator.isPending} | ||
> | ||
Save | ||
</ActionButton> | ||
</form> | ||
</Form> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,26 @@ | ||
import { z } from "zod"; | ||
|
||
const PASSWORD_MAX_LENGTH = 100; | ||
|
||
export const zSignUpSchema = z | ||
.object({ | ||
name: z.string().min(1, { message: "Name can't be empty" }), | ||
email: z.string().email(), | ||
password: z.string().min(8), | ||
password: z.string().min(8).max(PASSWORD_MAX_LENGTH), | ||
confirmPassword: z.string(), | ||
}) | ||
.refine((data) => data.password === data.confirmPassword, { | ||
message: "Passwords don't match", | ||
path: ["confirmPassword"], | ||
}); | ||
|
||
export const zChangePasswordSchema = z | ||
.object({ | ||
currentPassword: z.string(), | ||
newPassword: z.string().min(8).max(PASSWORD_MAX_LENGTH), | ||
newPasswordConfirm: z.string(), | ||
}) | ||
.refine((data) => data.newPassword === data.newPasswordConfirm, { | ||
message: "Passwords don't match", | ||
path: ["newPasswordConfirm"], | ||
}); |