-
Notifications
You must be signed in to change notification settings - Fork 0
feat(auth): password change #226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
50942ad
feat: add option to change password
Konzum59 4c2a8b9
feat: add proper validation
Konzum59 2d1b216
fix: small changes
Konzum59 051e2f6
feat: add adjustments from the comments
Konzum59 5bb7fec
fix: small review changes
Konzum59 604af96
refactor: simplify password change error logic
michalges cd14584
refactor: simplify handleServerValidationErrors types
michalges 923569d
fix: remove unnecessary styling
michalges File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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,10 @@ | ||
| import { ChangePasswordForm } from "@/features/password-change"; | ||
|
|
||
| export default function ChangePasswordPage() { | ||
| return ( | ||
| <div className="container mx-auto flex h-full flex-col items-center justify-center p-4 sm:p-8"> | ||
| <h1 className="mb-4 text-2xl font-semibold">Zmiana hasła</h1> | ||
| <ChangePasswordForm /> | ||
| </div> | ||
| ); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| import { fetchMutation } from "@/features/backend"; | ||
| import type { MessageResponse } from "@/features/backend/types"; | ||
|
|
||
| export async function changePassword({ | ||
| oldPassword, | ||
| newPassword, | ||
| newPasswordConfirm, | ||
| }: { | ||
| oldPassword: string; | ||
| newPassword: string; | ||
| newPasswordConfirm: string; | ||
| }): Promise<MessageResponse> { | ||
| const response = await fetchMutation<MessageResponse>( | ||
| "auth/change_password", | ||
| { | ||
|
michalges marked this conversation as resolved.
|
||
| method: "POST", | ||
| body: { | ||
| oldPassword, | ||
| newPassword, | ||
| newPasswordConfirm, | ||
| }, | ||
| }, | ||
| ); | ||
| return response; | ||
| } | ||
126 changes: 126 additions & 0 deletions
126
src/features/password-change/components/change-password-form.tsx
This file contains hidden or 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,126 @@ | ||
| "use client"; | ||
|
|
||
| import { zodResolver } from "@hookform/resolvers/zod"; | ||
| import { useMutation } from "@tanstack/react-query"; | ||
| import { useForm } from "react-hook-form"; | ||
| import { toast } from "sonner"; | ||
|
|
||
| import { PasswordInput } from "@/components/inputs/password-input"; | ||
| import { Button } from "@/components/ui/button"; | ||
| import { Form, FormField } from "@/components/ui/form"; | ||
| import { FetchError } from "@/features/backend"; | ||
| import { getToastMessages } from "@/lib/get-toast-messages"; | ||
|
|
||
| import { changePassword } from "../api/change-password"; | ||
| import { ChangePasswordSchema } from "../schemas/change-password-schema"; | ||
| import type { ChangePasswordFormValues } from "../schemas/change-password-schema"; | ||
|
|
||
| /** | ||
| * If a fetch request fails due to user input (incorrect old password), displays an appropriate toast message. | ||
| * @param {unknown} error - Error thrown by the fetch request. | ||
| * @returns {boolean} Whether the error was handled (whether a toast message was displayed). | ||
| */ | ||
| function handleServerValidationErrors(error: unknown): boolean { | ||
| if (!(error instanceof FetchError)) { | ||
| return false; | ||
| } | ||
|
|
||
| const validationIssues = error.errorReport?.error.validationIssues; | ||
| if (!Array.isArray(validationIssues)) { | ||
| return false; | ||
| } | ||
| for (const issue of validationIssues) { | ||
| const fieldName = issue.field ?? issue.rule; | ||
| const refersToOldPassword = | ||
| fieldName === "oldPassword" || | ||
| Object.keys(issue).includes("oldPassword") || | ||
| Object.values(issue).includes("oldPassword"); | ||
| if (refersToOldPassword) { | ||
| toast.error(getToastMessages.changePassword.invalidOldPassword); | ||
| return true; | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| export function ChangePasswordForm() { | ||
| const form = useForm<ChangePasswordFormValues>({ | ||
| resolver: zodResolver(ChangePasswordSchema), | ||
| defaultValues: { | ||
| oldPassword: "", | ||
| newPassword: "", | ||
| newPasswordConfirm: "", | ||
| }, | ||
| }); | ||
|
|
||
| const { mutateAsync, isPending } = useMutation({ | ||
| mutationFn: changePassword, | ||
| }); | ||
|
|
||
| return ( | ||
| <Form {...form}> | ||
| <form | ||
| noValidate | ||
| onSubmit={form.handleSubmit(async (data) => { | ||
| const messages = getToastMessages.changePassword; | ||
| const loadingToast = toast.loading(messages.loading); | ||
| try { | ||
| await mutateAsync(data); | ||
|
michalges marked this conversation as resolved.
|
||
| toast.success(messages.success); | ||
| form.reset(); | ||
| } catch (error: unknown) { | ||
|
michalges marked this conversation as resolved.
|
||
| const handled = handleServerValidationErrors(error); | ||
| if (!handled) { | ||
| toast.error(messages.error); | ||
| } | ||
| } finally { | ||
| toast.dismiss(loadingToast); | ||
| } | ||
| })} | ||
| className="bg-background w-full max-w-md space-y-4 rounded-xl px-6 py-8" | ||
| > | ||
| <FormField | ||
| control={form.control} | ||
| name="oldPassword" | ||
| render={({ field }) => ( | ||
| <PasswordInput | ||
| label="Aktualne hasło" | ||
| placeholder="Aktualne hasło" | ||
| {...field} | ||
| /> | ||
| )} | ||
| /> | ||
|
|
||
| <FormField | ||
| control={form.control} | ||
| name="newPassword" | ||
| render={({ field }) => ( | ||
| <PasswordInput | ||
| label="Nowe hasło" | ||
| placeholder="Nowe hasło" | ||
| {...field} | ||
| /> | ||
| )} | ||
| /> | ||
|
|
||
| <FormField | ||
| control={form.control} | ||
| name="newPasswordConfirm" | ||
| render={({ field }) => ( | ||
| <PasswordInput | ||
|
michalges marked this conversation as resolved.
|
||
| label="Potwierdź nowe hasło" | ||
| placeholder="Potwierdź nowe hasło" | ||
| {...field} | ||
| /> | ||
| )} | ||
| /> | ||
|
Konzum59 marked this conversation as resolved.
|
||
|
|
||
| <div className="flex justify-end"> | ||
| <Button type="submit" loading={isPending}> | ||
| Zmień hasło | ||
| </Button> | ||
| </div> | ||
| </form> | ||
| </Form> | ||
| ); | ||
| } | ||
|
Konzum59 marked this conversation as resolved.
|
||
This file contains hidden or 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 @@ | ||
| export { ChangePasswordForm } from "./components/change-password-form"; |
32 changes: 32 additions & 0 deletions
32
src/features/password-change/schemas/change-password-schema.ts
This file contains hidden or 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,32 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| import { FORM_ERROR_MESSAGES } from "@/data/form-error-messages"; | ||
| import { RequiredStringSchema } from "@/schemas"; | ||
|
|
||
| export const ChangePasswordSchema = z | ||
| .object({ | ||
| oldPassword: RequiredStringSchema, | ||
| newPassword: RequiredStringSchema.min(8, { | ||
| message: FORM_ERROR_MESSAGES.CHANGE_PASSWORD_MIN_LENGTH, | ||
| }) | ||
| .regex(/[A-Z]/, { | ||
| message: FORM_ERROR_MESSAGES.CHANGE_PASSWORD_REQUIRE_UPPER, | ||
| }) | ||
| .regex(/[a-z]/, { | ||
| message: FORM_ERROR_MESSAGES.CHANGE_PASSWORD_REQUIRE_LOWER, | ||
| }) | ||
| .regex(/[0-9]/, { | ||
| message: FORM_ERROR_MESSAGES.CHANGE_PASSWORD_REQUIRE_NUMBER, | ||
| }), | ||
| newPasswordConfirm: RequiredStringSchema, | ||
|
michalges marked this conversation as resolved.
|
||
| }) | ||
| .refine((data) => data.newPassword === data.newPasswordConfirm, { | ||
| message: FORM_ERROR_MESSAGES.CHANGE_PASSWORD_PASSWORDS_MUST_MATCH, | ||
| path: ["newPasswordConfirm"], | ||
| }) | ||
| .refine((data) => data.oldPassword !== data.newPassword, { | ||
| message: FORM_ERROR_MESSAGES.CHANGE_PASSWORD_MUST_DIFFER, | ||
| path: ["newPassword"], | ||
| }); | ||
|
|
||
| export type ChangePasswordFormValues = z.infer<typeof ChangePasswordSchema>; | ||
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.