-
Notifications
You must be signed in to change notification settings - Fork 887
feat: add user password change page #1866
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
3 commits
Select commit
Hold shift + click to select a range
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
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
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
109 changes: 109 additions & 0 deletions
109
site/src/components/SettingsSecurityForm/SettingsSecurityForm.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,109 @@ | ||||||||
import FormHelperText from "@material-ui/core/FormHelperText" | ||||||||
import TextField from "@material-ui/core/TextField" | ||||||||
import { FormikContextType, FormikErrors, useFormik } from "formik" | ||||||||
import React from "react" | ||||||||
import * as Yup from "yup" | ||||||||
import { getFormHelpers, onChangeTrimmed } from "../../util/formUtils" | ||||||||
import { LoadingButton } from "../LoadingButton/LoadingButton" | ||||||||
import { Stack } from "../Stack/Stack" | ||||||||
|
||||||||
interface SecurityFormValues { | ||||||||
old_password: string | ||||||||
password: string | ||||||||
confirm_password: string | ||||||||
} | ||||||||
|
||||||||
export const Language = { | ||||||||
oldPasswordLabel: "Old Password", | ||||||||
newPasswordLabel: "New Password", | ||||||||
confirmPasswordLabel: "Confirm Password", | ||||||||
oldPasswordRequired: "Old password is required", | ||||||||
newPasswordRequired: "New password is required", | ||||||||
confirmPasswordRequired: "Password confirmation is required", | ||||||||
passwordMinLength: "Password must be at least 8 characters", | ||||||||
passwordMaxLength: "Password must be no more than 64 characters", | ||||||||
confirmPasswordMatch: "Password and confirmation must match", | ||||||||
updatePassword: "Update password", | ||||||||
} | ||||||||
|
||||||||
const validationSchema = Yup.object({ | ||||||||
old_password: Yup.string().trim().required(Language.oldPasswordRequired), | ||||||||
password: Yup.string() | ||||||||
.trim() | ||||||||
.min(8, Language.passwordMinLength) | ||||||||
.max(64, Language.passwordMaxLength) | ||||||||
.required(Language.newPasswordRequired), | ||||||||
confirm_password: Yup.string() | ||||||||
.trim() | ||||||||
.test("passwords-match", Language.confirmPasswordMatch, function (value) { | ||||||||
return (this.parent as SecurityFormValues).password === value | ||||||||
}), | ||||||||
}) | ||||||||
|
||||||||
export type SecurityFormErrors = FormikErrors<SecurityFormValues> | ||||||||
export interface SecurityFormProps { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
isLoading: boolean | ||||||||
initialValues: SecurityFormValues | ||||||||
onSubmit: (values: SecurityFormValues) => void | ||||||||
formErrors?: SecurityFormErrors | ||||||||
error?: string | ||||||||
} | ||||||||
|
||||||||
export const SecurityForm: React.FC<SecurityFormProps> = ({ | ||||||||
isLoading, | ||||||||
onSubmit, | ||||||||
initialValues, | ||||||||
formErrors = {}, | ||||||||
error, | ||||||||
}) => { | ||||||||
const form: FormikContextType<SecurityFormValues> = useFormik<SecurityFormValues>({ | ||||||||
initialValues, | ||||||||
validationSchema, | ||||||||
onSubmit, | ||||||||
}) | ||||||||
const getFieldHelpers = getFormHelpers<SecurityFormValues>(form, formErrors) | ||||||||
|
||||||||
return ( | ||||||||
<> | ||||||||
<form onSubmit={form.handleSubmit}> | ||||||||
<Stack> | ||||||||
<TextField | ||||||||
{...getFieldHelpers("old_password")} | ||||||||
onChange={onChangeTrimmed(form)} | ||||||||
autoComplete="old_password" | ||||||||
fullWidth | ||||||||
label={Language.oldPasswordLabel} | ||||||||
variant="outlined" | ||||||||
type="password" | ||||||||
/> | ||||||||
<TextField | ||||||||
{...getFieldHelpers("password")} | ||||||||
onChange={onChangeTrimmed(form)} | ||||||||
autoComplete="password" | ||||||||
fullWidth | ||||||||
label={Language.newPasswordLabel} | ||||||||
variant="outlined" | ||||||||
type="password" | ||||||||
/> | ||||||||
<TextField | ||||||||
{...getFieldHelpers("confirm_password")} | ||||||||
onChange={onChangeTrimmed(form)} | ||||||||
autoComplete="confirm_password" | ||||||||
fullWidth | ||||||||
label={Language.confirmPasswordLabel} | ||||||||
variant="outlined" | ||||||||
type="password" | ||||||||
/> | ||||||||
|
||||||||
{error && <FormHelperText error>{error}</FormHelperText>} | ||||||||
|
||||||||
<div> | ||||||||
<LoadingButton loading={isLoading} type="submit" variant="contained"> | ||||||||
{isLoading ? "" : Language.updatePassword} | ||||||||
</LoadingButton> | ||||||||
</div> | ||||||||
</Stack> | ||||||||
</form> | ||||||||
</> | ||||||||
) | ||||||||
} |
12 changes: 0 additions & 12 deletions
12
site/src/pages/SettingsPages/AccountPage/LinkedAccountsPage.tsx
This file was deleted.
Oops, something went wrong.
100 changes: 100 additions & 0 deletions
100
site/src/pages/SettingsPages/SecurityPage/SecurityPage.test.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,100 @@ | ||
import { fireEvent, screen, waitFor } from "@testing-library/react" | ||
import React from "react" | ||
import * as API from "../../../api/api" | ||
import { GlobalSnackbar } from "../../../components/GlobalSnackbar/GlobalSnackbar" | ||
import * as SecurityForm from "../../../components/SettingsSecurityForm/SettingsSecurityForm" | ||
import { renderWithAuth } from "../../../testHelpers/renderHelpers" | ||
import * as AuthXService from "../../../xServices/auth/authXService" | ||
import { Language, SecurityPage } from "./SecurityPage" | ||
|
||
const renderPage = () => { | ||
return renderWithAuth( | ||
<> | ||
<SecurityPage /> | ||
<GlobalSnackbar /> | ||
</>, | ||
) | ||
} | ||
|
||
const newData = { | ||
old_password: "password1", | ||
password: "password2", | ||
confirm_password: "password2", | ||
} | ||
|
||
const fillAndSubmitForm = async () => { | ||
await waitFor(() => screen.findByLabelText("Old Password")) | ||
fireEvent.change(screen.getByLabelText("Old Password"), { target: { value: newData.old_password } }) | ||
fireEvent.change(screen.getByLabelText("New Password"), { target: { value: newData.password } }) | ||
fireEvent.change(screen.getByLabelText("Confirm Password"), { target: { value: newData.confirm_password } }) | ||
fireEvent.click(screen.getByText(SecurityForm.Language.updatePassword)) | ||
} | ||
|
||
describe("SecurityPage", () => { | ||
describe("when it is a success", () => { | ||
it("shows the success message", async () => { | ||
jest.spyOn(API, "updateUserPassword").mockImplementationOnce((_userId, _data) => Promise.resolve(undefined)) | ||
const { user } = renderPage() | ||
await fillAndSubmitForm() | ||
|
||
const successMessage = await screen.findByText(AuthXService.Language.successSecurityUpdate) | ||
expect(successMessage).toBeDefined() | ||
expect(API.updateUserPassword).toBeCalledTimes(1) | ||
expect(API.updateUserPassword).toBeCalledWith(user.id, newData) | ||
}) | ||
}) | ||
|
||
describe("when the old_password is incorrect", () => { | ||
it("shows an error", async () => { | ||
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({ | ||
isAxiosError: true, | ||
response: { | ||
data: { message: "Incorrect password.", errors: [{ detail: "Incorrect password.", field: "old_password" }] }, | ||
}, | ||
}) | ||
|
||
const { user } = renderPage() | ||
await fillAndSubmitForm() | ||
|
||
const errorMessage = await screen.findByText("Incorrect password.") | ||
expect(errorMessage).toBeDefined() | ||
expect(API.updateUserPassword).toBeCalledTimes(1) | ||
expect(API.updateUserPassword).toBeCalledWith(user.id, newData) | ||
}) | ||
}) | ||
|
||
describe("when the password is invalid", () => { | ||
it("shows an error", async () => { | ||
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({ | ||
isAxiosError: true, | ||
response: { | ||
data: { message: "Invalid password.", errors: [{ detail: "Invalid password.", field: "password" }] }, | ||
}, | ||
}) | ||
|
||
const { user } = renderPage() | ||
await fillAndSubmitForm() | ||
|
||
const errorMessage = await screen.findByText("Invalid password.") | ||
expect(errorMessage).toBeDefined() | ||
expect(API.updateUserPassword).toBeCalledTimes(1) | ||
expect(API.updateUserPassword).toBeCalledWith(user.id, newData) | ||
}) | ||
}) | ||
|
||
describe("when it is an unknown error", () => { | ||
it("shows a generic error message", async () => { | ||
jest.spyOn(API, "updateUserPassword").mockRejectedValueOnce({ | ||
data: "unknown error", | ||
}) | ||
|
||
const { user } = renderPage() | ||
await fillAndSubmitForm() | ||
|
||
const errorMessage = await screen.findByText(Language.unknownError) | ||
expect(errorMessage).toBeDefined() | ||
expect(API.updateUserPassword).toBeCalledTimes(1) | ||
expect(API.updateUserPassword).toBeCalledWith(user.id, newData) | ||
}) | ||
}) | ||
}) |
44 changes: 44 additions & 0 deletions
44
site/src/pages/SettingsPages/SecurityPage/SecurityPage.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,44 @@ | ||
import { useActor } from "@xstate/react" | ||
import React, { useContext } from "react" | ||
import { isApiError, mapApiErrorToFieldErrors } from "../../../api/errors" | ||
import { Section } from "../../../components/Section/Section" | ||
import { SecurityForm } from "../../../components/SettingsSecurityForm/SettingsSecurityForm" | ||
import { XServiceContext } from "../../../xServices/StateContext" | ||
|
||
export const Language = { | ||
title: "Security", | ||
unknownError: "Oops, an unknown error occurred.", | ||
} | ||
|
||
export const SecurityPage: React.FC = () => { | ||
const xServices = useContext(XServiceContext) | ||
const [authState, authSend] = useActor(xServices.authXService) | ||
const { me, updateSecurityError } = authState.context | ||
const hasError = !!updateSecurityError | ||
const formErrors = | ||
hasError && isApiError(updateSecurityError) | ||
? mapApiErrorToFieldErrors(updateSecurityError.response.data) | ||
: undefined | ||
const hasUnknownError = hasError && !isApiError(updateSecurityError) | ||
|
||
if (!me) { | ||
throw new Error("No current user found") | ||
} | ||
|
||
return ( | ||
<Section title={Language.title}> | ||
<SecurityForm | ||
error={hasUnknownError ? Language.unknownError : undefined} | ||
formErrors={formErrors} | ||
isLoading={authState.matches("signedIn.security.updatingSecurity")} | ||
initialValues={{ old_password: "", password: "", confirm_password: "" }} | ||
onSubmit={(data) => { | ||
authSend({ | ||
type: "UPDATE_SECURITY", | ||
data, | ||
}) | ||
}} | ||
/> | ||
</Section> | ||
) | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Praise: Nice typing!