2021-06-21 22:58:08 +00:00
|
|
|
import React from "react";
|
2021-06-22 23:26:55 +00:00
|
|
|
import { useState } from "react/cjs/react.development";
|
2021-06-21 22:58:08 +00:00
|
|
|
import "../../App.css";
|
|
|
|
import Footer from "../../Footer";
|
|
|
|
import InputField from "../InputField";
|
|
|
|
import SubmitField from "../SubmitField";
|
2021-06-29 22:58:44 +00:00
|
|
|
import "../Input.css";
|
2021-07-05 16:00:01 +00:00
|
|
|
import ErrorMessage from "../ErrorMessage";
|
2021-07-06 00:12:32 +00:00
|
|
|
import { login, useAuth } from "../../auth/AuthProvider";
|
|
|
|
import { Redirect } from "react-router-dom";
|
2021-06-21 22:58:08 +00:00
|
|
|
|
|
|
|
export default function Login() {
|
2021-06-23 01:40:37 +00:00
|
|
|
const [username, setUsername] = useState("");
|
|
|
|
const [password, setPassword] = useState("");
|
2021-06-22 23:26:55 +00:00
|
|
|
|
2021-07-04 21:31:18 +00:00
|
|
|
const [errorMessage, setErrorMessage] = useState("");
|
|
|
|
|
|
|
|
const [isStatusOk, setIsStatusOk] = useState("");
|
2021-06-28 01:46:27 +00:00
|
|
|
|
2021-07-06 00:12:32 +00:00
|
|
|
const [isLoggedIn] = useAuth();
|
|
|
|
|
2021-06-23 01:40:37 +00:00
|
|
|
const onSubmitClick = (e) => {
|
2021-06-22 23:26:55 +00:00
|
|
|
e.preventDefault();
|
|
|
|
let opts = {
|
|
|
|
username: username,
|
|
|
|
password: password,
|
|
|
|
};
|
|
|
|
fetch("/api/register", {
|
|
|
|
method: "post",
|
|
|
|
body: JSON.stringify(opts),
|
2021-06-28 01:46:27 +00:00
|
|
|
}).then((response) => {
|
|
|
|
if (response.status === 409) {
|
2021-06-29 22:03:34 +00:00
|
|
|
/*
|
|
|
|
then is needed twice to get rid of the javascript Promise thing
|
|
|
|
*/
|
2021-07-04 21:31:18 +00:00
|
|
|
setIsStatusOk(false);
|
2021-06-29 22:03:34 +00:00
|
|
|
response.json().then((resp2) => {
|
2021-07-04 21:31:18 +00:00
|
|
|
setErrorMessage(resp2.message);
|
2021-06-29 22:03:34 +00:00
|
|
|
});
|
2021-06-29 22:58:44 +00:00
|
|
|
} else if (response.status === 200) {
|
2021-07-04 21:31:18 +00:00
|
|
|
response.json().then((resp) => {
|
2021-07-06 00:12:32 +00:00
|
|
|
fetch("/api/login", {
|
|
|
|
method: "post",
|
|
|
|
body: JSON.stringify(opts),
|
|
|
|
}).then((response) => {
|
|
|
|
response.json().then((token) => {
|
|
|
|
login(token);
|
|
|
|
});
|
|
|
|
});
|
2021-07-04 21:31:18 +00:00
|
|
|
});
|
|
|
|
setIsStatusOk(true);
|
2021-06-28 01:46:27 +00:00
|
|
|
}
|
2021-06-23 01:40:37 +00:00
|
|
|
});
|
2021-06-22 23:26:55 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleUsernameChange = (e) => {
|
|
|
|
setUsername(e.target.value);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handlePasswordChange = (e) => {
|
|
|
|
setPassword(e.target.value);
|
|
|
|
};
|
|
|
|
|
2021-06-21 22:58:08 +00:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<div className="sitePage">
|
|
|
|
<h1>Registrierung</h1>
|
|
|
|
<form>
|
|
|
|
<InputField
|
2021-06-23 01:16:09 +00:00
|
|
|
LabelName="Benutzername"
|
2021-06-22 23:26:55 +00:00
|
|
|
InputType="text"
|
|
|
|
onChange={handleUsernameChange}
|
2021-06-21 22:58:08 +00:00
|
|
|
InputName="username"
|
|
|
|
/>
|
2021-07-05 16:00:01 +00:00
|
|
|
{!isStatusOk && <ErrorMessage message={errorMessage} />}
|
2021-06-21 22:58:08 +00:00
|
|
|
<InputField
|
|
|
|
LabelName="Passwort"
|
|
|
|
InputType="password"
|
2021-06-22 23:26:55 +00:00
|
|
|
onChange={handlePasswordChange}
|
2021-06-21 22:58:08 +00:00
|
|
|
InputName="password"
|
|
|
|
/>
|
|
|
|
<br />
|
2021-06-29 23:00:33 +00:00
|
|
|
<SubmitField onClick={onSubmitClick} LabelName="Registrieren" />
|
2021-06-21 22:58:08 +00:00
|
|
|
</form>
|
2021-07-04 21:31:18 +00:00
|
|
|
|
2021-07-06 00:12:32 +00:00
|
|
|
{isLoggedIn && <Redirect to="/umfrage" />}
|
2021-06-21 22:58:08 +00:00
|
|
|
</div>
|
|
|
|
<Footer />
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|