@lastshotlabs/bunshot 0.0.13 → 0.0.18

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 (123) hide show
  1. package/README.md +2816 -1747
  2. package/dist/adapters/memoryAuth.d.ts +7 -0
  3. package/dist/adapters/memoryAuth.js +177 -2
  4. package/dist/adapters/mongoAuth.js +94 -0
  5. package/dist/adapters/sqliteAuth.d.ts +9 -0
  6. package/dist/adapters/sqliteAuth.js +190 -2
  7. package/dist/app.d.ts +120 -2
  8. package/dist/app.js +104 -4
  9. package/dist/entrypoints/queue.d.ts +2 -2
  10. package/dist/entrypoints/queue.js +1 -1
  11. package/dist/index.d.ts +24 -8
  12. package/dist/index.js +15 -5
  13. package/dist/lib/appConfig.d.ts +81 -0
  14. package/dist/lib/appConfig.js +30 -0
  15. package/dist/lib/authAdapter.d.ts +54 -0
  16. package/dist/lib/authRateLimit.d.ts +2 -0
  17. package/dist/lib/authRateLimit.js +4 -0
  18. package/dist/lib/clientIp.d.ts +14 -0
  19. package/dist/lib/clientIp.js +52 -0
  20. package/dist/lib/constants.d.ts +4 -0
  21. package/dist/lib/constants.js +4 -0
  22. package/dist/lib/context.d.ts +2 -0
  23. package/dist/lib/createDtoMapper.d.ts +33 -0
  24. package/dist/lib/createDtoMapper.js +69 -0
  25. package/dist/lib/crypto.d.ts +11 -0
  26. package/dist/lib/crypto.js +22 -0
  27. package/dist/lib/emailVerification.d.ts +4 -0
  28. package/dist/lib/emailVerification.js +20 -12
  29. package/dist/lib/jwt.d.ts +1 -1
  30. package/dist/lib/jwt.js +19 -6
  31. package/dist/lib/mfaChallenge.d.ts +42 -0
  32. package/dist/lib/mfaChallenge.js +293 -0
  33. package/dist/lib/oauth.d.ts +14 -1
  34. package/dist/lib/oauth.js +19 -1
  35. package/dist/lib/oauthCode.d.ts +15 -0
  36. package/dist/lib/oauthCode.js +90 -0
  37. package/dist/lib/queue.d.ts +33 -0
  38. package/dist/lib/queue.js +98 -0
  39. package/dist/lib/resetPassword.js +12 -16
  40. package/dist/lib/roles.d.ts +4 -0
  41. package/dist/lib/roles.js +27 -0
  42. package/dist/lib/session.d.ts +12 -0
  43. package/dist/lib/session.js +165 -5
  44. package/dist/lib/tenant.d.ts +15 -0
  45. package/dist/lib/tenant.js +65 -0
  46. package/dist/lib/ws.js +5 -1
  47. package/dist/lib/zodToMongoose.d.ts +38 -0
  48. package/dist/lib/zodToMongoose.js +84 -0
  49. package/dist/middleware/bearerAuth.js +4 -3
  50. package/dist/middleware/botProtection.js +2 -2
  51. package/dist/middleware/cacheResponse.d.ts +1 -0
  52. package/dist/middleware/cacheResponse.js +18 -3
  53. package/dist/middleware/cors.d.ts +2 -0
  54. package/dist/middleware/cors.js +22 -8
  55. package/dist/middleware/csrf.d.ts +18 -0
  56. package/dist/middleware/csrf.js +115 -0
  57. package/dist/middleware/rateLimit.d.ts +2 -1
  58. package/dist/middleware/rateLimit.js +7 -5
  59. package/dist/middleware/requireRole.d.ts +14 -3
  60. package/dist/middleware/requireRole.js +46 -6
  61. package/dist/middleware/tenant.d.ts +5 -0
  62. package/dist/middleware/tenant.js +116 -0
  63. package/dist/models/AuthUser.d.ts +17 -0
  64. package/dist/models/AuthUser.js +17 -0
  65. package/dist/models/TenantRole.d.ts +15 -0
  66. package/dist/models/TenantRole.js +23 -0
  67. package/dist/routes/auth.d.ts +5 -3
  68. package/dist/routes/auth.js +173 -30
  69. package/dist/routes/jobs.d.ts +2 -0
  70. package/dist/routes/jobs.js +270 -0
  71. package/dist/routes/mfa.d.ts +5 -0
  72. package/dist/routes/mfa.js +616 -0
  73. package/dist/routes/oauth.js +378 -23
  74. package/dist/schemas/auth.d.ts +2 -0
  75. package/dist/schemas/auth.js +22 -1
  76. package/dist/server.d.ts +6 -0
  77. package/dist/server.js +19 -3
  78. package/dist/services/auth.d.ts +18 -5
  79. package/dist/services/auth.js +112 -18
  80. package/dist/services/mfa.d.ts +84 -0
  81. package/dist/services/mfa.js +543 -0
  82. package/dist/ws/index.js +3 -2
  83. package/docs/sections/adding-middleware/full.md +35 -0
  84. package/docs/sections/adding-models/full.md +125 -0
  85. package/docs/sections/adding-models/overview.md +13 -0
  86. package/docs/sections/adding-routes/full.md +182 -0
  87. package/docs/sections/adding-routes/overview.md +23 -0
  88. package/docs/sections/auth-flow/full.md +634 -0
  89. package/docs/sections/auth-flow/overview.md +10 -0
  90. package/docs/sections/cli/full.md +30 -0
  91. package/docs/sections/configuration/full.md +155 -0
  92. package/docs/sections/configuration/overview.md +17 -0
  93. package/docs/sections/configuration-example/full.md +117 -0
  94. package/docs/sections/configuration-example/overview.md +30 -0
  95. package/docs/sections/documentation/full.md +171 -0
  96. package/docs/sections/environment-variables/full.md +55 -0
  97. package/docs/sections/exports/full.md +92 -0
  98. package/docs/sections/extending-context/full.md +59 -0
  99. package/docs/sections/header.md +3 -0
  100. package/docs/sections/installation/full.md +6 -0
  101. package/docs/sections/jobs/full.md +140 -0
  102. package/docs/sections/jobs/overview.md +15 -0
  103. package/docs/sections/mongodb-connections/full.md +45 -0
  104. package/docs/sections/mongodb-connections/overview.md +7 -0
  105. package/docs/sections/multi-tenancy/full.md +66 -0
  106. package/docs/sections/multi-tenancy/overview.md +15 -0
  107. package/docs/sections/oauth/full.md +189 -0
  108. package/docs/sections/oauth/overview.md +16 -0
  109. package/docs/sections/package-development/full.md +7 -0
  110. package/docs/sections/peer-dependencies/full.md +47 -0
  111. package/docs/sections/quick-start/full.md +43 -0
  112. package/docs/sections/response-caching/full.md +117 -0
  113. package/docs/sections/response-caching/overview.md +13 -0
  114. package/docs/sections/roles/full.md +136 -0
  115. package/docs/sections/roles/overview.md +12 -0
  116. package/docs/sections/running-without-redis/full.md +16 -0
  117. package/docs/sections/running-without-redis-or-mongodb/full.md +60 -0
  118. package/docs/sections/stack/full.md +10 -0
  119. package/docs/sections/websocket/full.md +101 -0
  120. package/docs/sections/websocket/overview.md +5 -0
  121. package/docs/sections/websocket-rooms/full.md +97 -0
  122. package/docs/sections/websocket-rooms/overview.md +5 -0
  123. package/package.json +30 -9
@@ -0,0 +1,59 @@
1
+ ## Extending the Context (Custom Variables)
2
+
3
+ When building a tenant app or any app that needs extra typed context variables (beyond the built-in), extend `AppEnv["Variables"]` and create a typed router factory.
4
+
5
+ ```ts
6
+ // src/lib/context.ts
7
+ import { createRouter as coreCreateRouter, type AppEnv } from "@lastshotlabs/bunshot";
8
+ import type { OpenAPIHono } from "@hono/zod-openapi";
9
+
10
+ export type MyVariables = AppEnv["Variables"] & {
11
+ tenantId: string;
12
+ };
13
+
14
+ export type MyEnv = { Variables: MyVariables };
15
+
16
+ export const createRouter = () => coreCreateRouter() as unknown as OpenAPIHono<MyEnv>;
17
+ ```
18
+
19
+ Use the local `createRouter` instead of the one from the package — your routes will then have full TypeScript access to the extra variables:
20
+
21
+ ```ts
22
+ // src/routes/items.ts
23
+ import { createRouter } from "../lib/context";
24
+ import { userAuth } from "@lastshotlabs/bunshot";
25
+
26
+ export const router = createRouter();
27
+
28
+ router.use("/items", userAuth);
29
+
30
+ router.get("/items", async (c) => {
31
+ const tenantId = c.get("tenantId"); // fully typed
32
+ const userId = c.get("userId"); // still available from AppEnv
33
+ return c.json({ tenantId, userId });
34
+ });
35
+ ```
36
+
37
+ Populate the extra variables from a global middleware:
38
+
39
+ ```ts
40
+ // src/middleware/tenant.ts
41
+ import type { MiddlewareHandler } from "hono";
42
+ import type { MyEnv } from "../lib/context";
43
+
44
+ export const tenantMiddleware: MiddlewareHandler<MyEnv> = async (c, next) => {
45
+ const tenantId = c.req.header("x-tenant-id") ?? "default";
46
+ c.set("tenantId", tenantId);
47
+ await next();
48
+ };
49
+ ```
50
+
51
+ Then register it in `createServer`:
52
+
53
+ ```ts
54
+ await createServer({
55
+ routesDir: import.meta.dir + "/routes",
56
+ app: { name: "My App", version: "1.0.0" },
57
+ middleware: [tenantMiddleware],
58
+ });
59
+ ```
@@ -0,0 +1,3 @@
1
+ # Bunshot by Last Shot Labs
2
+
3
+ A personal Bun + Hono API framework. Install it in any app and get auth, sessions, rate limiting, WebSocket, queues, and OpenAPI docs out of the box — then add your own routes, workers, models, and services.
@@ -0,0 +1,6 @@
1
+ ## Installation
2
+
3
+ ```bash
4
+ # from npm
5
+ bun add @lastshotlabs/bunshot
6
+ ```
@@ -0,0 +1,140 @@
1
+ ## Jobs (BullMQ)
2
+
3
+ > **Redis requirement**: BullMQ requires `maxmemory-policy noeviction`. Set it in `redis.conf` or via Docker:
4
+ > ```yaml
5
+ > command: redis-server --maxmemory-policy noeviction
6
+ > ```
7
+
8
+ Queues and workers share the existing Redis connection automatically.
9
+
10
+ ### Define a queue
11
+
12
+ ```ts
13
+ // src/queues/email.ts
14
+ import { createQueue } from "@lastshotlabs/bunshot";
15
+
16
+ export type EmailJob = { to: string; subject: string; body: string };
17
+
18
+ export const emailQueue = createQueue<EmailJob>("email");
19
+ ```
20
+
21
+ ### Add jobs
22
+
23
+ ```ts
24
+ import { emailQueue } from "../queues/email";
25
+
26
+ await emailQueue.add("send-welcome", { to: "user@example.com", subject: "Welcome", body: "..." });
27
+
28
+ // with options
29
+ await emailQueue.add("send-reset", payload, { delay: 5000, attempts: 3 });
30
+ ```
31
+
32
+ ### Define a worker
33
+
34
+ ```ts
35
+ // src/workers/email.ts
36
+ import { createWorker } from "@lastshotlabs/bunshot";
37
+ import type { EmailJob } from "../queues/email";
38
+
39
+ export const emailWorker = createWorker<EmailJob>("email", async (job) => {
40
+ const { to, subject, body } = job.data;
41
+ // send email...
42
+ });
43
+ ```
44
+
45
+ Workers in `workersDir` are auto-discovered and registered after the server starts — no manual imports needed. Subdirectories are supported.
46
+
47
+ ### Broadcasting WebSocket messages from a worker
48
+
49
+ Use `publish` to broadcast to all connected clients from inside a worker (or anywhere):
50
+
51
+ ```ts
52
+ // src/workers/notify.ts
53
+ import { createWorker, publish } from "@lastshotlabs/bunshot";
54
+ import type { NotifyJob } from "../queues/notify";
55
+
56
+ export const notifyWorker = createWorker<NotifyJob>("notify", async (job) => {
57
+ const { text, from } = job.data;
58
+ publish("broadcast", { text, from, timestamp: new Date().toISOString() });
59
+ });
60
+ ```
61
+
62
+ `publish` is available after `createServer` resolves. Workers are loaded after that point, so it's always safe to use inside a worker.
63
+
64
+ ### Cron / scheduled workers
65
+
66
+ Use `createCronWorker` for recurring jobs. It creates both a queue and worker, and uses BullMQ's `upsertJobScheduler` for idempotent scheduling across restarts.
67
+
68
+ ```ts
69
+ // src/workers/cleanup.ts
70
+ import { createCronWorker } from "@lastshotlabs/bunshot/queue";
71
+
72
+ export const { worker, queue } = createCronWorker(
73
+ "cleanup",
74
+ async (job) => {
75
+ // runs every hour
76
+ await deleteExpiredRecords();
77
+ },
78
+ { cron: "0 * * * *" } // or { every: 3_600_000 } for interval-based
79
+ );
80
+ ```
81
+
82
+ **Ghost job cleanup**: When a cron worker is renamed or removed, the old scheduler persists in Redis. Bunshot handles this automatically — after all workers in `workersDir` are loaded, stale schedulers are pruned. For workers managed outside `workersDir`, call `cleanupStaleSchedulers(activeNames)` manually.
83
+
84
+ ### Job status endpoint
85
+
86
+ Expose job state via REST for client-side polling (e.g., long-running uploads or exports):
87
+
88
+ ```ts
89
+ import { userAuth, requireRole } from "@lastshotlabs/bunshot";
90
+
91
+ await createServer({
92
+ jobs: {
93
+ statusEndpoint: true, // default: false
94
+ auth: "userAuth", // "userAuth" | "none" | MiddlewareHandler[]
95
+ roles: ["admin"], // require these roles (works with userAuth)
96
+ allowedQueues: ["export", "upload"], // whitelist — empty = nothing exposed (secure by default)
97
+ scopeToUser: false, // when true with userAuth, users only see their own jobs
98
+ },
99
+ });
100
+ ```
101
+
102
+ **Auth options:**
103
+ - `"userAuth"` — requires an authenticated user session. Combine with `roles` for RBAC.
104
+ - `"none"` — no auth protection (not recommended for production).
105
+ - `MiddlewareHandler[]` — pass a custom middleware stack for full control, e.g. `[userAuth, requireRole("admin")]`.
106
+
107
+ #### Endpoints
108
+
109
+ | Endpoint | Purpose |
110
+ |---|---|
111
+ | `GET /jobs` | List available queues |
112
+ | `GET /jobs/:queue` | List jobs in a queue (paginated, filterable by state) |
113
+ | `GET /jobs/:queue/:id` | Job state, progress, result, or failure reason |
114
+ | `GET /jobs/:queue/:id/logs` | Job logs |
115
+ | `GET /jobs/:queue/dead-letters` | Paginated list of DLQ jobs |
116
+
117
+ The list endpoint (`GET /jobs/:queue`) accepts `?state=waiting|active|completed|failed|delayed|paused` and `?start=0&end=19` for pagination.
118
+
119
+ ### Dead Letter Queue (DLQ)
120
+
121
+ Automatically move permanently failed jobs to a DLQ for inspection and retry:
122
+
123
+ ```ts
124
+ import { createWorker, createDLQHandler } from "@lastshotlabs/bunshot/queue";
125
+
126
+ const emailWorker = createWorker("email", async (job) => { ... });
127
+
128
+ const { dlqQueue, retryJob } = createDLQHandler(emailWorker, "email", {
129
+ maxSize: 1000, // default: 1000 — oldest trimmed when exceeded
130
+ onDeadLetter: async (job, error) => { // optional alerting callback
131
+ await alertSlack(`Job ${job.id} failed: ${error.message}`);
132
+ },
133
+ preserveJobOptions: true, // default: true — retry with original delay/priority/attempts
134
+ });
135
+
136
+ // Retry a specific failed job
137
+ await retryJob("job-id-123");
138
+ ```
139
+
140
+ The DLQ queue is named `${sourceQueueName}-dlq` (e.g., `email-dlq`). It's automatically available via the job status endpoint if listed in `allowedQueues`.
@@ -0,0 +1,15 @@
1
+ ## Jobs (BullMQ)
2
+
3
+ Queue-based background jobs powered by BullMQ (requires Redis with `noeviction` policy).
4
+
5
+ ```ts
6
+ // Define a queue
7
+ import { createQueue } from "@lastshotlabs/bunshot";
8
+ export const emailQueue = createQueue<{ to: string; subject: string }>("email");
9
+
10
+ // Define a worker (auto-discovered from workersDir)
11
+ import { createWorker } from "@lastshotlabs/bunshot";
12
+ export const emailWorker = createWorker("email", async (job) => { /* send email */ });
13
+ ```
14
+
15
+ Features include cron/scheduled workers via `createCronWorker`, dead letter queues via `createDLQHandler`, job status REST endpoints, and WebSocket broadcasting from workers via `publish`.
@@ -0,0 +1,45 @@
1
+ ## MongoDB Connections
2
+
3
+ MongoDB and Redis connect automatically inside `createServer` / `createApp`. Control the behavior via the `db` config object:
4
+
5
+ ### Single database (default)
6
+
7
+ Both auth and app data share one server. Uses `MONGO_*` env vars.
8
+
9
+ ```ts
10
+ await createServer({
11
+ // ...
12
+ db: { mongo: "single", redis: true }, // these are the defaults — can omit db entirely
13
+ // app, auth, security are all optional with sensible defaults
14
+ });
15
+ ```
16
+
17
+ ### Separate auth database
18
+
19
+ Auth users live on a dedicated server (`MONGO_AUTH_*` env vars), app data on its own server (`MONGO_*` env vars). Useful when multiple tenant apps share one auth cluster.
20
+
21
+ ```ts
22
+ await createServer({
23
+ // ...
24
+ db: { mongo: "separate" },
25
+ });
26
+ ```
27
+
28
+ ### Manual connections
29
+
30
+ Set `mongo: false` and/or `redis: false` to skip auto-connect and manage connections yourself:
31
+
32
+ ```ts
33
+ import { connectAuthMongo, connectAppMongo, connectRedis, createServer } from "@lastshotlabs/bunshot";
34
+
35
+ await connectAuthMongo();
36
+ await connectAppMongo();
37
+ await connectRedis();
38
+
39
+ await createServer({
40
+ // ...
41
+ db: { mongo: false, redis: false },
42
+ });
43
+ ```
44
+
45
+ `AuthUser` and all built-in auth routes always use `authConnection`. Your app models use `appConnection` (see Adding Models below).
@@ -0,0 +1,7 @@
1
+ ## MongoDB Connections
2
+
3
+ MongoDB and Redis connect automatically inside `createServer` / `createApp`. Control via the `db` config:
4
+
5
+ - **`mongo: "single"`** (default) — auth and app data share one server (`MONGO_*` env vars)
6
+ - **`mongo: "separate"`** — auth on its own server (`MONGO_AUTH_*` env vars), app data on another
7
+ - **`mongo: false`** — skip auto-connect, manage connections yourself via `connectAuthMongo()`, `connectAppMongo()`, `connectRedis()`
@@ -0,0 +1,66 @@
1
+ ## Multi-Tenancy
2
+
3
+ Add multi-tenancy to your app by configuring tenant resolution. Bunshot resolves the tenant on each request and attaches `tenantId` + `tenantConfig` to the Hono context.
4
+
5
+ ```ts
6
+ await createServer({
7
+ tenancy: {
8
+ resolution: "header", // "header" | "subdomain" | "path"
9
+ headerName: "x-tenant-id", // default for "header" strategy
10
+ onResolve: async (tenantId) => { // validate + load tenant config — return null to reject
11
+ const tenant = await getTenant(tenantId);
12
+ return tenant?.config ?? null;
13
+ },
14
+ cacheTtlMs: 60_000, // LRU cache TTL for onResolve (default: 60s, 0 to disable)
15
+ cacheMaxSize: 500, // max cached entries (default: 500)
16
+ exemptPaths: ["/webhooks"], // additional paths that skip tenant resolution
17
+ rejectionStatus: 403, // 403 (default) or 404 when onResolve returns null
18
+ },
19
+ });
20
+ ```
21
+
22
+ ### Resolution strategies
23
+
24
+ | Strategy | How it extracts tenant ID | Example |
25
+ |---|---|---|
26
+ | `"header"` | From request header (default `x-tenant-id`) | `x-tenant-id: acme` |
27
+ | `"subdomain"` | From first subdomain | `acme.myapp.com` → `"acme"` |
28
+ | `"path"` | From URL path segment (does **not** strip prefix) | `/acme/api/users` → `"acme"` |
29
+
30
+ ### Default exempt paths
31
+
32
+ These paths skip tenant resolution by default: `/health`, `/docs`, `/openapi.json`, `/auth/` (auth is global — all tenants share a user pool). Add more via `exemptPaths`.
33
+
34
+ ### `onResolve` is required in production
35
+
36
+ When `tenancy` is configured without an `onResolve` callback, tenant IDs from headers/subdomains/paths are trusted without validation — a cross-tenant access risk. **In production (`NODE_ENV=production`), the server will refuse to start** if `onResolve` is missing. In development, a warning is logged instead.
37
+
38
+ ### Accessing tenant in routes
39
+
40
+ ```ts
41
+ router.openapi(myRoute, async (c) => {
42
+ const tenantId = c.get("tenantId"); // string | null
43
+ const tenantConfig = c.get("tenantConfig"); // Record<string, unknown> | null
44
+ // Filter queries by tenantId, apply tenant-specific settings, etc.
45
+ });
46
+ ```
47
+
48
+ ### Tenant provisioning helpers
49
+
50
+ CRUD utilities for managing tenants (stored in the auth database via MongoDB):
51
+
52
+ ```ts
53
+ import { createTenant, getTenant, listTenants, deleteTenant } from "@lastshotlabs/bunshot";
54
+
55
+ await createTenant("acme", { displayName: "Acme Corp", config: { maxUsers: 100 } });
56
+ const tenant = await getTenant("acme"); // { tenantId, displayName, config, createdAt }
57
+ const all = await listTenants(); // active tenants only
58
+ await deleteTenant("acme"); // soft-delete + invalidates resolution cache
59
+ ```
60
+
61
+ ### Per-tenant namespacing
62
+
63
+ When tenant context is present, rate limits and cache keys are automatically namespaced per-tenant — no code changes needed. Each tenant gets independent rate limit buckets and cache entries.
64
+
65
+ - Rate limit keys: `t:${tenantId}:ip:${ip}` (instead of `ip:${ip}`)
66
+ - Cache keys: `cache:${appName}:${tenantId}:${key}` (instead of `cache:${appName}:${key}`)
@@ -0,0 +1,15 @@
1
+ ## Multi-Tenancy
2
+
3
+ Opt-in via `tenancy` config. Resolves tenant ID from header, subdomain, or path segment on each request.
4
+
5
+ ```ts
6
+ await createServer({
7
+ tenancy: {
8
+ resolution: "header",
9
+ headerName: "x-tenant-id",
10
+ onResolve: async (tenantId) => { /* validate, return config or null */ },
11
+ },
12
+ });
13
+ ```
14
+
15
+ Auth routes are exempt (global user pool). Rate limits and cache keys are auto-namespaced per-tenant. CRUD helpers: `createTenant`, `getTenant`, `listTenants`, `deleteTenant`.
@@ -0,0 +1,189 @@
1
+ ## Social Login (OAuth)
2
+
3
+ Pass `auth.oauth.providers` to `createServer` to enable Google, Apple, Microsoft, and/or GitHub sign-in. Routes are mounted automatically for each configured provider.
4
+
5
+ ```ts
6
+ await createServer({
7
+ routesDir: import.meta.dir + "/routes",
8
+ app: { name: "My App", version: "1.0.0" },
9
+ auth: {
10
+ oauth: {
11
+ postRedirect: "/lobby", // where to redirect after login (default: "/")
12
+ providers: {
13
+ google: {
14
+ clientId: process.env.GOOGLE_CLIENT_ID!,
15
+ clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
16
+ redirectUri: "https://myapp.com/auth/google/callback",
17
+ },
18
+ apple: {
19
+ clientId: process.env.APPLE_CLIENT_ID!, // Services ID, e.g. "com.myapp.auth"
20
+ teamId: process.env.APPLE_TEAM_ID!,
21
+ keyId: process.env.APPLE_KEY_ID!,
22
+ privateKey: process.env.APPLE_PRIVATE_KEY!, // PEM string
23
+ redirectUri: "https://myapp.com/auth/apple/callback",
24
+ },
25
+ microsoft: {
26
+ tenantId: process.env.MICROSOFT_TENANT_ID!, // "common", "organizations", "consumers", or tenant GUID
27
+ clientId: process.env.MICROSOFT_CLIENT_ID!,
28
+ clientSecret: process.env.MICROSOFT_CLIENT_SECRET!,
29
+ redirectUri: "https://myapp.com/auth/microsoft/callback",
30
+ },
31
+ github: {
32
+ clientId: process.env.GITHUB_CLIENT_ID!,
33
+ clientSecret: process.env.GITHUB_CLIENT_SECRET!,
34
+ redirectUri: "https://myapp.com/auth/github/callback",
35
+ },
36
+ },
37
+ },
38
+ },
39
+ });
40
+ ```
41
+
42
+ ### Routes mounted automatically
43
+
44
+ | Provider | Initiate login | Callback | Link to existing account | Unlink |
45
+ |---|---|---|---|---|
46
+ | Google | `GET /auth/google` | `GET /auth/google/callback` | `GET /auth/google/link` | `DELETE /auth/google/link` |
47
+ | Apple | `GET /auth/apple` | `POST /auth/apple/callback` | `GET /auth/apple/link` | — |
48
+ | Microsoft | `GET /auth/microsoft` | `GET /auth/microsoft/callback` | `GET /auth/microsoft/link` | `DELETE /auth/microsoft/link` |
49
+ | GitHub | `GET /auth/github` | `GET /auth/github/callback` | `GET /auth/github/link` | `DELETE /auth/github/link` |
50
+
51
+ > Apple sends its callback as a **POST** with form data. Your server must be publicly reachable and the redirect URI must be registered in the Apple developer console.
52
+
53
+ > **Microsoft `tenantId` options:** `"common"` accepts any Microsoft account (personal + work/school), `"organizations"` accepts work/school accounts only, `"consumers"` accepts personal accounts only, or pass a specific tenant GUID to restrict to a single Azure AD tenant (recommended for company SSO).
54
+
55
+ > **GitHub:** Create an OAuth App (not a GitHub App) at [github.com/settings/developers](https://github.com/settings/developers). The `user:email` scope is requested to retrieve the user's verified email address, since the primary `/user` endpoint may not return it for users with private email settings.
56
+
57
+ Additionally, a shared code exchange endpoint is always mounted:
58
+
59
+ | Endpoint | Purpose |
60
+ |---|---|
61
+ | `POST /auth/oauth/exchange` | Exchange one-time authorization code for session token |
62
+
63
+ ### Flow
64
+
65
+ 1. Client navigates to `GET /auth/google` (or `/auth/apple`, `/auth/microsoft`, `/auth/github`)
66
+ 2. Package redirects to the provider's OAuth page
67
+ 3. Provider redirects (or POSTs) back to the callback URL
68
+ 4. Package exchanges the code, fetches the user profile, and calls `authAdapter.findOrCreateByProvider`
69
+ 5. A session is created and a **one-time authorization code** is generated
70
+ 6. User is redirected to `auth.oauth.postRedirect?code=<one-time-code>`
71
+ 7. Client exchanges the code for a session token via `POST /auth/oauth/exchange`
72
+
73
+ > **Security:** The JWT is never exposed in the redirect URL. The one-time code expires after 60 seconds and can only be used once, preventing token leakage via browser history, server logs, or referrer headers.
74
+
75
+ #### Code exchange
76
+
77
+ After the OAuth redirect, the client must exchange the one-time code for a session token:
78
+
79
+ ```ts
80
+ // Client-side
81
+ const res = await fetch("/auth/oauth/exchange", {
82
+ method: "POST",
83
+ headers: { "Content-Type": "application/json" },
84
+ body: JSON.stringify({ code: new URLSearchParams(location.search).get("code") }),
85
+ });
86
+ const { token, userId, email, refreshToken } = await res.json();
87
+ ```
88
+
89
+ The exchange endpoint sets session cookies automatically for browser clients. Mobile/SPA clients can use the JSON response directly. Rate limited to 20 requests per minute per IP.
90
+
91
+ | Field | Description |
92
+ |---|---|
93
+ | `token` | Session JWT |
94
+ | `userId` | Authenticated user ID |
95
+ | `email` | User email (if available) |
96
+ | `refreshToken` | Refresh token (only when `auth.refreshTokens` is configured) |
97
+
98
+ ### Redirect URL validation
99
+
100
+ Pass `auth.oauth.allowedRedirectUrls` to restrict where OAuth callbacks can redirect:
101
+
102
+ ```ts
103
+ auth: {
104
+ oauth: {
105
+ postRedirect: "/dashboard",
106
+ allowedRedirectUrls: ["https://myapp.com", "https://staging.myapp.com"],
107
+ providers: { ... },
108
+ },
109
+ }
110
+ ```
111
+
112
+ When configured, the `postRedirect` value is validated against the allowlist at startup. If omitted, any redirect URL is accepted (not recommended for production).
113
+
114
+ ### User storage
115
+
116
+ The default `mongoAuthAdapter` stores social users in `AuthUser` with a `providerIds` field (e.g. `["google:1234567890"]`). If no existing provider key is found, a new account is created — emails are never auto-linked. To connect a social identity to an existing credential account the user must explicitly use the link flow below.
117
+
118
+ **Email conflict handling:** If a user attempts to sign in via Google (or Apple/Microsoft/GitHub) and the email returned by the provider already belongs to a credential-based account, `findOrCreateByProvider` throws `HttpError(409, ...)`. The OAuth callback catches this and redirects to `auth.oauth.postRedirect?error=<message>` so the client can display a helpful prompt (e.g. "An account with this email already exists — sign in with your password, then link Google from your account settings.").
119
+
120
+ To support social login with a custom adapter, implement `findOrCreateByProvider`:
121
+
122
+ ```ts
123
+ const myAdapter: AuthAdapter = {
124
+ findByEmail: ...,
125
+ create: ...,
126
+ async findOrCreateByProvider(provider, providerId, profile) {
127
+ // find or upsert user by provider + providerId
128
+ // return { id: string }
129
+ },
130
+ };
131
+ ```
132
+
133
+ ### Linking a provider to an existing account
134
+
135
+ A logged-in user can link their account to a Google, Apple, Microsoft, or GitHub identity by navigating to the link route. This is the only way to associate a social login with an existing credential account — email matching is intentionally not done automatically.
136
+
137
+ ```
138
+ GET /auth/google/link (requires active session via cookie)
139
+ GET /auth/apple/link (requires active session via cookie)
140
+ GET /auth/microsoft/link (requires active session via cookie)
141
+ GET /auth/github/link (requires active session via cookie)
142
+ ```
143
+
144
+ The link flow:
145
+ 1. User is already logged in (session cookie set)
146
+ 2. Client navigates to `/auth/google/link`
147
+ 3. User completes Google OAuth as normal
148
+ 4. On callback, instead of creating a new session, the Google identity is added to their existing account
149
+ 5. User is redirected to `auth.oauth.postRedirect?linked=google`
150
+
151
+ To support linking with a custom adapter, implement `linkProvider`:
152
+
153
+ ```ts
154
+ const myAdapter: AuthAdapter = {
155
+ // ...
156
+ async linkProvider(userId, provider, providerId) {
157
+ const key = `${provider}:${providerId}`;
158
+ await db.update(users)
159
+ .set({ providerIds: sql`array_append(provider_ids, ${key})` })
160
+ .where(eq(users.id, userId));
161
+ },
162
+ };
163
+ ```
164
+
165
+ ### Unlinking a provider
166
+
167
+ A logged-in user can remove a linked Google, Microsoft, or GitHub identity via:
168
+
169
+ ```
170
+ DELETE /auth/google/link (requires active session via cookie)
171
+ DELETE /auth/microsoft/link (requires active session via cookie)
172
+ DELETE /auth/github/link (requires active session via cookie)
173
+ ```
174
+
175
+ Returns `204 No Content` on success. All `google:*` entries are removed from the user's `providerIds`.
176
+
177
+ To support unlinking with a custom adapter, implement `unlinkProvider`:
178
+
179
+ ```ts
180
+ const myAdapter: AuthAdapter = {
181
+ // ...
182
+ async unlinkProvider(userId, provider) {
183
+ const user = await db.query.users.findFirst({ where: eq(users.id, userId) });
184
+ if (!user) throw new HttpError(404, "User not found");
185
+ const filtered = user.providerIds.filter((id: string) => !id.startsWith(`${provider}:`));
186
+ await db.update(users).set({ providerIds: filtered }).where(eq(users.id, userId));
187
+ },
188
+ };
189
+ ```
@@ -0,0 +1,16 @@
1
+ ## Social Login (OAuth)
2
+
3
+ Pass `auth.oauth.providers` to enable Google, Apple, Microsoft, and/or GitHub sign-in. Routes are mounted automatically for each configured provider.
4
+
5
+ ```ts
6
+ auth: {
7
+ oauth: {
8
+ postRedirect: "/dashboard",
9
+ providers: {
10
+ google: { clientId: "...", clientSecret: "...", redirectUri: "..." },
11
+ },
12
+ },
13
+ }
14
+ ```
15
+
16
+ Auto-mounted routes per provider: initiate (`GET /auth/{provider}`), callback, link to existing account (`GET /auth/{provider}/link`), and unlink (`DELETE /auth/{provider}/link`). After OAuth redirect, the client exchanges a one-time authorization code via `POST /auth/oauth/exchange` to receive the session token (the JWT is never exposed in the redirect URL). Supports custom adapters via `findOrCreateByProvider`, `linkProvider`, and `unlinkProvider`. Optionally restrict redirect URLs with `allowedRedirectUrls`.
@@ -0,0 +1,7 @@
1
+ ## Package Development
2
+
3
+ To test changes locally, install the package from the local path in a sibling project:
4
+
5
+ ```bash
6
+ bun add @lastshotlabs/bunshot@file:../bunshot
7
+ ```
@@ -0,0 +1,47 @@
1
+ ## Peer Dependencies
2
+
3
+ Bunshot declares the following as peer dependencies so you control their versions and avoid duplicate installs in your app.
4
+
5
+ ### Required
6
+
7
+ These must be installed in every consuming app:
8
+
9
+ ```bash
10
+ bun add hono zod
11
+ ```
12
+
13
+ | Package | Required version |
14
+ |---|---|
15
+ | `hono` | `>=4.12 <5` |
16
+ | `zod` | `>=4.0 <5` |
17
+
18
+ ### Optional
19
+
20
+ Install only what your app actually uses:
21
+
22
+ ```bash
23
+ # MongoDB auth / sessions / cache
24
+ bun add mongoose
25
+
26
+ # Redis sessions, cache, rate limiting, or BullMQ
27
+ bun add ioredis
28
+
29
+ # Background job queues
30
+ bun add bullmq
31
+
32
+ # MFA / TOTP
33
+ bun add otpauth
34
+
35
+ # MFA / WebAuthn (security keys, Touch ID, Windows Hello)
36
+ bun add @simplewebauthn/server
37
+ ```
38
+
39
+ | Package | Required version | When you need it |
40
+ |---|---|---|
41
+ | `mongoose` | `>=9.0 <10` | `db.auth: "mongo"`, `db.sessions: "mongo"`, or `db.cache: "mongo"` |
42
+ | `ioredis` | `>=5.0 <6` | `db.redis: true` (the default), or any store set to `"redis"` |
43
+ | `bullmq` | `>=5.0 <6` | Workers / queues |
44
+ | `otpauth` | `>=9.0 <10` | `auth.mfa` configuration (TOTP) |
45
+ | `@simplewebauthn/server` | `>=10.0.0` | `auth.mfa.webauthn` configuration |
46
+
47
+ If you're running fully on SQLite or memory (no Redis, no MongoDB), none of the optional peers are needed.
@@ -0,0 +1,43 @@
1
+ ## Quick Start
2
+
3
+ ```bash
4
+ bun add @lastshotlabs/bunshot hono zod
5
+ ```
6
+
7
+ ```ts
8
+ // src/index.ts
9
+ import { createServer } from "@lastshotlabs/bunshot";
10
+
11
+ await createServer({
12
+ routesDir: import.meta.dir + "/routes",
13
+ db: { auth: "memory", mongo: false, redis: false, sessions: "memory", cache: "memory" },
14
+ });
15
+ ```
16
+
17
+ ```ts
18
+ // src/routes/hello.ts
19
+ import { z } from "zod";
20
+ import { createRoute, createRouter } from "@lastshotlabs/bunshot";
21
+
22
+ export const router = createRouter();
23
+
24
+ router.openapi(
25
+ createRoute({
26
+ method: "get",
27
+ path: "/hello",
28
+ responses: {
29
+ 200: {
30
+ content: { "application/json": { schema: z.object({ message: z.string() }) } },
31
+ description: "Hello",
32
+ },
33
+ },
34
+ }),
35
+ (c) => c.json({ message: "Hello world!" }, 200)
36
+ );
37
+ ```
38
+
39
+ ```bash
40
+ bun run src/index.ts
41
+ ```
42
+
43
+ Auth, OpenAPI docs (`/docs`), health check, and WebSocket are all live. No databases required — swap `"memory"` for `"redis"` / `"mongo"` / `"sqlite"` when you're ready.