@insforge/sdk 1.2.10 → 1.3.0-ssr.1

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.
package/README.md CHANGED
@@ -258,28 +258,74 @@ The SDK supports the following configuration options:
258
258
  const insforge = createClient({
259
259
  baseUrl: "http://localhost:7130", // Your InsForge backend URL
260
260
  anonKey: "your-anon-key", // Optional
261
- isServerMode: false, // Optional (set true in SSR/server runtime)
262
261
  });
263
262
  ```
264
263
 
265
264
  ### SSR / Next.js
266
265
 
267
- For SSR apps, configure `isServerMode: true`.
268
- In this mode, auth requests use `client_type=mobile` so auth methods return `refreshToken` in the response body.
269
- The SDK does not auto-refresh in server mode; your Next.js app should manage refresh token flow.
270
- In server mode, the SDK does not persist session/user state.
271
- Read your access token from cookies in Next.js and pass it as `edgeFunctionToken` per request.
272
- Your app should write/update cookies itself after login/refresh.
266
+ Use `@insforge/sdk/ssr` for apps that need the same auth session in Server Components, Client Components, Storage, and Realtime.
267
+ The helper uses explicit `baseUrl` / `anonKey` when provided. Otherwise it reads `NEXT_PUBLIC_INSFORGE_URL` / `NEXT_PUBLIC_INSFORGE_ANON_KEY`. Missing config throws a clear error.
268
+
269
+ By default, the SSR helpers use:
270
+
271
+ - `insforge_access_token`: browser-readable access token cookie, expires at the JWT `exp`
272
+ - `insforge_refresh_token`: httpOnly refresh token cookie, expires at the JWT `exp`
273
273
 
274
274
  ```typescript
275
- import { createClient } from "@insforge/sdk";
276
- const accessToken = /* read access token from request cookies */ null;
275
+ // app/lib/insforge/client.ts
276
+ import { createBrowserClient } from "@insforge/sdk/ssr";
277
277
 
278
- const insforge = createClient({
279
- baseUrl: process.env.INSFORGE_URL!,
280
- isServerMode: true,
281
- edgeFunctionToken: accessToken ?? undefined,
282
- });
278
+ export const insforge = createBrowserClient();
279
+ ```
280
+
281
+ ```typescript
282
+ // app/lib/insforge/server.ts
283
+ import { cookies } from "next/headers";
284
+ import { createServerClient } from "@insforge/sdk/ssr";
285
+
286
+ export async function createInsForgeServerClient() {
287
+ return createServerClient({ cookies: await cookies() });
288
+ }
289
+ ```
290
+
291
+ ```typescript
292
+ // app/api/auth/refresh/route.ts
293
+ import { createRefreshAuthRouter } from "@insforge/sdk/ssr";
294
+
295
+ export const { POST } = createRefreshAuthRouter();
296
+ ```
297
+
298
+ For server-owned refresh cookies, run sign-in in a Route Handler or Server Action and use `setAuthCookies()` from `@insforge/sdk/ssr` with the auth response.
299
+
300
+ If your refresh route needs custom side effects:
301
+
302
+ ```typescript
303
+ import { refreshAuth } from "@insforge/sdk/ssr";
304
+
305
+ export async function POST(request: Request) {
306
+ const result = await refreshAuth({ request });
307
+ // run app-specific side effects here
308
+ return result.response;
309
+ }
310
+ ```
311
+
312
+ For Next.js Proxy/Middleware, refresh before Server Components render:
313
+
314
+ ```typescript
315
+ // proxy.ts on Next.js 16+, middleware.ts on Next.js 15 and earlier
316
+ import { NextResponse, type NextRequest } from "next/server";
317
+ import { updateSession } from "@insforge/sdk/ssr";
318
+
319
+ export async function proxy(request: NextRequest) {
320
+ const response = NextResponse.next({ request });
321
+
322
+ await updateSession({
323
+ requestCookies: request.cookies,
324
+ responseCookies: response.cookies,
325
+ });
326
+
327
+ return response;
328
+ }
283
329
  ```
284
330
 
285
331
  ## TypeScript Support