- Notifications
You must be signed in to change notification settings - Fork928
Add reset user password action#1320
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes fromall commits
346e5e4
4bd7557
de39cf5
2fe1716
212020a
2699445
355f163
56b29fd
30b8f15
f6be255
5df5763
b9dbd64
69af903
d85092b
d35139c
2a9b9be
8957339
e388f8a
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Story } from "@storybook/react" | ||
import React from "react" | ||
import { MockUser } from "../../testHelpers" | ||
import { generateRandomString } from "../../util/random" | ||
import { ResetPasswordDialog, ResetPasswordDialogProps } from "./ResetPasswordDialog" | ||
export default { | ||
title: "components/ResetPasswordDialog", | ||
component: ResetPasswordDialog, | ||
argTypes: { | ||
onClose: { action: "onClose" }, | ||
onConfirm: { action: "onConfirm" }, | ||
}, | ||
} | ||
const Template: Story<ResetPasswordDialogProps> = (args: ResetPasswordDialogProps) => <ResetPasswordDialog {...args} /> | ||
export const Example = Template.bind({}) | ||
Example.args = { | ||
open: true, | ||
user: MockUser, | ||
newPassword: generateRandomString(12), | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import DialogActions from "@material-ui/core/DialogActions" | ||
import DialogContent from "@material-ui/core/DialogContent" | ||
import DialogContentText from "@material-ui/core/DialogContentText" | ||
import { makeStyles } from "@material-ui/core/styles" | ||
import React from "react" | ||
import * as TypesGen from "../../api/typesGenerated" | ||
import { CodeBlock } from "../CodeBlock/CodeBlock" | ||
import { Dialog, DialogActionButtons, DialogTitle } from "../Dialog/Dialog" | ||
export interface ResetPasswordDialogProps { | ||
open: boolean | ||
onClose: () => void | ||
onConfirm: () => void | ||
user?: TypesGen.User | ||
newPassword?: string | ||
loading: boolean | ||
} | ||
export const Language = { | ||
title: "Reset password", | ||
message: (username?: string): JSX.Element => ( | ||
<> | ||
You will need to send <strong>{username}</strong> the following password: | ||
</> | ||
), | ||
confirmText: "Reset password", | ||
} | ||
export const ResetPasswordDialog: React.FC<ResetPasswordDialogProps> = ({ | ||
open, | ||
onClose, | ||
onConfirm, | ||
user, | ||
newPassword, | ||
loading, | ||
}) => { | ||
const styles = useStyles() | ||
return ( | ||
<Dialog open={open} onClose={onClose}> | ||
<DialogTitle title={Language.title} /> | ||
<DialogContent> | ||
<DialogContentText variant="subtitle2">{Language.message(user?.username)}</DialogContentText> | ||
<DialogContentText component="div"> | ||
<CodeBlock lines={[newPassword ?? ""]} className={styles.codeBlock} /> | ||
</DialogContentText> | ||
</DialogContent> | ||
<DialogActions> | ||
<DialogActionButtons | ||
onCancel={onClose} | ||
confirmText={Language.confirmText} | ||
onConfirm={onConfirm} | ||
confirmLoading={loading} | ||
/> | ||
</DialogActions> | ||
</Dialog> | ||
) | ||
} | ||
const useStyles = makeStyles(() => ({ | ||
codeBlock: { | ||
minHeight: "auto", | ||
userSelect: "all", | ||
width: "100%", | ||
}, | ||
})) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,6 +3,7 @@ import React, { useContext, useEffect } from "react" | ||
import { useNavigate } from "react-router" | ||
import { ConfirmDialog } from "../../components/ConfirmDialog/ConfirmDialog" | ||
import { FullScreenLoader } from "../../components/Loader/FullScreenLoader" | ||
import { ResetPasswordDialog } from "../../components/ResetPasswordDialog/ResetPasswordDialog" | ||
import { XServiceContext } from "../../xServices/StateContext" | ||
import { UsersPageView } from "./UsersPageView" | ||
@@ -15,9 +16,10 @@ export const Language = { | ||
export const UsersPage: React.FC = () => { | ||
const xServices = useContext(XServiceContext) | ||
const [usersState, usersSend] = useActor(xServices.usersXService) | ||
const { users, getUsersError, userIdToSuspend, userIdToResetPassword, newUserPassword } = usersState.context | ||
const navigate = useNavigate() | ||
const userToBeSuspended = users?.find((u) => u.id === userIdToSuspend) | ||
const userToResetPassword = users?.find((u) => u.id === userIdToResetPassword) | ||
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. In the future we may want to find a way to make the XService give the component the user instead of the user id, but this is fine for now. 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. Yes, I've been wondering about that, instead of passing a userId, we could just send the full user object to make things easier but it makes it hard to sync, if we need it, in case the same user is updated by any other action. Using the ID we keep the reference to the user in the list which is always updated since it is the source of the truth. Makes sense? | ||
/** | ||
* Fetch users on component mount | ||
@@ -39,6 +41,9 @@ export const UsersPage: React.FC = () => { | ||
onSuspendUser={(user) => { | ||
usersSend({ type: "SUSPEND_USER", userId: user.id }) | ||
}} | ||
onResetUserPassword={(user) => { | ||
usersSend({ type: "RESET_USER_PASSWORD", userId: user.id }) | ||
}} | ||
error={getUsersError} | ||
/> | ||
@@ -61,6 +66,19 @@ export const UsersPage: React.FC = () => { | ||
</> | ||
} | ||
/> | ||
<ResetPasswordDialog | ||
loading={usersState.matches("resettingUserPassword")} | ||
user={userToResetPassword} | ||
newPassword={newUserPassword} | ||
open={usersState.matches("confirmUserPasswordReset")} | ||
onClose={() => { | ||
usersSend("CANCEL_USER_PASSWORD_RESET") | ||
}} | ||
onConfirm={() => { | ||
usersSend("CONFIRM_USER_PASSWORD_RESET") | ||
}} | ||
/> | ||
</> | ||
) | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* Generate a cryptographically secure random string using the specified number | ||
* of bytes then encode with base64. | ||
* | ||
* Base64 encodes 6 bits per character and pads with = so the length will not | ||
* equal the number of randomly generated bytes. | ||
* @see <https://developer.mozilla.org/en-US/docs/Glossary/Base64#encoded_size_increase> | ||
*/ | ||
export const generateRandomString = (bytes: number): string => { | ||
const byteArr = window.crypto.getRandomValues(new Uint8Array(bytes)) | ||
// The types for `map` don't seem to support mapping from one array type to | ||
// another and `String.fromCharCode.apply` wants `number[]` so loop like this | ||
// instead. | ||
const strArr: string[] = [] | ||
for (const byte of byteArr) { | ||
strArr.push(String.fromCharCode(byte)) | ||
} | ||
return btoa(strArr.join("")) | ||
} |
Uh oh!
There was an error while loading.Please reload this page.