Skip to content

Commit

Permalink
Merge pull request #351 from FalkorDB/fix-password-validation
Browse files Browse the repository at this point in the history
Fix #350 fix password validation
  • Loading branch information
AviAvni authored Aug 20, 2024
2 parents a3531b4 + cd30890 commit 3b06f7a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 49 deletions.
72 changes: 28 additions & 44 deletions app/settings/users/AddUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ export default function AddUser({ setUsers }: {
const [showConfirmPassword, setConfirmShowPassword] = useState(false)
const [role, setRole] = useState("")

const handelClose = () => {
setPassword("")
setConfirmPassword("")
setUsername("")
setRole("")
setConfirmShowPassword(false)
setShowPassword(false)
}

const addUser = async (e: FormEvent) => {
e.preventDefault();

Expand All @@ -30,11 +39,6 @@ export default function AddUser({ setUsers }: {
return
}

if (password !== confirmPassword) {
Toast("Passwords do not match")
return
}

const response = await securedFetch('/api/user/', {
method: 'POST',
headers: {
Expand All @@ -48,10 +52,18 @@ export default function AddUser({ setUsers }: {
setUsers(prev => [...prev, { username, role, selected: false }])
}
setOpen(false)

handelClose()
};

return (
<Dialog open={open} onOpenChange={setOpen}>
<Dialog
open={open}
onOpenChange={(o) => {
setOpen(o)
handelClose()
}}
>
<DialogTrigger asChild>
<Button
variant="Primary"
Expand All @@ -73,6 +85,7 @@ export default function AddUser({ setUsers }: {
onChange={(e) => {
setUsername(e.target.value)
e.currentTarget.setCustomValidity("")
e.currentTarget.reportValidity()
}}
onInvalid={(e) => e.currentTarget.setCustomValidity("Username is required")}
required
Expand All @@ -92,23 +105,7 @@ export default function AddUser({ setUsers }: {
onChange={(e) => {
setPassword(e.target.value)
e.currentTarget.setCustomValidity("")
if (!e.target.checkValidity()) {
if (!e.target.value) {
e.currentTarget.setCustomValidity("Password is required");
} else if (e.target.validity.patternMismatch) {
e.currentTarget.setCustomValidity(`
Password must contain:
- At least one lowercase letter
- At least one uppercase letter
- At least one digit
- At least one special character (@$!%*?&#+)
- At least 8 characters
`);
}
} else {
// If the value is valid or the input is empty, clear the custom validity message
e.currentTarget.setCustomValidity("");
}
e.currentTarget.reportValidity()
}}
value={password}
onInvalid={(e) => {
Expand All @@ -124,7 +121,6 @@ export default function AddUser({ setUsers }: {
- At least 8 characters
`);
}
e.currentTarget.reportValidity();
}}
required
/>
Expand All @@ -141,29 +137,18 @@ export default function AddUser({ setUsers }: {
variant="Small"
type={showConfirmPassword ? "text" : "password"}
onChange={(e) => {
setConfirmPassword(e.target.value)
const val = e.target.value
setConfirmPassword(val)
e.currentTarget.setCustomValidity("")
if (!e.target.checkValidity()) {
if (!e.target.value) {
e.currentTarget.setCustomValidity("Password is required");
} else if (e.target.validity.patternMismatch) {
e.currentTarget.setCustomValidity(`
Password must contain:
- At least one lowercase letter
- At least one uppercase letter
- At least one digit
- At least one special character (@$!%*?&#+)
- At least 8 characters
`);
}
} else {
// If the value is valid or the input is empty, clear the custom validity message
e.currentTarget.setCustomValidity("");
const valid = e.target.reportValidity()
if (valid && password !== val) {
e.currentTarget.setCustomValidity("password do not match")
}
}}
value={confirmPassword}
onInvalid={(e) => {
if (!e.currentTarget.value) {
const val = e.currentTarget.value
if (!val) {
e.currentTarget.setCustomValidity("Confirm Password is required");
} else if (e.currentTarget.validity.patternMismatch) {
e.currentTarget.setCustomValidity(`
Expand All @@ -175,7 +160,6 @@ export default function AddUser({ setUsers }: {
- At least 8 characters
`);
}
e.currentTarget.reportValidity();
}}
required
/>
Expand All @@ -190,6 +174,6 @@ export default function AddUser({ setUsers }: {
</div>
</form>
</DialogComponent>
</Dialog>
</Dialog >
)
}
10 changes: 5 additions & 5 deletions app/settings/users/Users.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,20 +43,20 @@ export default function Users() {
const handelSetRole = async (role: string, username?: string) => {
const updatedUsers = await Promise.all(users.map(async user => {
const updated = username ? user.username === username : user.selected

if (!updated) return user

const result = await securedFetch(`api/user/${username}/?role=${role}`, {
method: 'PATCH'
})

if (result.ok) {
return {
...user,
role
}
}

return user
}))

Expand Down Expand Up @@ -134,7 +134,7 @@ export default function Users() {
</TableCell>
<TableCell className="w-[10%]">
{
hover === username &&
hover === username && username !== "default" &&
<DeleteUser key="delete" users={[{ username, role, selected }]} setUsers={setUsers} />
}
</TableCell>
Expand Down

0 comments on commit 3b06f7a

Please sign in to comment.