Skip to content

Commit

Permalink
Adición comprobación nombre de usuario ya existe en register
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianSantamarina committed Mar 11, 2024
1 parent dfe361a commit 38d745e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions users/userservice/user-service.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ app.post('/adduser', async (req, res) => {
res.status(400).json({ error: error.message });
}});

app.get('/checkuser/:username', async (req, res) => {
try{
const existUser = await User.findOne({ username: req.params.username });
if(existUser){
return res.json({ exists: true });
}else{
return res.json({ exists: false });
}
} catch (error) {
res.status(400).json({ error: error.message });
}
});

const server = app.listen(port, () => {
console.log(`User Service listening at http://localhost:${port}`);
});
Expand Down
7 changes: 7 additions & 0 deletions webapp/src/components/AddUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ const AddUser = () => {
setError("Passwords don't match");
return;
}

const response = await axios.get(`${apiEndpoint}/checkuser/${username}`);
if (response.data.exists) {
setError("Username already exists");
return;
}

await axios.post(`${apiEndpoint}/adduser`, { username, password });
setOpenSnackbar(true);
} catch (error) {
Expand Down

0 comments on commit 38d745e

Please sign in to comment.