From b1e949dce27b991ea360e0de3fa73a90abd910e6 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Sat, 11 Jan 2025 14:45:37 +0100 Subject: [PATCH] enhance: detect requests of pwa --- src/index.ts | 1 + src/middleware/detectPWA.ts | 11 +++++++++++ src/routes/index.ts | 2 ++ 3 files changed, 14 insertions(+) create mode 100644 src/middleware/detectPWA.ts diff --git a/src/index.ts b/src/index.ts index a1d7178..f5a73e6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,7 @@ declare global { username: string; isOwner: boolean; permissions: PermissionObject; + isPWA: boolean; } } } diff --git a/src/middleware/detectPWA.ts b/src/middleware/detectPWA.ts new file mode 100644 index 0000000..d30df95 --- /dev/null +++ b/src/middleware/detectPWA.ts @@ -0,0 +1,11 @@ +import { Request, Response } from "express"; + +export default async function detectPWA(req: Request, res: Response, next: Function) { + const userAgent = req.headers["user-agent"] || ""; + if ((userAgent.includes("Mobile") && userAgent.includes("Standalone")) || req.headers["x-pwa-client"] === "true") { + req.isPWA = true; + } else { + req.isPWA = false; + } + next(); +} diff --git a/src/routes/index.ts b/src/routes/index.ts index 72cb526..b939d52 100644 --- a/src/routes/index.ts +++ b/src/routes/index.ts @@ -13,6 +13,7 @@ import reset from "./reset"; import auth from "./auth"; import admin from "./admin/index"; import user from "./user"; +import detectPWA from "../middleware/detectPWA"; export default (app: Express) => { app.set("query parser", "extended"); @@ -25,6 +26,7 @@ export default (app: Express) => { app.use(cors()); app.options("*", cors()); + app.use(detectPWA); app.use("/api/public", publicAvailable); app.use("/api/setup", allowSetup, setup); app.use("/api/reset", reset);