Skip to content
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

fix: remove password strength indicator for login #34

Merged
merged 1 commit into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/app/activate/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { CenteredCard } from "@/components/custom/CenteredCard";
import { CustomFormField } from "@/components/custom/CustomFormField";
import { DatePicker } from "@/components/custom/DatePicker";
import { LoadingButton } from "@/components/custom/LoadingButton";
import { PasswordInput } from "@/components/custom/PasswordInput";
import { PasswordInputWithStrength } from "@/components/custom/PasswordInputWithStrength";
import { PhoneCustomInput } from "@/components/custom/PhoneCustomInput";
import { CustomSelectTrigger } from "@/components/custom/SelectInput";
import { SuspenseConditional } from "@/components/custom/SuspenseConditional";
Expand Down Expand Up @@ -268,7 +268,7 @@ const RegisterPage = () => {
form={form}
name="password"
label="Mot de passe"
render={(field) => <PasswordInput {...field} />}
render={(field) => <PasswordInputWithStrength {...field} />}
/>
<LoadingButton
type="submit"
Expand Down
4 changes: 2 additions & 2 deletions src/app/reset-password/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { postUsersResetPassword } from "@/api";
import { CenteredCard } from "@/components/custom/CenteredCard";
import { CustomFormField } from "@/components/custom/CustomFormField";
import { LoadingButton } from "@/components/custom/LoadingButton";
import { PasswordInput } from "@/components/custom/PasswordInput";
import { PasswordInputWithStrength } from "@/components/custom/PasswordInputWithStrength";
import { SuspenseHiddenField } from "@/components/custom/SuspenseHiddenField";
import { Form } from "@/components/ui/form";
import { toast } from "@/components/ui/use-toast";
Expand Down Expand Up @@ -78,7 +78,7 @@ const ResetPasswordPage = () => {
name="password"
label="Mot de passe"
render={(field) => {
return <PasswordInput {...field} />;
return <PasswordInputWithStrength {...field} />;
}}
/>
<LoadingButton
Expand Down
68 changes: 14 additions & 54 deletions src/components/custom/PasswordInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,75 +2,35 @@

import { Input } from "@/components/ui/input";
import { cn } from "@/lib/utils";
import { zxcvbn } from "@zxcvbn-ts/core";
import * as React from "react";
import { HiEye, HiEyeOff } from "react-icons/hi";


export type InputProps = React.InputHTMLAttributes<HTMLInputElement>;

const PasswordInput = React.forwardRef<HTMLInputElement, InputProps>(
({ className, ...props }, ref) => {
const [showPassword, setShowPassword] = React.useState(false);
const [score, setScore] = React.useState(0);
const togglePasswordVisibility = () => setShowPassword(!showPassword);

const previousChange = props.onChange;

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (previousChange) {
const zxcvbnResult = zxcvbn(e.target.value);
setScore(zxcvbnResult.score);
previousChange(e);
}
};

props.onChange = onChange;

function color(strength: number, index: number, length: number): string {
if (!length) return "bg-gray-200";
if (strength < index) return "bg-gray-200";
return [
"bg-red-500",
"bg-orange-500",
"bg-amber-500",
"bg-yellow-400",
"bg-green-400",
][strength];
}

return (
<div>
<div className="relative">
<Input
type={showPassword ? "text" : "password"}
className={cn(className)}
ref={ref}
{...props}
/>
<div className="absolute inset-y-0 right-0 flex cursor-pointer items-center pr-3 text-gray-400 hover:text-[hsl(var(--ring))]">
{showPassword ? (
<HiEye className="h-4 w-4" onClick={togglePasswordVisibility} />
) : (
<HiEyeOff
className="h-4 w-4"
onClick={togglePasswordVisibility}
/>
)}
</div>
</div>
<div className="grid grid-flow-col justify-stretch gap-4 mt-4 px-1">
{[...Array(5)].map((_, index) => (
<div
key={index}
className={`px-1 h-2 rounded-xl transition-colors ${color(score, index, props.value?.toString()?.length || 0)}`}
/>
))}
<div className="relative">
<Input
type={showPassword ? "text" : "password"}
className={cn(className)}
ref={ref}
{...props}
/>
<div className="absolute inset-y-0 right-0 flex cursor-pointer items-center pr-3 text-gray-400 hover:text-[hsl(var(--ring))]">
{showPassword ? (
<HiEye className="h-4 w-4" onClick={togglePasswordVisibility} />
) : (
<HiEyeOff className="h-4 w-4" onClick={togglePasswordVisibility} />
)}
</div>
</div>
);
},
);
PasswordInput.displayName = "PasswordInput";

export { PasswordInput };
export { PasswordInput };
55 changes: 55 additions & 0 deletions src/components/custom/PasswordInputWithStrength.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"use client";

import { PasswordInput } from "./PasswordInput";
import { zxcvbn } from "@zxcvbn-ts/core";
import * as React from "react";

export type InputProps = React.InputHTMLAttributes<HTMLInputElement>;

const PasswordInputWithStrength = React.forwardRef<
HTMLInputElement,
InputProps
>(({ ...props }, ref) => {
const [score, setScore] = React.useState(0);

const previousChange = props.onChange;

const onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
if (previousChange) {
const zxcvbnResult = zxcvbn(e.target.value);
setScore(zxcvbnResult.score);
previousChange(e);
}
};

props.onChange = onChange;

function color(strength: number, index: number, length: number): string {
if (!length) return "bg-gray-200";
if (strength < index) return "bg-gray-200";
return [
"bg-red-500",
"bg-orange-500",
"bg-amber-500",
"bg-yellow-400",
"bg-green-400",
][strength];
}

return (
<div>
<PasswordInput {...props} ref={ref} />
<div className="grid grid-flow-col justify-stretch gap-4 mt-4 px-1">
{[...Array(5)].map((_, index) => (
<div
key={index}
className={`px-1 h-2 rounded-xl transition-colors ${color(score, index, props.value?.toString()?.length || 0)}`}
/>
))}
</div>
</div>
);
});
PasswordInputWithStrength.displayName = "PasswordInputWithStrength";

export { PasswordInputWithStrength };