create-authhero 0.7.0 → 0.9.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 (28) hide show
  1. package/dist/cloudflare-multitenant/.dev.vars.example +20 -4
  2. package/dist/cloudflare-multitenant/README.md +238 -89
  3. package/dist/cloudflare-multitenant/drizzle.config.ts +8 -0
  4. package/dist/cloudflare-multitenant/migrations/0000_init.sql +782 -0
  5. package/dist/cloudflare-multitenant/migrations/meta/_journal.json +13 -0
  6. package/dist/cloudflare-multitenant/seed-helper.js +75 -0
  7. package/dist/cloudflare-multitenant/src/app.ts +3 -22
  8. package/dist/cloudflare-multitenant/src/db/schema.ts +845 -0
  9. package/dist/cloudflare-multitenant/src/index.ts +61 -55
  10. package/dist/cloudflare-multitenant/src/seed.ts +64 -0
  11. package/dist/cloudflare-multitenant/src/types.ts +17 -17
  12. package/dist/cloudflare-multitenant/wrangler.toml +40 -28
  13. package/dist/cloudflare-simple/.dev.vars.example +20 -0
  14. package/dist/cloudflare-simple/README.md +246 -10
  15. package/dist/cloudflare-simple/drizzle.config.ts +8 -0
  16. package/dist/cloudflare-simple/migrations/0000_init.sql +782 -0
  17. package/dist/cloudflare-simple/migrations/meta/0000_snapshot.json +5325 -0
  18. package/dist/cloudflare-simple/migrations/meta/_journal.json +13 -0
  19. package/dist/cloudflare-simple/seed-helper.js +75 -0
  20. package/dist/cloudflare-simple/src/app.ts +10 -0
  21. package/dist/cloudflare-simple/src/db/schema.ts +845 -0
  22. package/dist/cloudflare-simple/src/index.ts +64 -13
  23. package/dist/cloudflare-simple/src/seed.ts +64 -0
  24. package/dist/cloudflare-simple/src/types.ts +18 -0
  25. package/dist/cloudflare-simple/wrangler.toml +29 -0
  26. package/dist/create-authhero.js +199 -131
  27. package/package.json +1 -1
  28. package/dist/cloudflare-multitenant/src/database-factory.ts +0 -220
@@ -1,27 +1,78 @@
1
- import { OpenAPIHono } from "@hono/zod-openapi";
2
1
  import { D1Dialect } from "kysely-d1";
3
2
  import { Kysely } from "kysely";
4
3
  import createAdapters from "@authhero/kysely-adapter";
5
4
  import createApp from "./app";
6
5
  import { Env } from "./types";
7
- import { AuthHeroConfig, Bindings, Variables } from "authhero";
6
+ import { AuthHeroConfig } from "authhero";
8
7
 
9
- let app: OpenAPIHono<{ Bindings: Bindings; Variables: Variables }> | undefined;
8
+ // ──────────────────────────────────────────────────────────────────────────────
9
+ // OPTIONAL: Uncomment to enable Cloudflare adapters (Analytics Engine, etc.)
10
+ // ──────────────────────────────────────────────────────────────────────────────
11
+ // import createCloudflareAdapters from "@authhero/cloudflare-adapter";
10
12
 
11
13
  export default {
12
14
  async fetch(request: Request, env: Env): Promise<Response> {
13
- if (!app) {
14
- const dialect = new D1Dialect({ database: env.AUTH_DB });
15
- const db = new Kysely<any>({ dialect });
16
- const dataAdapter = createAdapters(db);
15
+ const url = new URL(request.url);
16
+ const issuer = `${url.protocol}//${url.host}/`;
17
17
 
18
- const config: AuthHeroConfig = {
19
- dataAdapter,
20
- };
18
+ // Get the origin from the request for dynamic CORS
19
+ const origin = request.headers.get("Origin") || "";
21
20
 
22
- app = createApp(config);
23
- }
21
+ const dialect = new D1Dialect({ database: env.AUTH_DB });
22
+ const db = new Kysely<any>({ dialect });
23
+ const dataAdapter = createAdapters(db);
24
24
 
25
- return app.fetch(request, env);
25
+ // ────────────────────────────────────────────────────────────────────────
26
+ // OPTIONAL: Cloudflare Analytics Engine for centralized logging
27
+ // Uncomment to enable:
28
+ // ────────────────────────────────────────────────────────────────────────
29
+ // const cloudflareAdapters = createCloudflareAdapters({
30
+ // accountId: env.CLOUDFLARE_ACCOUNT_ID,
31
+ // apiToken: env.CLOUDFLARE_API_TOKEN,
32
+ // analyticsEngineLogs: {
33
+ // analyticsEngineBinding: env.AUTH_LOGS,
34
+ // accountId: env.CLOUDFLARE_ACCOUNT_ID,
35
+ // apiToken: env.ANALYTICS_ENGINE_API_TOKEN || env.CLOUDFLARE_API_TOKEN,
36
+ // dataset: "authhero_logs",
37
+ // },
38
+ // });
39
+
40
+ // ────────────────────────────────────────────────────────────────────────
41
+ // OPTIONAL: Rate Limiting
42
+ // Uncomment to enable rate limiting on authentication endpoints:
43
+ // ────────────────────────────────────────────────────────────────────────
44
+ // const clientIp = request.headers.get("CF-Connecting-IP") || "unknown";
45
+ // const { success } = await env.RATE_LIMITER.limit({ key: clientIp });
46
+ // if (!success) {
47
+ // return new Response("Rate limit exceeded", { status: 429 });
48
+ // }
49
+
50
+ const config: AuthHeroConfig = {
51
+ dataAdapter,
52
+ // ──────────────────────────────────────────────────────────────────────
53
+ // OPTIONAL: Spread Cloudflare adapters to enable Analytics Engine logging
54
+ // Uncomment when using createCloudflareAdapters above:
55
+ // ──────────────────────────────────────────────────────────────────────
56
+ // ...cloudflareAdapters,
57
+
58
+ // Allow CORS for the Management API from admin UIs
59
+ allowedOrigins: [
60
+ "http://localhost:5173",
61
+ "https://localhost:3000",
62
+ "https://manage.authhero.net",
63
+ "https://local.authhero.net",
64
+ origin,
65
+ ].filter(Boolean),
66
+ };
67
+
68
+ const app = createApp(config);
69
+
70
+ // Pass the issuer via env bindings
71
+ const envWithIssuer = {
72
+ ...env,
73
+ ISSUER: issuer,
74
+ };
75
+
76
+ return app.fetch(request, envWithIssuer);
26
77
  },
27
78
  };
@@ -0,0 +1,64 @@
1
+ import { D1Dialect } from "kysely-d1";
2
+ import { Kysely } from "kysely";
3
+ import createAdapters from "@authhero/kysely-adapter";
4
+ import { seed } from "authhero";
5
+
6
+ interface Env {
7
+ AUTH_DB: D1Database;
8
+ }
9
+
10
+ export default {
11
+ async fetch(request: Request, env: Env): Promise<Response> {
12
+ const url = new URL(request.url);
13
+ const adminEmail = url.searchParams.get("email");
14
+ const adminPassword = url.searchParams.get("password");
15
+
16
+ if (!adminEmail || !adminPassword) {
17
+ return new Response(
18
+ JSON.stringify({
19
+ error: "Missing email or password query parameters",
20
+ usage: "/?email=admin@example.com&password=yourpassword",
21
+ }),
22
+ {
23
+ status: 400,
24
+ headers: { "Content-Type": "application/json" },
25
+ },
26
+ );
27
+ }
28
+
29
+ try {
30
+ const dialect = new D1Dialect({ database: env.AUTH_DB });
31
+ const db = new Kysely<any>({ dialect });
32
+ const adapters = createAdapters(db);
33
+
34
+ const result = await seed(adapters, {
35
+ adminEmail,
36
+ adminPassword,
37
+ });
38
+
39
+ return new Response(
40
+ JSON.stringify({
41
+ success: true,
42
+ message: "Database seeded successfully",
43
+ result,
44
+ }),
45
+ {
46
+ status: 200,
47
+ headers: { "Content-Type": "application/json" },
48
+ },
49
+ );
50
+ } catch (error) {
51
+ console.error("Seed error:", error);
52
+ return new Response(
53
+ JSON.stringify({
54
+ error: "Failed to seed database",
55
+ message: error instanceof Error ? error.message : String(error),
56
+ }),
57
+ {
58
+ status: 500,
59
+ headers: { "Content-Type": "application/json" },
60
+ },
61
+ );
62
+ }
63
+ },
64
+ };
@@ -1,5 +1,23 @@
1
1
  /// <reference types="@cloudflare/workers-types" />
2
2
 
3
+ // Uncomment to enable Analytics Engine logging:
4
+ // import { AnalyticsEngineDataset } from "@authhero/cloudflare-adapter";
5
+
3
6
  export interface Env {
4
7
  AUTH_DB: D1Database;
8
+
9
+ // ──────────────────────────────────────────────────────────────────────────
10
+ // OPTIONAL: Analytics Engine for centralized logging
11
+ // Uncomment to enable:
12
+ // ──────────────────────────────────────────────────────────────────────────
13
+ // AUTH_LOGS: AnalyticsEngineDataset;
14
+ // CLOUDFLARE_ACCOUNT_ID: string;
15
+ // CLOUDFLARE_API_TOKEN: string;
16
+ // ANALYTICS_ENGINE_API_TOKEN?: string; // Optional: separate token for Analytics Engine
17
+
18
+ // ──────────────────────────────────────────────────────────────────────────
19
+ // OPTIONAL: Rate Limiting
20
+ // Uncomment to enable:
21
+ // ──────────────────────────────────────────────────────────────────────────
22
+ // RATE_LIMITER: RateLimiter;
5
23
  }
@@ -9,12 +9,41 @@ compatibility_date = "2024-11-20"
9
9
  # binding = "AUTH_DB"
10
10
  # database_name = "authhero-db"
11
11
  # database_id = "<YOUR_DATABASE_ID>"
12
+ # migrations_dir = "migrations"
12
13
 
13
14
  # For local development, you can use a local D1 database:
14
15
  [[d1_databases]]
15
16
  binding = "AUTH_DB"
16
17
  database_name = "authhero-db"
17
18
  database_id = "local"
19
+ migrations_dir = "migrations"
20
+
21
+ # ════════════════════════════════════════════════════════════════════════════
22
+ # OPTIONAL: Analytics Engine for centralized logging
23
+ # ════════════════════════════════════════════════════════════════════════════
24
+ # To enable Analytics Engine:
25
+ # 1. Create dataset in Cloudflare Dashboard: Workers & Pages > Analytics Engine
26
+ # 2. Uncomment the binding below
27
+ # 3. Add CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN to .dev.vars
28
+ # 4. Uncomment the Analytics Engine code in src/index.ts
29
+ #
30
+ # [[analytics_engine_datasets]]
31
+ # binding = "AUTH_LOGS"
32
+ # dataset = "authhero_logs"
33
+
34
+ # ════════════════════════════════════════════════════════════════════════════
35
+ # OPTIONAL: Rate Limiting
36
+ # ════════════════════════════════════════════════════════════════════════════
37
+ # To enable Rate Limiting:
38
+ # 1. Rate limiting is available on Workers Paid plan ($5/month)
39
+ # 2. Uncomment the binding below
40
+ # 3. Implement rate limiting logic in src/index.ts
41
+ #
42
+ # [[unsafe.bindings]]
43
+ # name = "RATE_LIMITER"
44
+ # type = "ratelimit"
45
+ # namespace_id = "1001" # Unique namespace ID for this limiter
46
+ # simple = { limit = 100, period = 60 } # 100 requests per 60 seconds
18
47
 
19
48
  # Optional: Enable observability
20
49
  # [observability]