generated from lucien/api-template
add: added homepage
This commit is contained in:
parent
1f24907c80
commit
4ea8c9f2b3
11 changed files with 239 additions and 68 deletions
47
front/src/pages/Register.tsx
Normal file
47
front/src/pages/Register.tsx
Normal file
|
@ -0,0 +1,47 @@
|
|||
import axios from "axios";
|
||||
import { useState } from "react";
|
||||
import { useNavigate, Link } from "react-router-dom";
|
||||
|
||||
export default function Register () {
|
||||
const [username, setUsername] = useState("");
|
||||
const [password, setPassword] = useState("");
|
||||
const navigate = useNavigate();
|
||||
|
||||
function handleSubmit(e: React.FormEvent) {
|
||||
e.preventDefault();
|
||||
axios.post("/api/auth/register", { username, password });
|
||||
axios
|
||||
.post("/api/auth/login", { username, password })
|
||||
.then((res) => {
|
||||
localStorage.setItem("token", res.data.token);
|
||||
navigate("/");
|
||||
})
|
||||
.catch((err) => {
|
||||
alert(err.response.data.message);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Link to="/">Home</Link>
|
||||
<h1>Register</h1>
|
||||
<form onSubmit={handleSubmit}>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="Username"
|
||||
value={username}
|
||||
onChange={(e) => setUsername(e.target.value)}
|
||||
/>
|
||||
<input
|
||||
type="password"
|
||||
placeholder="Password"
|
||||
value={password}
|
||||
onChange={(e) => setPassword(e.target.value)}
|
||||
/>
|
||||
<button type="submit">Register</button>
|
||||
</form>
|
||||
<Link to="/login">Login</Link>
|
||||
</div>
|
||||
);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue