store token
This commit is contained in:
parent
30950e6c83
commit
926926f357
@ -4,6 +4,7 @@ extern crate prost_build;
|
|||||||
|
|
||||||
fn main() -> Result<(), std::io::Error> {
|
fn main() -> Result<(), std::io::Error> {
|
||||||
println!("cargo:rerun-if-changed=../protobuf/items.proto");
|
println!("cargo:rerun-if-changed=../protobuf/items.proto");
|
||||||
|
println!("cargo:rerun-if-changed=../frontend/src/");
|
||||||
|
|
||||||
let mut cfg = prost_build::Config::new();
|
let mut cfg = prost_build::Config::new();
|
||||||
|
|
||||||
|
|||||||
@ -5,19 +5,18 @@ import { PersonList } from "./PersonList";
|
|||||||
import { PersonDetails } from "./PersonDetails";
|
import { PersonDetails } from "./PersonDetails";
|
||||||
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
|
import { BrowserRouter, Routes, Route, Link } from "react-router-dom";
|
||||||
import "./App.css";
|
import "./App.css";
|
||||||
|
import { apiFetch } from "./api";
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [people, setPeople] = useState<Person[]>([]);
|
const [people, setPeople] = useState<Person[]>([]);
|
||||||
const [token, setToken] = useState<string>("");
|
const [token, setToken] = useState<string>(
|
||||||
|
localStorage.getItem("token") || ""
|
||||||
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!token) return;
|
if (!token) return;
|
||||||
|
|
||||||
fetch("/api", {
|
apiFetch("/api")
|
||||||
headers: {
|
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then((res) => res.arrayBuffer())
|
.then((res) => res.arrayBuffer())
|
||||||
.then((buffer) => {
|
.then((buffer) => {
|
||||||
const list = PersonListProto.decode(new Uint8Array(buffer));
|
const list = PersonListProto.decode(new Uint8Array(buffer));
|
||||||
@ -26,13 +25,19 @@ function App() {
|
|||||||
.catch((err) => console.error("Failed to fetch people:", err));
|
.catch((err) => console.error("Failed to fetch people:", err));
|
||||||
}, [token]);
|
}, [token]);
|
||||||
|
|
||||||
|
const handleLogin = (newToken: string) => {
|
||||||
|
setToken(newToken);
|
||||||
|
localStorage.setItem("token", newToken);
|
||||||
|
};
|
||||||
|
|
||||||
const handleLogout = () => {
|
const handleLogout = () => {
|
||||||
setToken("");
|
setToken("");
|
||||||
setPeople([]);
|
setPeople([]);
|
||||||
|
localStorage.removeItem("token");
|
||||||
};
|
};
|
||||||
|
|
||||||
if (!token) {
|
if (!token) {
|
||||||
return <Login onLogin={setToken} />;
|
return <Login onLogin={handleLogin} />;
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -55,10 +60,7 @@ function App() {
|
|||||||
</div>
|
</div>
|
||||||
<Routes>
|
<Routes>
|
||||||
<Route path="/" element={<PersonList people={people} />} />
|
<Route path="/" element={<PersonList people={people} />} />
|
||||||
<Route
|
<Route path="/person/:name" element={<PersonDetails />} />
|
||||||
path="/person/:name"
|
|
||||||
element={<PersonDetails token={token} />}
|
|
||||||
/>
|
|
||||||
</Routes>
|
</Routes>
|
||||||
</div>
|
</div>
|
||||||
</BrowserRouter>
|
</BrowserRouter>
|
||||||
|
|||||||
@ -1,12 +1,9 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useState, useEffect } from "react";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams } from "react-router-dom";
|
||||||
import { Person, AddOpinionRequest } from "../items";
|
import { Person, AddOpinionRequest } from "../items";
|
||||||
|
import { apiFetch } from "./api";
|
||||||
|
|
||||||
interface Props {
|
export const PersonDetails = () => {
|
||||||
token: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export const PersonDetails = ({ token }: Props) => {
|
|
||||||
const { name } = useParams<{ name: string }>();
|
const { name } = useParams<{ name: string }>();
|
||||||
const [person, setPerson] = useState<Person | null>(null);
|
const [person, setPerson] = useState<Person | null>(null);
|
||||||
const [gameTitle, setGameTitle] = useState("");
|
const [gameTitle, setGameTitle] = useState("");
|
||||||
@ -14,9 +11,7 @@ export const PersonDetails = ({ token }: Props) => {
|
|||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (name) {
|
if (name) {
|
||||||
fetch(`/api/${name}`, {
|
apiFetch(`/api/${name}`)
|
||||||
headers: { Authorization: `Bearer ${token}` },
|
|
||||||
})
|
|
||||||
.then((res) => res.arrayBuffer())
|
.then((res) => res.arrayBuffer())
|
||||||
.then((buffer) => {
|
.then((buffer) => {
|
||||||
try {
|
try {
|
||||||
@ -27,7 +22,7 @@ export const PersonDetails = ({ token }: Props) => {
|
|||||||
})
|
})
|
||||||
.catch(console.error);
|
.catch(console.error);
|
||||||
}
|
}
|
||||||
}, [name, token]);
|
}, [name]);
|
||||||
|
|
||||||
const handleAddOpinion = async () => {
|
const handleAddOpinion = async () => {
|
||||||
if (!person) return;
|
if (!person) return;
|
||||||
@ -40,11 +35,10 @@ export const PersonDetails = ({ token }: Props) => {
|
|||||||
const buffer = AddOpinionRequest.encode(req).finish();
|
const buffer = AddOpinionRequest.encode(req).finish();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const res = await fetch("/api/opinion", {
|
const res = await apiFetch("/api/opinion", {
|
||||||
method: "POST",
|
method: "POST",
|
||||||
headers: {
|
headers: {
|
||||||
"Content-Type": "application/octet-stream",
|
"Content-Type": "application/octet-stream",
|
||||||
Authorization: `Bearer ${token}`,
|
|
||||||
},
|
},
|
||||||
body: buffer,
|
body: buffer,
|
||||||
});
|
});
|
||||||
|
|||||||
24
frontend/src/api.ts
Normal file
24
frontend/src/api.ts
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
export const apiFetch = async (
|
||||||
|
url: string,
|
||||||
|
options: RequestInit = {}
|
||||||
|
): Promise<Response> => {
|
||||||
|
const token = localStorage.getItem("token");
|
||||||
|
const headers = new Headers(options.headers);
|
||||||
|
|
||||||
|
if (token) {
|
||||||
|
headers.set("Authorization", `Bearer ${token}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
...options,
|
||||||
|
headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await fetch(url, config);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Request failed with status ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user