Skip to content

Commit

Permalink
fix: Device id was unproperly resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
Brayan-724 committed Sep 24, 2024
1 parent 6872baa commit b15fdea
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 58 deletions.
23 changes: 4 additions & 19 deletions crates/backend/src/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,10 @@ use crate::shared::{error_wrapper, needs_auth};
use crate::RouterContext;

pub fn get_device_id(req: &worker::Request) -> Option<String> {
let site_id = req
.headers()
.get("cf-connecting-ip")
.ok()
.flatten()
.or_else(|| req.headers().get("x-forwarded-for").ok().flatten())
.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()
.inspect(|x| worker::console_log!("DeviceID-UserAgent: {x:?}"))?;

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

Some(site_id + &user_agent)
req.headers()
.get("X-device-id")
.inspect_err(|err| worker::console_error!("DeviceId Error: {err}"))
.ok()?
}

pub fn get_device_id_hash(req: &worker::Request) -> Option<String> {
Expand Down
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
},
"type": "module",
"dependencies": {
"nanoid": "^5.0.7",
"svelte-canvas-confetti": "^0.6.0"
}
}
10 changes: 10 additions & 0 deletions frontend/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions frontend/src/lib/service/device_id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { nanoid } from "nanoid";

const DEVICE_ID = "DEVICE_ID";

let id = localStorage.getItem(DEVICE_ID)

if (typeof id !== "string") {
id = nanoid();

localStorage.setItem(DEVICE_ID, id);
}
39 changes: 0 additions & 39 deletions frontend/src/lib/service/session.ts

This file was deleted.

11 changes: 11 additions & 0 deletions frontend/src/routes/form/[slug]/+page.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Answer } from "$lib/form/models/Answer.d";
import type { Form } from "$lib/form/models/Form";
import type { ApiResponse } from "$lib/models/api";
import { error, redirect } from "@sveltejs/kit";
import { nanoid } from "nanoid";

export async function load(
{ url, params, platform, cookies, request }: App.LoadServerEvent<{ slug: string }>,
Expand All @@ -15,6 +16,14 @@ export async function load(
return redirect(301, `/form/${params.slug}`);
}

let device_id = cookies.get("device-id");

if (!device_id) {
device_id = nanoid();

cookies.set("device-id", device_id, { path: "/" });
}

const FORM_URL = `${platform!.env.API_HOST}/api/form/${params.slug}`;

console.log(`Loading form ${params.slug} from: ${FORM_URL}`);
Expand All @@ -39,9 +48,11 @@ export async function load(
? {
"Authorization": `Bearer ${code}`,
"user-agent": request.headers.get("user-agent"),
"X-device-id": device_id,
}
: {
"user-agent": request.headers.get("user-agent"),
"X-device-id": device_id,
},
});

Expand Down

0 comments on commit b15fdea

Please sign in to comment.