Better login screen

This commit is contained in:
Martin Berg Alstad 2023-07-22 20:44:12 +02:00
parent 51bc754ef1
commit f4d91e43d8

View File

@ -1,4 +1,4 @@
import React, {FC, FormEvent} from "react"; import React, {FC, FormEvent, useState} from "react";
import {Button} from "../components/button"; import {Button} from "../components/button";
import Input from "../components/input"; import Input from "../components/input";
import {useSetAtom} from "jotai"; import {useSetAtom} from "jotai";
@ -11,6 +11,7 @@ const Login: FC = () => {
const setThisPlayer = useSetAtom(thisPlayerAtom); const setThisPlayer = useSetAtom(thisPlayerAtom);
const navigate = useNavigate(); const navigate = useNavigate();
const [error, setError] = useState<string | undefined>();
async function handleLogin(e: FormEvent<HTMLFormElement>): Promise<void> { async function handleLogin(e: FormEvent<HTMLFormElement>): Promise<void> {
e.preventDefault(); e.preventDefault();
@ -30,23 +31,27 @@ const Login: FC = () => {
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json() as PlayerProps;
console.debug("Login successful: ", data); setThisPlayer(new Player(data));
setThisPlayer(new Player(data as PlayerProps));
navigate("/lobby"); navigate("/lobby");
} else { } else {
const data = await response.text(); const data = await response.text();
console.error("Error: ", data); console.error(data);
// TODO display error setError(data);
} }
} }
return ( // TODO prettify const username = "username", password = "password";
<form onSubmit={handleLogin}>
<h1>Login</h1> return (
<Input name={"username"} placeholder={"Username"}/> <form onSubmit={handleLogin} className={"container w-fit mx-auto flex flex-col gap-2"}>
<Input name={"password"} type={"password"} placeholder={"Password"}/> <h1 className={"my-5"}>Login</h1>
{error && <p className={"text-red-500"}>{error}</p>}
<label htmlFor={username}>Username:</label>
<Input id={username} name={username} placeholder={"Username"} required/>
<label htmlFor={password}>Password:</label>
<Input id={password} name={password} type={"password"} placeholder={"Password"} required/>
<Button type={"submit"}>Login</Button> <Button type={"submit"}>Login</Button>
</form> </form>
); );