-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add password requirements (#988)
* feat: add password requirements * fix: format issue * fix: unexpected empty string in component jsx * test: adjust unit test passwords
- Loading branch information
1 parent
0585187
commit 2d155fa
Showing
14 changed files
with
198 additions
and
37 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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
packages/ui/src/components/password-input/password-input.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,35 @@ | ||
"use client"; | ||
|
||
import type { ChangeEvent } from "react"; | ||
import { useState } from "react"; | ||
import { PasswordInput } from "@mantine/core"; | ||
import type { PasswordInputProps } from "@mantine/core"; | ||
|
||
import { PasswordRequirementsPopover } from "./password-requirements-popover"; | ||
|
||
interface CustomPasswordInputProps extends PasswordInputProps { | ||
withPasswordRequirements?: boolean; | ||
} | ||
|
||
export const CustomPasswordInput = ({ withPasswordRequirements, ...props }: CustomPasswordInputProps) => { | ||
if (withPasswordRequirements) { | ||
return <WithPasswordRequirements {...props} />; | ||
} | ||
|
||
return <PasswordInput {...props} />; | ||
}; | ||
|
||
const WithPasswordRequirements = (props: PasswordInputProps) => { | ||
const [value, setValue] = useState(""); | ||
|
||
const onChange = (event: ChangeEvent<HTMLInputElement>) => { | ||
setValue(event.currentTarget.value); | ||
props.onChange?.(event); | ||
}; | ||
|
||
return ( | ||
<PasswordRequirementsPopover password={value}> | ||
<PasswordInput {...props} onChange={onChange} /> | ||
</PasswordRequirementsPopover> | ||
); | ||
}; |
17 changes: 17 additions & 0 deletions
17
packages/ui/src/components/password-input/password-requirement.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,17 @@ | ||
import { rem, Text } from "@mantine/core"; | ||
import { IconCheck, IconX } from "@tabler/icons-react"; | ||
|
||
export function PasswordRequirement({ meets, label }: { meets: boolean; label: string }) { | ||
return ( | ||
<Text c={meets ? "teal" : "red"} display="flex" style={{ alignItems: "center" }} size="sm"> | ||
{meets ? ( | ||
<IconCheck style={{ width: rem(14), height: rem(14) }} /> | ||
) : ( | ||
<IconX style={{ width: rem(14), height: rem(14) }} /> | ||
)} | ||
<Text span ml={10}> | ||
{label} | ||
</Text> | ||
</Text> | ||
); | ||
} |
49 changes: 49 additions & 0 deletions
49
packages/ui/src/components/password-input/password-requirements-popover.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,49 @@ | ||
import type { PropsWithChildren } from "react"; | ||
import { useState } from "react"; | ||
import { Popover, Progress } from "@mantine/core"; | ||
|
||
import { useScopedI18n } from "@homarr/translation/client"; | ||
import { passwordRequirements } from "@homarr/validation"; | ||
|
||
import { PasswordRequirement } from "./password-requirement"; | ||
|
||
export const PasswordRequirementsPopover = ({ password, children }: PropsWithChildren<{ password: string }>) => { | ||
const requirements = useRequirements(); | ||
const strength = useStrength(password); | ||
const [popoverOpened, setPopoverOpened] = useState(false); | ||
const checks = ( | ||
<> | ||
{requirements.map((requirement) => ( | ||
<PasswordRequirement key={requirement.label} label={requirement.label} meets={requirement.check(password)} /> | ||
))} | ||
</> | ||
); | ||
|
||
const color = strength === 100 ? "teal" : strength > 50 ? "yellow" : "red"; | ||
|
||
return ( | ||
<Popover opened={popoverOpened} position="bottom" width="target" transitionProps={{ transition: "pop" }}> | ||
<Popover.Target> | ||
<div onFocusCapture={() => setPopoverOpened(true)} onBlurCapture={() => setPopoverOpened(false)}> | ||
{children} | ||
</div> | ||
</Popover.Target> | ||
<Popover.Dropdown> | ||
<Progress color={color} value={strength} size={5} mb="xs" /> | ||
{checks} | ||
</Popover.Dropdown> | ||
</Popover> | ||
); | ||
}; | ||
|
||
const useRequirements = () => { | ||
const t = useScopedI18n("user.field.password.requirement"); | ||
|
||
return passwordRequirements.map(({ check, value }) => ({ check, label: t(value) })); | ||
}; | ||
|
||
function useStrength(password: string) { | ||
const requirements = useRequirements(); | ||
|
||
return (100 / requirements.length) * requirements.filter(({ check }) => check(password)).length; | ||
} |
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
Oops, something went wrong.