Compare commits

...

2 Commits

2 changed files with 31 additions and 4 deletions

View File

@ -1,6 +1,6 @@
import { A } from "@solidjs/router"; import { A, useNavigate } from "@solidjs/router";
import { createSignal, onMount, Show } from "solid-js"; import { createSignal, onMount, Show } from "solid-js";
import { backend, UserInfo } from "../utils"; import { backend, set_auth_token, UserInfo } from "../utils";
import { Card } from "../components/Card"; import { Card } from "../components/Card";
import { AxiosError } from "axios"; import { AxiosError } from "axios";
@ -119,6 +119,7 @@ function UserRegistration(props: {setUserInfo: (u: UserInfo) => void}) {
props.setUserInfo(response.data); props.setUserInfo(response.data);
localStorage.setItem(userIdKey, response.data.UserId); localStorage.setItem(userIdKey, response.data.UserId);
localStorage.setItem(usernameKey, response.data.Username); localStorage.setItem(usernameKey, response.data.Username);
set_auth_token(response.data.UserId);
} catch (_e) { } catch (_e) {
const e = _e as AxiosError<{error: string}>; const e = _e as AxiosError<{error: string}>;
@ -163,14 +164,27 @@ function UserRegistration(props: {setUserInfo: (u: UserInfo) => void}) {
function LobbyConnection() { function LobbyConnection() {
const [loading, setLoading] = createSignal(false); const [loading, setLoading] = createSignal(false);
const [error, setError] = createSignal("");
const navigate = useNavigate();
const joinLobby = (ev: Event) => { const joinLobby = (ev: Event) => {
ev.preventDefault(); ev.preventDefault();
}; };
const createLobby = async() => { const createLobby = async() => {
//
setLoading(true); setLoading(true);
try {
const res = await backend.post<{LobbyId: string}>("/lobby/new");
console.log(res.data);
// Redirect to the lobby component using the received lobby id
navigate(`/lobby/${res.data.LobbyId}`);
} catch (_e) {
const e: Error = _e as Error;
setError(e.message);
} finally {
setLoading(false);
}
}; };
return ( return (
@ -209,6 +223,7 @@ function LobbyConnection() {
Create a new lobby Create a new lobby
</button> </button>
<p>{error()}</p>
</div> </div>
); );
} }

View File

@ -1,9 +1,21 @@
import axios from "axios"; import axios from "axios";
export const backend = axios.create({ export let backend = axios.create({
baseURL: `${import.meta.env.VITE_BACKEND_URL}/api`, baseURL: `${import.meta.env.VITE_BACKEND_URL}/api`,
headers: {
"Authorization": `Bearer ${localStorage.getItem("UserId") ?? ""}`,
},
}); });
export function set_auth_token(token: string) {
backend = axios.create({
baseURL: `${import.meta.env.VITE_BACKEND_URL}/api`,
headers: {
"Authorization": `Bearer ${token}`,
},
});
}
export type UserInfo = { export type UserInfo = {
UserId: string, UserId: string,
Username: string, Username: string,