mirror of
https://github.com/ajnart/homarr.git
synced 2026-02-27 17:00:54 +01:00
feat: add password requirements (#988)
* feat: add password requirements * fix: format issue * fix: unexpected empty string in component jsx * test: adjust unit test passwords
This commit is contained in:
@@ -25,3 +25,4 @@ export {
|
||||
type BoardItemIntegration,
|
||||
type BoardItemAdvancedOptions,
|
||||
} from "./shared";
|
||||
export { passwordRequirements } from "./user";
|
||||
|
||||
@@ -1,9 +1,35 @@
|
||||
import { z } from "zod";
|
||||
|
||||
import type { TranslationObject } from "@homarr/translation";
|
||||
|
||||
import { createCustomErrorParams } from "./form/i18n";
|
||||
|
||||
const usernameSchema = z.string().min(3).max(255);
|
||||
const passwordSchema = z.string().min(8).max(255);
|
||||
|
||||
const regexCheck = (regex: RegExp) => (value: string) => regex.test(value);
|
||||
export const passwordRequirements = [
|
||||
{ check: (value) => value.length >= 8, value: "length" },
|
||||
{ check: regexCheck(/[a-z]/), value: "lowercase" },
|
||||
{ check: regexCheck(/[A-Z]/), value: "uppercase" },
|
||||
{ check: regexCheck(/\d/), value: "number" },
|
||||
{ check: regexCheck(/[$&+,:;=?@#|'<>.^*()%!-]/), value: "special" },
|
||||
] satisfies {
|
||||
check: (value: string) => boolean;
|
||||
value: keyof TranslationObject["user"]["field"]["password"]["requirement"];
|
||||
}[];
|
||||
|
||||
const passwordSchema = z
|
||||
.string()
|
||||
.min(8)
|
||||
.max(255)
|
||||
.refine(
|
||||
(value) => {
|
||||
return passwordRequirements.every((requirement) => requirement.check(value));
|
||||
},
|
||||
{
|
||||
params: createCustomErrorParams("passwordRequirements"),
|
||||
},
|
||||
);
|
||||
|
||||
const confirmPasswordRefine = [
|
||||
(data: { password: string; confirmPassword: string }) => data.password === data.confirmPassword,
|
||||
|
||||
Reference in New Issue
Block a user