feat: Conditionally render 'Add Opinion' form based on user authentication status and add Rocket server port configuration.
This commit is contained in:
parent
1ff7e1dd83
commit
9140498c6c
5
Rocket.toml
Normal file
5
Rocket.toml
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
[default]
|
||||||
|
port = 8000
|
||||||
|
|
||||||
|
[release]
|
||||||
|
port = 24237
|
||||||
@ -1,6 +1,13 @@
|
|||||||
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, Game, GameList } from "../items";
|
import {
|
||||||
|
Person,
|
||||||
|
AddOpinionRequest,
|
||||||
|
Game,
|
||||||
|
GameList,
|
||||||
|
AuthStatusRequest,
|
||||||
|
AuthStatusResponse,
|
||||||
|
} from "../items";
|
||||||
import { apiFetch } from "./api";
|
import { apiFetch } from "./api";
|
||||||
|
|
||||||
export const PersonDetails = () => {
|
export const PersonDetails = () => {
|
||||||
@ -9,6 +16,35 @@ export const PersonDetails = () => {
|
|||||||
const [games, setGames] = useState<Game[]>([]);
|
const [games, setGames] = useState<Game[]>([]);
|
||||||
const [gameTitle, setGameTitle] = useState("");
|
const [gameTitle, setGameTitle] = useState("");
|
||||||
const [wouldPlay, setWouldPlay] = useState(false);
|
const [wouldPlay, setWouldPlay] = useState(false);
|
||||||
|
const [currentUser, setCurrentUser] = useState<string>("");
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const token = localStorage.getItem("token");
|
||||||
|
if (token) {
|
||||||
|
const req = AuthStatusRequest.create({ token });
|
||||||
|
const buffer = AuthStatusRequest.encode(req).finish();
|
||||||
|
|
||||||
|
apiFetch("/auth/get_auth_status", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/octet-stream",
|
||||||
|
},
|
||||||
|
body: buffer,
|
||||||
|
})
|
||||||
|
.then((res) => res.arrayBuffer())
|
||||||
|
.then((buffer) => {
|
||||||
|
try {
|
||||||
|
const response = AuthStatusResponse.decode(new Uint8Array(buffer));
|
||||||
|
if (response.authenticated) {
|
||||||
|
setCurrentUser(response.username);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
console.error("Failed to decode auth status:", e);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch(console.error);
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
apiFetch("/api/games")
|
apiFetch("/api/games")
|
||||||
@ -99,6 +135,7 @@ export const PersonDetails = () => {
|
|||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{currentUser === name && (
|
||||||
<div
|
<div
|
||||||
style={{
|
style={{
|
||||||
marginTop: "2rem",
|
marginTop: "2rem",
|
||||||
@ -156,6 +193,7 @@ export const PersonDetails = () => {
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user