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 #350 fix password validation #351

Merged
merged 1 commit into from
Aug 20, 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
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
Loading