diff --git a/backend/src/app.py b/backend/src/app.py index 3614406..c4b6d57 100644 --- a/backend/src/app.py +++ b/backend/src/app.py @@ -138,16 +138,24 @@ def register(): req = flask.request.get_json(force=True) username = req.get('username', None) password = req.get('password', None) - new_user = User( - username=username, - password=guard.hash_password(password) - ) - db.session.add(new_user) - db.session.commit() - ret = {'message': 'Account erstellt für den Account {}'.format( - new_user.username - )} - return ret, 200 + + get_list_by_username = User.query.filter_by(username=username).first() + if get_list_by_username is None: + new_user = User( + username=username, + password=guard.hash_password(password) + ) + db.session.add(new_user) + db.session.commit() + ret = {'message': 'Account erstellt für den Account {}'.format( + new_user.username + )} + return ret, 200 + else: + ret = {'message': 'Benutzername {} existiert bereits. Bitte wähle einen anderen '.format( + username + )} + return ret, 409 @app.route('/umfrage', methods=['POST']) diff --git a/frontend/src/App.css b/frontend/src/App.css index 49af386..d4d58cd 100644 --- a/frontend/src/App.css +++ b/frontend/src/App.css @@ -11,6 +11,7 @@ --secondary: rgb(218, 218, 218); --third: rgb(171, 183, 183); --forth: rgb(255, 255, 255); + --error: rgb(221, 140, 18); } .home, @@ -54,3 +55,7 @@ h1 { max-width: 60%; margin-bottom: 4vh; } + +.errorMessage { + color: var(--error); +} diff --git a/frontend/src/components/Input.css b/frontend/src/components/Input.css index cf46cda..239ff38 100644 --- a/frontend/src/components/Input.css +++ b/frontend/src/components/Input.css @@ -1,5 +1,5 @@ form { - max-width: 60%; + width: 60%; } .input-field { @@ -39,4 +39,4 @@ form { .input-field label { font-size: 1.5em; -} \ No newline at end of file +} diff --git a/frontend/src/components/SubmitField.js b/frontend/src/components/SubmitField.js index 42619ea..a6303a5 100644 --- a/frontend/src/components/SubmitField.js +++ b/frontend/src/components/SubmitField.js @@ -1,6 +1,5 @@ import React from "react"; import "./Input.css"; -import "./Button.css"; function SubmitField(props) { const InputValue = props.LabelName; diff --git a/frontend/src/components/pages/Register.js b/frontend/src/components/pages/Register.js index 3145a26..e8c4195 100644 --- a/frontend/src/components/pages/Register.js +++ b/frontend/src/components/pages/Register.js @@ -4,21 +4,35 @@ import "../../App.css"; import Footer from "../../Footer"; import InputField from "../InputField"; import SubmitField from "../SubmitField"; +import "../Input.css"; export default function Login() { const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); + const [message, setMessage] = useState(""); + const onSubmitClick = (e) => { e.preventDefault(); let opts = { username: username, password: password, }; - console.log(opts); fetch("/api/register", { method: "post", body: JSON.stringify(opts), + }).then((response) => { + console.log(response); + if (response.status === 409) { + /* + then is needed twice to get rid of the javascript Promise thing + */ + response.json().then((resp2) => { + setMessage(resp2.message); + }); + } else if (response.status === 200) { + //TODO redirect to login #77 or #74 + } }); }; @@ -41,6 +55,7 @@ export default function Login() { onChange={handleUsernameChange} InputName="username" /> +

{message}


- +