Added login functionality

This commit is contained in:
keannu125 2023-02-25 23:27:12 +08:00
parent aea617500c
commit 5a34617d77
2 changed files with 29 additions and 4 deletions

View file

@ -22,3 +22,21 @@ export function AddNote(note: note) {
export function DeleteNote(id: number) { export function DeleteNote(id: number) {
return axios.delete("http://localhost:8000/api/v1/notes/" + id + "/"); return axios.delete("http://localhost:8000/api/v1/notes/" + id + "/");
} }
export interface user {
username: string;
password: string;
}
export function UserLogin(user: user) {
return axios
.post("http://localhost:8000/api/v1/accounts/token/login", user)
.then((response) => {
console.log("Success! Token: " + response.data);
return true;
})
.catch((error) => {
console.log("Login Failed: " + error);
return false;
});
}

View file

@ -5,6 +5,7 @@ import { useLocation, useNavigate } from "react-router-dom";
import Header from "../../Components/Header/Header"; import Header from "../../Components/Header/Header";
import { useState } from "react"; import { useState } from "react";
import { Button } from "@mui/material"; import { Button } from "@mui/material";
import { UserLogin } from "../../Components/Api/Api";
export default function Login() { export default function Login() {
const navigate = useNavigate(); const navigate = useNavigate();
@ -12,7 +13,8 @@ export default function Login() {
username: "", username: "",
password: "", password: "",
}); });
console.log("We are in the" + useLocation()); const [error, setError] = useState("");
return ( return (
<div style={styles.background}> <div style={styles.background}>
<Header /> <Header />
@ -44,17 +46,22 @@ export default function Login() {
<Button <Button
style={styles.button_green} style={styles.button_green}
variant="contained" variant="contained"
onClick={() => { onClick={async () => {
navigate("/Login"); if (await UserLogin(user)) {
navigate("/");
} else {
setError("Invalid Login");
}
}} }}
> >
Login Login
</Button> </Button>
<p style={styles.text_small_red}>{error}</p>
<Button <Button
style={styles.button_yellow} style={styles.button_yellow}
variant="contained" variant="contained"
onClick={() => { onClick={() => {
navigate("/Login"); navigate("/Register");
}} }}
> >
Register Register