Skip to content

Commit 2efcc5a

Browse files
committed
docs(changeset): chore: added better-auth on error handling for production (console.error(error, AuthContext)
chore: added upstash vs valkey getter handling (upstash does not resturn string) chore: added wrangler dev, bun dev, and production runtime handling for server chore: context must be re-created (not a singleton) for edge runtime)
1 parent c81fe17 commit 2efcc5a

File tree

6 files changed

+33
-15
lines changed

6 files changed

+33
-15
lines changed

.changeset/fancy-moments-slide.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
"@nimbus/auth": patch
3+
"@nimbus/server": patch
4+
---
5+
6+
chore: added better-auth on error handling for production (console.error(error, AuthContext) chore: added upstash vs
7+
valkey getter handling (upstash does not resturn string) chore: added wrangler dev, bun dev, and production runtime
8+
handling for server chore: context must be re-created (not a singleton) for edge runtime)

apps/server/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"check": "tsc --noEmit",
1313
"build": "bun run check && bun build src/index.ts --target bun --minify --outdir dist",
1414
"start": "bun dist/index.js",
15-
"cf:dev": "wrangler dev",
15+
"cf:dev": "sudo wrangler dev",
1616
"cf:deploy:preview": "wrangler deploy --env preview",
1717
"cf:deploy:staging": "wrangler deploy --env staging",
1818
"cf:deploy:production": "wrangler deploy --env production",

apps/server/src/context.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export class ContextManager {
2323
}
2424

2525
public static getInstance(): ContextManager {
26-
if (!ContextManager.instance) {
26+
if (!ContextManager.instance || ContextManager.instance.env.IS_EDGE_RUNTIME) {
2727
ContextManager.instance = new ContextManager();
2828
}
2929
return ContextManager.instance;

apps/server/src/index.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ const app = createPublicRouter()
2929
try {
3030
await next();
3131
} finally {
32-
if (env.IS_EDGE_RUNTIME) {
32+
// WARNING: make sure to add WRANGLER_DEV to .dev.vars for wrangler dev
33+
// for local dev, always keep context open UNLESS wrangler dev, close context
34+
if (env.IS_EDGE_RUNTIME && (env.NODE_ENV === "production" || process.env.WRANGLER_DEV === "true")) {
3335
await ContextManager.getInstance().close();
3436
}
3537
}
@@ -41,17 +43,10 @@ export type AppType = typeof app;
4143

4244
// Create a wrapper that handles the environment initialization
4345
const handler = {
46+
port: process.env.SERVER_PORT,
4447
async fetch(request: Request, env: any, ctx: any) {
4548
return app.fetch(request, env, ctx);
4649
},
4750
};
4851

49-
// For non-edge environments, we'll use the original app with the port
50-
if (process.env.IS_EDGE_RUNTIME !== "true") {
51-
Object.assign(handler, {
52-
port: process.env.SERVER_PORT,
53-
...app,
54-
});
55-
}
56-
5752
export default handler;

apps/server/wrangler.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ compatibility_flags = ["nodejs_compat"]
55
observability = { enabled = true }
66

77
[dev]
8-
port = 1284
8+
# wrangler dev must be run on port 80 and with sudo because wrangler sets
9+
# Access-Control-Allow-Origin to http://localhost:1284:1284:3000
10+
port = 80
911
host = "localhost"
1012
local_protocol = "http"
1113

packages/auth/src/auth.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { type Account, type AuthContext, betterAuth } from "better-auth";
12
import type { Redis as UpstashRedis } from "@upstash/redis/cloudflare";
23
import { drizzleAdapter } from "better-auth/adapters/drizzle";
34
import schema, { user as userTable } from "@nimbus/db/schema";
4-
import { type Account, betterAuth } from "better-auth";
55
import type { Redis as ValkeyRedis } from "iovalkey";
66
import type { RedisClient } from "@nimbus/cache";
77
import { providerSchema } from "@nimbus/shared";
@@ -24,7 +24,13 @@ export const createAuth = (env: Env, db: DB, redisClient: RedisClient, resend: R
2424
appName: "Nimbus",
2525
baseURL: env.BACKEND_URL,
2626
trustedOrigins: [...env.TRUSTED_ORIGINS, env.BACKEND_URL],
27-
// onApiError: { errorUrl: env.FRONTEND_URL },
27+
onApiError: {
28+
// errorUrl: env.FRONTEND_URL,
29+
// throwError: true,
30+
onError: (error: unknown, ctx: AuthContext) => {
31+
console.error("API error", error, ctx);
32+
},
33+
},
2834

2935
// Ensure state is properly handled
3036
state: {
@@ -100,7 +106,14 @@ export const createAuth = (env: Env, db: DB, redisClient: RedisClient, resend: R
100106

101107
secondaryStorage: {
102108
get: async (key: string) => {
103-
return await redisClient.get(key);
109+
if (env.IS_EDGE_RUNTIME) {
110+
const value = await (redisClient as UpstashRedis).get(key);
111+
const string = JSON.stringify(value);
112+
return string as string | null;
113+
} else {
114+
const value = await (redisClient as ValkeyRedis).get(key);
115+
return value as string | null;
116+
}
104117
},
105118
set: async (key: string, value: string, ttl?: number) => {
106119
if (ttl) {

0 commit comments

Comments
 (0)