Skip to content

Commit f9b4c1e

Browse files
committed
add token request and cookie handling
1 parent 9054067 commit f9b4c1e

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed

src/client/HostLobbyModal.ts

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -592,8 +592,11 @@ export class HostLobbyModal extends LitElement {
592592

593593
createLobby(this.lobbyCreatorClientID)
594594
.then((lobby) => {
595-
this.lobbyId = lobby.gameID;
595+
this.lobbyId = lobby.gameInfo.gameID;
596596
// join lobby
597+
const cookieDurationSec = 60 * 60 * 6; //Store cookie for max 6 hours
598+
// getting error here from lobby.hostToken
599+
document.cookie = `hostToken=${lobby.hostToken}; Max-Axe=${cookieDurationSec}; Path=/`;
597600
})
598601
.then(() => {
599602
this.dispatchEvent(
@@ -794,15 +797,30 @@ export class HostLobbyModal extends LitElement {
794797
);
795798
this.close();
796799
const config = await getServerConfigFromClient();
800+
801+
const cookies = document.cookie.split(";");
802+
let hostToken = "";
803+
for (let i = 0; i < cookies.length; i++) {
804+
const cookie = cookies[i];
805+
if (cookie.trim().startsWith("hostToken=")) {
806+
hostToken = cookie.split("=")[1];
807+
break;
808+
}
809+
}
810+
console.log(hostToken, cookies);
811+
const j = JSON.stringify({ hostToken: hostToken });
812+
console.log(j);
797813
const response = await fetch(
798814
`${window.location.origin}/${config.workerPath(this.lobbyId)}/api/start_game/${this.lobbyId}`,
799815
{
800816
method: "POST",
801817
headers: {
802818
"Content-Type": "application/json",
803819
},
820+
body: j,
804821
},
805822
);
823+
document.cookie = "hostToken=;Max-Age=-1"; //delete cookie
806824
return response;
807825
}
808826

@@ -849,7 +867,9 @@ export class HostLobbyModal extends LitElement {
849867
}
850868
}
851869

852-
async function createLobby(creatorClientID: string): Promise<GameInfo> {
870+
async function createLobby(
871+
creatorClientID: string,
872+
): Promise<{ gameInfo: GameInfo; hostToken: string }> {
853873
const config = await getServerConfigFromClient();
854874
try {
855875
const id = generateID();
@@ -873,7 +893,7 @@ async function createLobby(creatorClientID: string): Promise<GameInfo> {
873893
const data = await response.json();
874894
console.log("Success:", data);
875895

876-
return data as GameInfo;
896+
return { gameInfo: data, hostToken: data.hostToken };
877897
} catch (error) {
878898
console.error("Error creating lobby:", error);
879899
throw error;

0 commit comments

Comments
 (0)