Skip to content

Commit 704b0da

Browse files
committed
WIP on auth, having problems with it
1 parent 259cfc5 commit 704b0da

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed
Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
import type { LoaderFunctionArgs } from "@remix-run/node";
22
import { env } from "~/env.server";
3+
import { logger } from "~/services/logger.server";
4+
import { getUserId, requireUserId } from "~/services/session.server";
5+
import { longPollingFetch } from "~/utils/longPollingFetch";
36

47
export async function loader({ params, request }: LoaderFunctionArgs) {
8+
const userId = await getUserId(request);
9+
10+
logger.log(`/sync/traces/${params.traceId}`, { userId });
11+
12+
if (!userId) {
13+
return new Response("authorization header not found", { status: 401 });
14+
}
15+
16+
//todo check the user has access to this trace
17+
518
const url = new URL(request.url);
619
const originUrl = new URL(`${env.ELECTRIC_ORIGIN}/v1/shape/public."TaskEvent"`);
720
url.searchParams.forEach((value, key) => {
@@ -10,21 +23,5 @@ export async function loader({ params, request }: LoaderFunctionArgs) {
1023

1124
originUrl.searchParams.set("where", `"traceId"='${params.traceId}'`);
1225

13-
// When proxying long-polling requests, content-encoding & content-length are added
14-
// erroneously (saying the body is gzipped when it's not) so we'll just remove
15-
// them to avoid content decoding errors in the browser.
16-
//
17-
// Similar-ish problem to https://github.com/wintercg/fetch/issues/23
18-
let response = await fetch(originUrl.toString());
19-
if (response.headers.get(`content-encoding`)) {
20-
const headers = new Headers(response.headers);
21-
headers.delete(`content-encoding`);
22-
headers.delete(`content-length`);
23-
response = new Response(response.body, {
24-
status: response.status,
25-
statusText: response.statusText,
26-
headers,
27-
});
28-
}
29-
return response;
26+
return longPollingFetch(originUrl.toString());
3027
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// When proxying long-polling requests, content-encoding & content-length are added
2+
// erroneously (saying the body is gzipped when it's not) so we'll just remove
3+
// them to avoid content decoding errors in the browser.
4+
//
5+
// Similar-ish problem to https://github.com/wintercg/fetch/issues/23
6+
export async function longPollingFetch(url: string, options?: RequestInit) {
7+
let response = await fetch(url, options);
8+
if (response.headers.get(`content-encoding`)) {
9+
const headers = new Headers(response.headers);
10+
headers.delete(`content-encoding`);
11+
headers.delete(`content-length`);
12+
response = new Response(response.body, {
13+
status: response.status,
14+
statusText: response.statusText,
15+
headers,
16+
});
17+
}
18+
return response;
19+
}

0 commit comments

Comments
 (0)