Enhanced client-side password validation

This commit is contained in:
2024-06-23 14:20:36 +08:00
parent 397dc90c8e
commit 281638199c
2 changed files with 18 additions and 6 deletions

View File

@@ -18,6 +18,7 @@ const validationSchema = Yup.object({
.required("Last Name is required"),
email: Yup.string()
.trim()
.lowercase()
.min(5)
.max(69)
.email("Invalid email format")
@@ -31,6 +32,10 @@ const validationSchema = Yup.object({
.trim()
.min(8, "Password must be at least 8 characters")
.max(69, "Password must be at most 69 characters")
.matches(
/^(?=.*[a-zA-Z])(?=.*[0-9]).{8,}$/,
"Password needs to contain both letters and numbers"
)
.required("Password is required"),
terms: Yup.boolean().oneOf(
[true],
@@ -51,7 +56,7 @@ export default function App() {
const handleSubmit = async (values: any) => {
try {
const response = await axios.post(
config.serverAddress + "/users",
config.serverAddress + "/users/register",
values
);
console.log("User created successfully:", response.data);

View File

@@ -20,8 +20,15 @@ let validationSchema = yup.object({
password: yup.string().trim().max(100).required(),
});
router.post("/", async (req, res) => {
router.post("/register", async (req, res) => {
let data = req.body;
let user = await User.findOne({
where: { email: data.email },
});
if (user) {
res.status(400).json({ message: "Email already exists." });
return;
}
try {
data.id = uuidV4();
data.password = await argon2.hash(data.password);
@@ -39,7 +46,7 @@ router.post("/", async (req, res) => {
}
});
router.get("/", async (req, res) => {
router.get("/all", async (req, res) => {
let condition = {};
let search = req.query.search;
if (search) {
@@ -56,7 +63,7 @@ router.get("/", async (req, res) => {
res.json(list);
});
router.get("/:id", async (req, res) => {
router.get("/individual/:id", async (req, res) => {
let id = req.params.id;
let user = await User.findByPk(id);
if (!user) {
@@ -66,7 +73,7 @@ router.get("/:id", async (req, res) => {
res.json(user);
});
router.put("/:id", async (req, res) => {
router.put("/individual/:id", async (req, res) => {
let id = req.params.id;
let user = await User.findByPk(id);
@@ -98,7 +105,7 @@ router.put("/:id", async (req, res) => {
}
});
router.delete("/:id", async (req, res) => {
router.delete("/individual/:id", async (req, res) => {
let id = req.params.id;
let user = await User.findByPk(id);