Skip to content

Commit

Permalink
chore: Add tracing
Browse files Browse the repository at this point in the history
  • Loading branch information
Brayan-724 committed Sep 14, 2024
1 parent 7300a88 commit 6fca7bb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
12 changes: 9 additions & 3 deletions crates/backend/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,17 @@ pub fn get_device_id(req: &worker::Request) -> Option<String> {
.ok()
.flatten()
.or_else(|| req.headers().get("x-forwarded-for").ok().flatten())
.or_else(|| req.cf().and_then(|cf| cf.city()))?;
.or_else(|| req.cf().and_then(|cf| cf.city()))
.inspect(|x| worker::console_log!("DeviceID-SiteID: {x:?}"))?;

let user_agent = req.headers().get("user-agent").ok().flatten()?;
let user_agent = req
.headers()
.get("user-agent")
.ok()
.flatten()
.inspect(|x| worker::console_log!("DeviceID-UserAgent: {x:?}"))?;

worker::console_log!("{site_id} + {user_agent}");
worker::console_log!("DeviceID: {site_id} + {user_agent}");

Some(site_id + &user_agent)
}
Expand Down
17 changes: 12 additions & 5 deletions frontend/src/routes/form/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { ApiResponse } from "$lib/models/api";
import { error, redirect } from "@sveltejs/kit";

export async function load(
{ url, params, platform, cookies }: App.LoadServerEvent<{ slug: string }>,
{ url, params, platform, cookies, request }: App.LoadServerEvent<{ slug: string }>,
) {
if (url.searchParams.has("code")) {
const code = url.searchParams.get("code")!;
Expand All @@ -20,9 +20,11 @@ export async function load(
console.log(`Loading form ${params.slug} from: ${FORM_URL}`);
const form_res = await fetch(FORM_URL);
const form = await form_res.json() as ApiResponse<Form>;
console.log("Form loaded:", form);

if (form.success) {
const code = cookies.get("external-code");
console.log("User has code: ", !!code)

if (form.data.require_login && !code) {
return {
Expand All @@ -31,15 +33,20 @@ export async function load(
};
}

console.log(`Loading session from: ${FORM_URL}/session`);
const session_res = await fetch(`${FORM_URL}/session`, {
headers: code
? {
"Authorization": `Bearer ${code}`,
"user-agent": request.headers.get("user-agent").
}
: {},
: {
"user-agent": request.headers.get("user-agent").
},
});

const session = await session_res.json() as ApiResponse<string>;
console.log("Session loaded:", session);

if (session.success) {
const user = session.data!;
Expand All @@ -65,10 +72,10 @@ export async function load(
user,
answers: [],
};
}

if (session_res.status === 403) {
} else if (session_res.status === 403) {
return error(session_res.status, "Already answered");
} else if (!session.success) {
return error(session_res.status, session.errors[0]);
}

return {
Expand Down

0 comments on commit 6fca7bb

Please sign in to comment.