Compare commits
2 Commits
b0b14bc559
...
3b30f11d0f
Author | SHA1 | Date | |
---|---|---|---|
3b30f11d0f | |||
f182f16fe8 |
@ -1,6 +1,6 @@
|
||||
import { A } from "@solidjs/router";
|
||||
import { A, useNavigate } from "@solidjs/router";
|
||||
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 { AxiosError } from "axios";
|
||||
|
||||
@ -119,6 +119,7 @@ function UserRegistration(props: {setUserInfo: (u: UserInfo) => void}) {
|
||||
props.setUserInfo(response.data);
|
||||
localStorage.setItem(userIdKey, response.data.UserId);
|
||||
localStorage.setItem(usernameKey, response.data.Username);
|
||||
set_auth_token(response.data.UserId);
|
||||
} catch (_e) {
|
||||
const e = _e as AxiosError<{error: string}>;
|
||||
|
||||
@ -163,14 +164,27 @@ function UserRegistration(props: {setUserInfo: (u: UserInfo) => void}) {
|
||||
|
||||
function LobbyConnection() {
|
||||
const [loading, setLoading] = createSignal(false);
|
||||
const [error, setError] = createSignal("");
|
||||
const navigate = useNavigate();
|
||||
|
||||
const joinLobby = (ev: Event) => {
|
||||
ev.preventDefault();
|
||||
};
|
||||
|
||||
const createLobby = async() => {
|
||||
//
|
||||
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 (
|
||||
@ -209,6 +223,7 @@ function LobbyConnection() {
|
||||
Create a new lobby
|
||||
</button>
|
||||
|
||||
<p>{error()}</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
14
src/utils.ts
14
src/utils.ts
@ -1,9 +1,21 @@
|
||||
import axios from "axios";
|
||||
|
||||
export const backend = axios.create({
|
||||
export let backend = axios.create({
|
||||
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 = {
|
||||
UserId: string,
|
||||
Username: string,
|
||||
|
Loading…
Reference in New Issue
Block a user