Spaces:
Running
Running
dylanebert
commited on
Commit
·
160a21b
1
Parent(s):
003d510
replace cookies with local store
Browse files
src/lib/stores/auth.ts
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import { writable } from "svelte/store";
|
2 |
+
|
3 |
+
export const accessToken = writable(localStorage.getItem("access_token") || null);
|
4 |
+
|
5 |
+
accessToken.subscribe((value) => {
|
6 |
+
if (value) {
|
7 |
+
localStorage.setItem("access_token", value);
|
8 |
+
} else {
|
9 |
+
localStorage.removeItem("access_token");
|
10 |
+
}
|
11 |
+
});
|
src/routes/Vote.svelte
CHANGED
@@ -40,10 +40,12 @@
|
|
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 |
});
|
49 |
const result = await response.json();
|
@@ -102,6 +104,12 @@
|
|
102 |
voteOverlayA.classList.add("show");
|
103 |
voteOverlayB.classList.add("show");
|
104 |
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
const payload = {
|
106 |
input: data.input,
|
107 |
better: option == "A" ? data.model1 : data.model2,
|
@@ -117,6 +125,7 @@
|
|
117 |
headers: {
|
118 |
"Cache-Control": "no-cache",
|
119 |
"Content-Type": "application/json",
|
|
|
120 |
},
|
121 |
body: JSON.stringify(payload),
|
122 |
});
|
|
|
40 |
|
41 |
try {
|
42 |
const url = "/api/fetchScenes";
|
43 |
+
const token = localStorage.getItem("access_token");
|
44 |
const response = await fetch(url, {
|
45 |
method: "GET",
|
46 |
headers: {
|
47 |
"Cache-Control": "no-cache",
|
48 |
+
Authorization: `Bearer ${token}`,
|
49 |
},
|
50 |
});
|
51 |
const result = await response.json();
|
|
|
104 |
voteOverlayA.classList.add("show");
|
105 |
voteOverlayB.classList.add("show");
|
106 |
|
107 |
+
const token = localStorage.getItem("access_token");
|
108 |
+
if (!token) {
|
109 |
+
window.location.href = "/api/authorize";
|
110 |
+
return;
|
111 |
+
}
|
112 |
+
|
113 |
const payload = {
|
114 |
input: data.input,
|
115 |
better: option == "A" ? data.model1 : data.model2,
|
|
|
125 |
headers: {
|
126 |
"Cache-Control": "no-cache",
|
127 |
"Content-Type": "application/json",
|
128 |
+
Authorization: `Bearer ${token}`,
|
129 |
},
|
130 |
body: JSON.stringify(payload),
|
131 |
});
|
src/routes/api/exchange-code/+server.ts
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import type { RequestHandler } from "@sveltejs/kit";
|
2 |
+
|
3 |
+
export const GET: RequestHandler = async ({ url }) => {
|
4 |
+
const code = url.searchParams.get("code");
|
5 |
+
if (!code) {
|
6 |
+
return new Response(JSON.stringify({ error: "Code not provided" }), { status: 400 });
|
7 |
+
}
|
8 |
+
const clientId = import.meta.env.VITE_CLIENT_ID;
|
9 |
+
const clientSecret = import.meta.env.VITE_CLIENT_SECRET;
|
10 |
+
const redirectUri = import.meta.env.VITE_REDIRECT_URI;
|
11 |
+
const tokenUrl = "https://huggingface.co/oauth/token";
|
12 |
+
const body = new URLSearchParams({
|
13 |
+
client_id: clientId,
|
14 |
+
client_secret: clientSecret,
|
15 |
+
redirect_uri: redirectUri,
|
16 |
+
code: code,
|
17 |
+
grant_type: "authorization_code",
|
18 |
+
});
|
19 |
+
|
20 |
+
try {
|
21 |
+
const response = await fetch(tokenUrl, {
|
22 |
+
method: "POST",
|
23 |
+
headers: {
|
24 |
+
"Content-Type": "application/x-www-form-urlencoded",
|
25 |
+
},
|
26 |
+
body,
|
27 |
+
});
|
28 |
+
|
29 |
+
if (!response.ok) {
|
30 |
+
return new Response(JSON.stringify({ error: "Token exchange failed" }), { status: response.status });
|
31 |
+
}
|
32 |
+
const data = await response.json();
|
33 |
+
const accessToken = data.access_token;
|
34 |
+
|
35 |
+
return new Response(JSON.stringify({ access_token: accessToken }), {
|
36 |
+
status: 200,
|
37 |
+
headers: { "Content-Type": "application/json" },
|
38 |
+
});
|
39 |
+
} catch (error) {
|
40 |
+
return new Response(JSON.stringify({ error: "Token exchange failed" }), { status: 500 });
|
41 |
+
}
|
42 |
+
};
|
src/routes/api/fetchScenes/+server.ts
CHANGED
@@ -1,12 +1,11 @@
|
|
1 |
import { RequestHandler } from "@sveltejs/kit";
|
2 |
|
3 |
export const GET: RequestHandler = async ({ request }) => {
|
4 |
-
const
|
5 |
-
|
6 |
-
|
7 |
-
.
|
8 |
-
|
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}`;
|
|
|
1 |
import { RequestHandler } from "@sveltejs/kit";
|
2 |
|
3 |
export const GET: RequestHandler = async ({ request }) => {
|
4 |
+
const authHeader = request.headers.get("authorization");
|
5 |
+
let accessToken = null;
|
6 |
+
if (authHeader && authHeader.startsWith("Bearer ")) {
|
7 |
+
accessToken = authHeader.substring("Bearer ".length);
|
8 |
+
}
|
|
|
9 |
|
10 |
const url = `https://dylanebert-3d-arena-backend.hf.space/pair?access_token=${accessToken}`;
|
11 |
// const url = `http://localhost:8000/pair?access_token=${access_token}`;
|
src/routes/api/oauth-redirect/+server.ts
DELETED
@@ -1,34 +0,0 @@
|
|
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,17 +1,13 @@
|
|
1 |
import type { RequestHandler } from "@sveltejs/kit";
|
2 |
|
3 |
export const POST: RequestHandler = async ({ request }) => {
|
4 |
-
const
|
5 |
-
|
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 |
payload.access_token = accessToken;
|
17 |
|
|
|
1 |
import type { RequestHandler } from "@sveltejs/kit";
|
2 |
|
3 |
export const POST: RequestHandler = async ({ request }) => {
|
4 |
+
const authHeader = request.headers.get("authorization");
|
5 |
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
return new Response(JSON.stringify({ error: "Unauthorized" }), { status: 401 });
|
7 |
}
|
8 |
|
9 |
+
const accessToken = authHeader.substring("Bearer ".length);
|
10 |
+
|
11 |
const payload = await request.json();
|
12 |
payload.access_token = accessToken;
|
13 |
|
src/routes/oauth-redirect/+page.svelte
ADDED
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<script lang="ts">
|
2 |
+
import { onMount } from "svelte";
|
3 |
+
import { goto } from "$app/navigation";
|
4 |
+
|
5 |
+
onMount(async () => {
|
6 |
+
const urlParams = new URLSearchParams(window.location.search);
|
7 |
+
const code = urlParams.get("code");
|
8 |
+
if (!code) {
|
9 |
+
goto("/");
|
10 |
+
return;
|
11 |
+
}
|
12 |
+
try {
|
13 |
+
const res = await fetch(`/api/exchange-code?code=${code}`);
|
14 |
+
if (!res.ok) {
|
15 |
+
console.error("Failed to exchange code for token");
|
16 |
+
goto("/");
|
17 |
+
return;
|
18 |
+
}
|
19 |
+
const data = await res.json();
|
20 |
+
const token = data.access_token;
|
21 |
+
if (token) {
|
22 |
+
localStorage.setItem("access_token", token);
|
23 |
+
goto("/");
|
24 |
+
} else {
|
25 |
+
console.error("No access token in response");
|
26 |
+
goto("/");
|
27 |
+
}
|
28 |
+
} catch (error) {
|
29 |
+
console.error("Error during token exchange", error);
|
30 |
+
goto("/");
|
31 |
+
}
|
32 |
+
});
|
33 |
+
</script>
|
34 |
+
|
35 |
+
<p>Authenticating… please wait.</p>
|