78 lines
2 KiB
JavaScript
78 lines
2 KiB
JavaScript
import React from "react";
|
|
import { useState } from "react/cjs/react.development";
|
|
import "../../App.css";
|
|
import Footer from "../../Footer";
|
|
import InputField from "../InputField";
|
|
import SubmitField from "../SubmitField";
|
|
import { login, useAuth, logout } from "../../auth/AuthProvider";
|
|
import Secret from "./Secret";
|
|
|
|
export default function Login() {
|
|
const [username, setUsername] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
|
|
const onSubmitClick = (e) => {
|
|
e.preventDefault();
|
|
let opts = {
|
|
username: username,
|
|
password: password,
|
|
};
|
|
fetch("/api/login", {
|
|
method: "post",
|
|
body: JSON.stringify(opts),
|
|
})
|
|
.then((r) => r.json())
|
|
.then((token) => {
|
|
if (token.access_token) {
|
|
login(token);
|
|
} else {
|
|
// TODO: add text if the login is not correct
|
|
console.log("Please type in the correct username / password");
|
|
}
|
|
});
|
|
};
|
|
|
|
const handleUsernameChange = (e) => {
|
|
setUsername(e.target.value);
|
|
};
|
|
|
|
const handlePasswordChange = (e) => {
|
|
setPassword(e.target.value);
|
|
};
|
|
|
|
const [logged] = useAuth();
|
|
|
|
return (
|
|
<>
|
|
<div className="sitePage">
|
|
<h1>Login</h1>
|
|
{!logged ? (
|
|
<form action="#">
|
|
<InputField
|
|
LabelName="Benutzername"
|
|
onChange={handleUsernameChange}
|
|
InputType="text"
|
|
InputName="username"
|
|
InputPlaceHolder="Benutzername"
|
|
/>
|
|
<InputField
|
|
LabelName="Passwort"
|
|
InputType="password"
|
|
onChange={handlePasswordChange}
|
|
InputName="password"
|
|
InputPlaceHolder="Passwort"
|
|
/>
|
|
<br />
|
|
<SubmitField onClick={onSubmitClick} LabelName="Einloggen" />
|
|
</form>
|
|
) : (
|
|
<>
|
|
<Secret />
|
|
<button onClick={() => logout()}>Logout</button>
|
|
</>
|
|
)}
|
|
</div>
|
|
<Footer />
|
|
</>
|
|
);
|
|
}
|