@designsystemsinternational/cloudflare 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/README.md +60 -0
  2. package/cli/index.ts +196 -0
  3. package/package.json +32 -0
  4. package/templates/dist/auth/assets/index-BqXcY2TH.css +1 -0
  5. package/templates/dist/auth/assets/index-TTeq04ep.js +9 -0
  6. package/templates/dist/auth/fonts/UniversNextPro-Medium.woff2 +0 -0
  7. package/templates/dist/auth/fonts/UniversNextPro-Regular.woff2 +0 -0
  8. package/templates/dist/auth/fonts/UniversNextTypewrtrPro-Rg.woff2 +0 -0
  9. package/templates/dist/auth/index.html +14 -0
  10. package/templates/dist/index.html +6 -0
  11. package/templates/eslint.config.js +23 -0
  12. package/templates/index.html +12 -0
  13. package/templates/package-lock.json +5394 -0
  14. package/templates/package.json +33 -0
  15. package/templates/postcss.config.cjs +19 -0
  16. package/templates/public/fonts/UniversNextPro-Medium.woff2 +0 -0
  17. package/templates/public/fonts/UniversNextPro-Regular.woff2 +0 -0
  18. package/templates/public/fonts/UniversNextTypewrtrPro-Rg.woff2 +0 -0
  19. package/templates/src/App.module.css +116 -0
  20. package/templates/src/App.tsx +116 -0
  21. package/templates/src/assets/Cloudflare_Logo.svg +51 -0
  22. package/templates/src/assets/react.svg +1 -0
  23. package/templates/src/main.tsx +16 -0
  24. package/templates/src/styles/breakpoints.css +9 -0
  25. package/templates/src/styles/fonts.css +17 -0
  26. package/templates/src/styles/globals.css +14 -0
  27. package/templates/src/styles/reset.css +438 -0
  28. package/templates/src/styles/textStyles.css +19 -0
  29. package/templates/src/styles/utilities.css +5 -0
  30. package/templates/src/styles/variables.css +195 -0
  31. package/templates/src/vite-env.d.ts +1 -0
  32. package/templates/tsconfig.app.json +28 -0
  33. package/templates/tsconfig.json +7 -0
  34. package/templates/tsconfig.node.json +26 -0
  35. package/templates/vite.config.ts +15 -0
  36. package/tsconfig.json +10 -0
  37. package/worker-configuration.d.ts +10851 -0
  38. package/workers/password/index.ts +67 -0
  39. package/workers/password/lib/constants.ts +1 -0
  40. package/workers/password/lib/schema.ts +8 -0
  41. package/workers/password/lib/util.ts +28 -0
  42. package/wrangler.jsonc +16 -0
@@ -0,0 +1,67 @@
1
+ import { envSchema } from "./lib/schema";
2
+ import { getAuthCookieHeaders, isAuthenticated } from "./lib/util";
3
+
4
+ export default {
5
+ async fetch(request, _env) {
6
+ const envRes = envSchema.safeParse(_env);
7
+
8
+ if (!envRes.success) {
9
+ console.error(envRes.error.issues);
10
+ return new Response(null, { status: 404 });
11
+ }
12
+
13
+ const env = envRes.data;
14
+ const url = new URL(request.url);
15
+ const authenticated = await isAuthenticated(request, env);
16
+
17
+ /**
18
+ * Authenticate endpoint
19
+ */
20
+ if (url.pathname.startsWith("/auth/authenticate")) {
21
+ if (authenticated) {
22
+ return Response.json({ success: true });
23
+ }
24
+
25
+ const formData = await request.formData();
26
+ const password = formData.get("password") as string;
27
+
28
+ if (password === env.PASSWORD) {
29
+ const headers = await getAuthCookieHeaders(env);
30
+ return Response.json(
31
+ {
32
+ success: true,
33
+ },
34
+ { headers }
35
+ );
36
+ }
37
+
38
+ return Response.json(
39
+ {
40
+ success: false,
41
+ },
42
+ { status: 404 }
43
+ );
44
+ }
45
+
46
+ /**
47
+ * Always render auth pages without auth
48
+ */
49
+ if (url.pathname.startsWith("/auth")) {
50
+ console.log("Auth page request:", request.url);
51
+ return _env.ASSETS.fetch(request);
52
+ }
53
+
54
+ /**
55
+ * All other requests
56
+ */
57
+ if (!authenticated) {
58
+ const authUrl = new URL("/auth", request.url);
59
+ const nextPath = url.pathname + url.search;
60
+ authUrl.searchParams.set("next", nextPath);
61
+ console.log("Redirecting to:", authUrl.toString());
62
+ return Response.redirect(authUrl.toString(), 302);
63
+ }
64
+
65
+ return _env.ASSETS.fetch(request);
66
+ },
67
+ } satisfies ExportedHandler<Env>;
@@ -0,0 +1 @@
1
+ export const COOKIE_NAME = "dsi-auth";
@@ -0,0 +1,8 @@
1
+ import { z } from "zod";
2
+
3
+ export const envSchema = z.object({
4
+ SECRET: z.string().min(6),
5
+ PASSWORD: z.string().min(1),
6
+ });
7
+
8
+ export type EnvSchema = z.infer<typeof envSchema>;
@@ -0,0 +1,28 @@
1
+ import * as cookie from "worktop/cookie";
2
+ import jwt from "@tsndr/cloudflare-worker-jwt";
3
+ import { COOKIE_NAME } from "./constants";
4
+ import type { EnvSchema } from "./schema";
5
+
6
+ export const isAuthenticated = async (request: Request, env: EnvSchema) => {
7
+ const cookieHeader = request.headers.get("Cookie") ?? "";
8
+ const cookies = cookie.parse(cookieHeader);
9
+ const token = cookies[COOKIE_NAME];
10
+ if (!token) {
11
+ return false;
12
+ }
13
+ return await jwt.verify(token, env.SECRET);
14
+ };
15
+
16
+ export const getAuthCookieHeaders = async (env: EnvSchema) => {
17
+ const payload = await jwt.sign({ iat: Date.now() }, env.SECRET);
18
+ const headers = new Headers();
19
+ const cookieStr = cookie.stringify(COOKIE_NAME, payload, {
20
+ path: "/",
21
+ httponly: true,
22
+ samesite: "Lax",
23
+ secure: true,
24
+ });
25
+
26
+ headers.set("Set-Cookie", cookieStr);
27
+ return headers;
28
+ };
package/wrangler.jsonc ADDED
@@ -0,0 +1,16 @@
1
+ // This file is simply used to test the package locally with workerd
2
+ {
3
+ "$schema": "node_modules/wrangler/config-schema.json",
4
+ "name": "cloudflare-password",
5
+ "main": "workers/password/index.ts",
6
+ "compatibility_date": "2025-12-02",
7
+ "assets": {
8
+ "directory": "./templates/dist",
9
+ "not_found_handling": "single-page-application",
10
+ "binding": "ASSETS",
11
+ "run_worker_first": true
12
+ },
13
+ "observability": {
14
+ "enabled": true
15
+ }
16
+ }