Skip to content

Commit

Permalink
Now we can add a user to the system
Browse files Browse the repository at this point in the history
There are some TODOs left for validating the data
  • Loading branch information
Mister-Mario committed Mar 23, 2024
1 parent 6f8d800 commit 9ffed27
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 26 deletions.
3 changes: 2 additions & 1 deletion users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ mongoose.connect(mongoUri);

// Function to validate required fields in the request body
function validateRequiredFields(req, requiredFields) {
//TODO: Add more validations
for (const field of requiredFields) {
if (!(field in req.body)) {
throw new Error(`Missing required field: ${field}`);
Expand All @@ -40,7 +41,7 @@ app.post('/adduser', async (req, res) => {
});

await newUser.save();
res.json(newUser);
res.json({username: newUser.username});
} catch (error) {
res.status(400).json({ error: error.message });
}});
Expand Down
86 changes: 61 additions & 25 deletions webapp/src/components/loginAndRegistration/AddUser.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,80 @@
import React from "react";
import { FaUser, FaLock } from "react-icons/fa";
import "../../custom.css";
import { Link } from "react-router-dom";
import { useTranslation } from "react-i18next";
import axios from 'axios';
import { useState } from 'react';

const AddUser = () => {
const apiUrl = (process.env.REACT_APP_API_ENDPOINT || 'http://localhost:8000') + "/adduser";
const { t } = useTranslation("global");
const [username, setUsername] = useState('');
const [password, setPassword] = useState('');
const [repeatPassword, setRepeatPassword] = useState('');

return (
<div className="wrapper">
<form action="">
<div className="wrapper2">
<h1>{t("addUser.title")}</h1>
<div className="input-box">
<input type="text" placeholder={t("addUser.username_placeholder")} required />
<FaUser className="icon" />
</div>
<div className="input-box">
<input type="password" placeholder={t("addUser.password_placeholder")} required />
<FaLock className="icon" />
</div>
<div className="input-box">
<input type="password" placeholder={t("addUser.repeat_password_placeholder")} required />
<FaLock className="icon" />
</div>
const handleSubmit = async (event) => {
event.preventDefault();
try {
//TODO: Add more validations
if(password === repeatPassword){ //User put the same password
const response = await axios.post(apiUrl, { username, password });
console.log("Registered user: " + response.username);
}
else{
//TODO: Show some errors to the user
}

<button type="submit">{t("addUser.register_button")}</button>
} catch (error) {
console.error('Error adding user:', error);
}
};

<LinkLogin />
</div>
</form>
return (
<div className="general">
<div className="card">
<div className="card2">
<form className="form" onSubmit={handleSubmit}>
<h1>{t("addUser.title")}</h1>
<div className="input-box">
<input
type="text"
placeholder={t("addUser.username_placeholder")}
required
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>
<div className="input-box">
<input
type="password"
placeholder={t("addUser.password_placeholder")}
required
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<div className="input-box">
<input
type="password"
placeholder={t("addUser.repeat_password_placeholder")}
required
value={repeatPassword}
onChange={(e) => setRepeatPassword(e.target.value)}
/>
</div>
<button type="submit">{t("addUser.register_button")}</button>
<LinkLogin />
</form>
</div>
</div>
</div>
);
};

function LinkLogin() {
const { t } = useTranslation("global");
return (
<Link to="/login" className="button-login" variant="body2" >
<Link to="/login" className="button-login" variant="body2">
{t("addUser.login_link")}
</Link>
);
Expand Down Expand Up @@ -94,5 +131,4 @@ export default AddUser;
// )}
// </Container>
// );
// };

// };

0 comments on commit 9ffed27

Please sign in to comment.