dylanebert HF staff commited on
Commit
1e16a1f
1 Parent(s): 41a7389
Dockerfile CHANGED
@@ -6,6 +6,12 @@ RUN npm install
6
 
7
  RUN --mount=type=secret,id=VITE_HF_TOKEN,mode=0444,required=true \
8
  echo "VITE_HF_TOKEN=$(cat /run/secrets/VITE_HF_TOKEN)" > /app/.env
 
 
 
 
 
 
9
 
10
  COPY / /app
11
  RUN npm run build
 
6
 
7
  RUN --mount=type=secret,id=VITE_HF_TOKEN,mode=0444,required=true \
8
  echo "VITE_HF_TOKEN=$(cat /run/secrets/VITE_HF_TOKEN)" > /app/.env
9
+ RUN --mount=type=secret,id=VITE_CLIENT_ID,mode=0444,required=true \
10
+ echo "VITE_CLIENT_ID=$(cat /run/secrets/VITE_CLIENT_ID)" >> /app/.env
11
+ RUN --mount=type=secret,id=VITE_CLIENT_SECRET,mode=0444,required=true \
12
+ echo "VITE_CLIENT_SECRET=$(cat /run/secrets/VITE_CLIENT_SECRET)" >> /app/.env
13
+ RUN --mount=type=secret,id=VITE_REDIRECT_URI,mode=0444,required=true \
14
+ echo "VITE_REDIRECT_URI=$(cat /run/secrets/VITE_REDIRECT_URI)" >> /app/.env
15
 
16
  COPY / /app
17
  RUN npm run build
src/routes/Vote.svelte CHANGED
@@ -1,6 +1,5 @@
1
  <script lang="ts">
2
  import { onMount, onDestroy } from "svelte";
3
- import { v4 as uuidv4 } from "uuid";
4
  import type { IViewer } from "./viewers/IViewer";
5
  import { createViewer } from "./viewers/ViewerFactory";
6
  import { Cube, WatsonHealth3DPrintMesh } from "carbon-icons-svelte";
@@ -35,27 +34,15 @@
35
  let errorMessage: string = "";
36
  let data: Data;
37
 
38
- function getUsername() {
39
- let storedUsername = sessionStorage.getItem("username");
40
- if (!storedUsername) {
41
- storedUsername = uuidv4();
42
- sessionStorage.setItem("username", storedUsername);
43
- }
44
- return storedUsername;
45
- }
46
-
47
  async function fetchScenes() {
48
  statusMessage = "Loading...";
49
  errorMessage = "";
50
 
51
  try {
52
- const username = getUsername();
53
- console.log(`Fetching with username: ${username}`);
54
  const url = "/api/fetchScenes";
55
  const response = await fetch(url, {
56
  method: "GET",
57
  headers: {
58
- username: username,
59
  "Cache-Control": "no-cache",
60
  },
61
  });
@@ -99,6 +86,7 @@
99
  window.addEventListener("resize", handleResize);
100
  handleResize();
101
  } catch (error) {
 
102
  errorMessage = "Failed to load scenes.";
103
  }
104
 
@@ -112,7 +100,6 @@
112
  voteOverlayB.classList.add("show");
113
 
114
  const payload = {
115
- username: getUsername(),
116
  input: data.input,
117
  better: option == "A" ? data.model1 : data.model2,
118
  worse: option == "A" ? data.model2 : data.model1,
@@ -144,7 +131,13 @@
144
  loadScenes();
145
  }, remainingTime);
146
  } else {
147
- errorMessage = "Failed to process vote.";
 
 
 
 
 
 
148
  }
149
  } catch (error) {
150
  errorMessage = "Failed to process vote.";
 
1
  <script lang="ts">
2
  import { onMount, onDestroy } from "svelte";
 
3
  import type { IViewer } from "./viewers/IViewer";
4
  import { createViewer } from "./viewers/ViewerFactory";
5
  import { Cube, WatsonHealth3DPrintMesh } from "carbon-icons-svelte";
 
34
  let errorMessage: string = "";
35
  let data: Data;
36
 
 
 
 
 
 
 
 
 
 
37
  async function fetchScenes() {
38
  statusMessage = "Loading...";
39
  errorMessage = "";
40
 
41
  try {
 
 
42
  const url = "/api/fetchScenes";
43
  const response = await fetch(url, {
44
  method: "GET",
45
  headers: {
 
46
  "Cache-Control": "no-cache",
47
  },
48
  });
 
86
  window.addEventListener("resize", handleResize);
87
  handleResize();
88
  } catch (error) {
89
+ console.log(error);
90
  errorMessage = "Failed to load scenes.";
91
  }
92
 
 
100
  voteOverlayB.classList.add("show");
101
 
102
  const payload = {
 
103
  input: data.input,
104
  better: option == "A" ? data.model1 : data.model2,
105
  worse: option == "A" ? data.model2 : data.model1,
 
131
  loadScenes();
132
  }, remainingTime);
133
  } else {
134
+ if (response.status === 401) {
135
+ statusMessage = "Unauthorized. Redirecting to login...";
136
+ await new Promise((resolve) => setTimeout(resolve, 1000));
137
+ window.location.href = "/api/authorize";
138
+ } else {
139
+ errorMessage = "Failed to process vote.";
140
+ }
141
  }
142
  } catch (error) {
143
  errorMessage = "Failed to process vote.";
src/routes/api/authorize/+server.ts ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { redirect } from "@sveltejs/kit";
2
+
3
+ export const GET = async () => {
4
+ const clientId = import.meta.env.VITE_CLIENT_ID;
5
+ const redirectUri = import.meta.env.VITE_REDIRECT_URI;
6
+ const url = "https://huggingface.co/oauth/authorize";
7
+ const body = new URLSearchParams({
8
+ client_id: clientId,
9
+ redirect_uri: redirectUri,
10
+ response_type: "code",
11
+ scope: "openid",
12
+ prompt: "consent",
13
+ state: "12345",
14
+ });
15
+
16
+ throw redirect(302, `${url}?${body}`);
17
+ };
src/routes/api/fetchScenes/+server.ts CHANGED
@@ -1,8 +1,15 @@
1
- import type { RequestHandler } from "@sveltejs/kit";
2
 
3
  export const GET: RequestHandler = async ({ request }) => {
4
- const username = request.headers.get("username");
5
- const url = `https://dylanebert-3d-arena-backend.hf.space/pair?username=${username}`;
 
 
 
 
 
 
 
6
 
7
  try {
8
  const response = await fetch(url, {
 
1
+ import { RequestHandler } from "@sveltejs/kit";
2
 
3
  export const GET: RequestHandler = async ({ request }) => {
4
+ const cookies = request.headers.get("cookie");
5
+ const accessToken = cookies
6
+ ?.split(";")
7
+ .map((cookie) => cookie.trim())
8
+ .find((cookie) => cookie.startsWith("access_token="))
9
+ ?.split("=")[1];
10
+
11
+ const url = `https://dylanebert-3d-arena-backend.hf.space/pair?access_token=${accessToken}`;
12
+ // const url = `http://localhost:8000/pair?access_token=${access_token}`;
13
 
14
  try {
15
  const response = await fetch(url, {
src/routes/api/leaderboard/+server.ts CHANGED
@@ -1,6 +1,4 @@
1
- import type { RequestHandler } from "@sveltejs/kit";
2
-
3
- export const GET: RequestHandler = async ({ request }) => {
4
  const url = `https://dylanebert-3d-arena-backend.hf.space/leaderboard`;
5
 
6
  try {
 
1
+ export const GET = async () => {
 
 
2
  const url = `https://dylanebert-3d-arena-backend.hf.space/leaderboard`;
3
 
4
  try {
src/routes/api/oauth-redirect/+server.ts ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { RequestHandler } from "@sveltejs/kit";
2
+
3
+ export const GET: RequestHandler = async ({ request }) => {
4
+ const requestUrl = new URL(request.url);
5
+ const code = requestUrl.searchParams.get("code") as string;
6
+
7
+ const clientId = import.meta.env.VITE_CLIENT_ID;
8
+ const clientSecret = import.meta.env.VITE_CLIENT_SECRET;
9
+ const redirectUri = import.meta.env.VITE_REDIRECT_URI;
10
+ const url = "https://huggingface.co/oauth/token";
11
+ const body = new URLSearchParams({
12
+ client_id: clientId,
13
+ client_secret: clientSecret,
14
+ redirect_uri: redirectUri,
15
+ code: code,
16
+ grant_type: "authorization_code",
17
+ });
18
+ const response = await fetch(url, {
19
+ method: "POST",
20
+ headers: {
21
+ "Content-Type": "application/x-www-form-urlencoded",
22
+ },
23
+ body: body,
24
+ });
25
+
26
+ const data = await response.json();
27
+ const accessToken = data.access_token;
28
+
29
+ const headers = new Headers();
30
+ headers.append("Set-Cookie", `access_token=${accessToken}; Path=/; SameSite=None; Secure`);
31
+ headers.append("Location", "/");
32
+
33
+ return new Response(null, { status: 302, headers: headers });
34
+ };
src/routes/api/vote/+server.ts CHANGED
@@ -1,8 +1,20 @@
1
  import type { RequestHandler } from "@sveltejs/kit";
2
 
3
  export const POST: RequestHandler = async ({ request }) => {
 
 
 
 
 
 
 
 
 
 
 
4
  const payload = await request.json();
5
  const url = `https://dylanebert-3d-arena-backend.hf.space/vote`;
 
6
 
7
  try {
8
  const response = await fetch(url, {
 
1
  import type { RequestHandler } from "@sveltejs/kit";
2
 
3
  export const POST: RequestHandler = async ({ request }) => {
4
+ const cookies = request.headers.get("cookie");
5
+ const accessToken = cookies
6
+ ?.split(";")
7
+ .map((cookie) => cookie.trim())
8
+ .find((cookie) => cookie.startsWith("access_token="))
9
+ ?.split("=")[1];
10
+
11
+ if (!accessToken) {
12
+ return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
13
+ }
14
+
15
  const payload = await request.json();
16
  const url = `https://dylanebert-3d-arena-backend.hf.space/vote`;
17
+ // const url = `http://localhost:8000/vote`;
18
 
19
  try {
20
  const response = await fetch(url, {