Skip to content

Commit

Permalink
Checking username at signup
Browse files Browse the repository at this point in the history
  • Loading branch information
UO287687 committed May 5, 2024
1 parent 9390efe commit c2a5d84
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
19 changes: 14 additions & 5 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,26 @@ app.post('/users', async (req, res) => {
try {
// Check if required fields are present in the request body
validateRequiredFields(req, ['username', 'password', 'profileImage']);
// Check if the password has at least 4 characters
if (req.body.password.length < 4) {
res.status(400).json({ error: "Password must be at least 4 characters long" });
return;
}
if (req.body.username.length === 0) {
res.status(400).json({ error: "Username can't be empty" });
return;
}
if (req.body.username.length > 20) {
res.status(400).json({ error: "Username can't have more than 20 characters" });
return;
}
// Check if the user already exists
const existingUser = await User.findOne({ username: req.body.username.toString() });
if (existingUser) {
res.status(400).json({ error: "User already exist" });
return;
}
// Check if the password has at least 4 characters
if (req.body.password.length < 4) {
res.status(400).json({ error: "Password must be at least 4 characters long" });
return;
}

// Encrypt the password before saving it
const hashedPassword = await bcrypt.hash(req.body.password, 10);

Expand Down
2 changes: 2 additions & 0 deletions webapp/src/messages/messages_en.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"Passwords do not match": "Passwords do not match",
"Password must be at least 4 characters long": "Password must be at least 4 characters long",
"User already exist": "User already exist",
"Username can't be empty": "Username can't be empty",
"Username can't have more than 20 characters": "Username can't have more than 20 characters",

"menu": "Menu",
"langen": "English",
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/messages/messages_es.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"Passwords do not match": "Las contraseñas no coinciden",
"Password must be at least 4 characters long": "La contraseña debe tener al menos 4 caracteres",
"User already exist": "El usuario ya existe",
"Username can't be empty": "El nombre de usuario no puede estar vacío",
"Username can't have more than 20 characters": "El nombre de usuario no puede tener más de 20 caracteres",

"menu": "Menú",
"langen": "Inglés",
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/messages/messages_fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"Passwords do not match": "Les mots de passe ne correspondent pas",
"Password must be at least 4 characters long": "Le mot de passe doit comporter au moins 4 caractères",
"User already exist": "L'utilisateur existe déjà",
"Username can't be empty": "Le nom d'utilisateur ne peut pas être vide",
"Username can't have more than 20 characters": "Le nom d'utilisateur ne peut pas comporter plus de 20 caractères",

"menu": "Menu",
"langen": "Anglais",
Expand Down
2 changes: 2 additions & 0 deletions webapp/src/messages/messages_it.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
"Passwords do not match": "Le password non corrispondono",
"Password must be at least 4 characters long": "La password deve essere lunga almeno 4 caratteri",
"User already exist": "L'utente esiste già",
"Username can't be empty": "Il nome utente non può essere vuoto",
"Username can't have more than 20 characters": "Il nome utente non può avere più di 20 caratteri",

"menu": "Menu",
"langen": "Inglese",
Expand Down

0 comments on commit c2a5d84

Please sign in to comment.