@insforge/nextjs 0.11.0 → 0.11.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/dist/esm/server/auth.js
CHANGED
|
@@ -9,8 +9,11 @@ async function auth() {
|
|
|
9
9
|
let userId = null;
|
|
10
10
|
if (userCookie) {
|
|
11
11
|
try {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
const parsed = JSON.parse(userCookie);
|
|
13
|
+
if (parsed && typeof parsed === "object" && "id" in parsed && "email" in parsed && "name" in parsed && typeof parsed.id === "string" && typeof parsed.email === "string" && typeof parsed.name === "string") {
|
|
14
|
+
user = parsed;
|
|
15
|
+
userId = user.id;
|
|
16
|
+
}
|
|
14
17
|
} catch (error) {
|
|
15
18
|
console.error("[InsForge Auth] Failed to parse user cookie:", error);
|
|
16
19
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/server/auth.ts"],"sourcesContent":["import { cookies } from 'next/headers';\
|
|
1
|
+
{"version":3,"sources":["../../../src/server/auth.ts"],"sourcesContent":["import { cookies } from 'next/headers';\n\n/**\n * Session information extracted from cookies\n */\nexport interface InsforgeAuth {\n /**\n * The current user's ID, or null if not authenticated\n */\n userId: string | null;\n\n /**\n * The current session token, or null if not authenticated\n */\n token: string | null;\n\n /**\n * User information (email, name) if available\n */\n user: {\n id: string;\n email: string;\n name: string;\n } | null;\n}\n\n/**\n * Get authentication information from cookies in Server Components,\n * Route Handlers, and Server Actions.\n *\n * This function reads the HTTP-only cookies set by the middleware\n * and returns user authentication data.\n *\n * @example\n * ```ts\n * // In a Server Component\n * import { auth } from '@insforge/nextjs/server';\n *\n * export default async function Page() {\n * const { userId, token } = await auth();\n *\n * if (!userId) {\n * return <div>Not authenticated</div>;\n * }\n *\n * return <div>User ID: {userId}</div>;\n * }\n * ```\n *\n * @example\n * ```ts\n * // In an API Route Handler\n * import { auth } from '@insforge/nextjs/server';\n * import { createClient } from '@insforge/sdk';\n *\n * export async function GET() {\n * const { userId, token } = await auth();\n *\n * if (!userId || !token) {\n * return Response.json({ error: 'Unauthorized' }, { status: 401 });\n * }\n *\n * // Use token with SDK\n * const insforge = createClient({\n * baseUrl: process.env.INSFORGE_BASE_URL!,\n * edgeFunctionToken: token,\n * });\n *\n * const result = await insforge.database.from('posts').select();\n * return Response.json(result.data);\n * }\n * ```\n *\n * @example\n * ```ts\n * // In a Server Action\n * 'use server';\n *\n * import { auth } from '@insforge/nextjs/server';\n *\n * export async function createPost(formData: FormData) {\n * const { userId } = await auth();\n *\n * if (!userId) {\n * throw new Error('Not authenticated');\n * }\n *\n * // Create post with userId\n * }\n * ```\n */\nexport async function auth(): Promise<InsforgeAuth> {\n const cookieStore = await cookies();\n\n // Fixed cookie names (same as middleware and route handlers)\n const sessionCookieName = 'insforge-session';\n const userCookieName = 'insforge-user';\n\n // Get session token\n const token = cookieStore.get(sessionCookieName)?.value || null;\n\n // Get user info\n const userCookie = cookieStore.get(userCookieName)?.value;\n let user: { id: string; email: string; name: string } | null = null;\n let userId: string | null = null;\n\n if (userCookie) {\n try {\n const parsed = JSON.parse(userCookie) as unknown;\n // Type guard to validate the parsed data\n if (\n parsed &&\n typeof parsed === 'object' &&\n 'id' in parsed &&\n 'email' in parsed &&\n 'name' in parsed &&\n typeof parsed.id === 'string' &&\n typeof parsed.email === 'string' &&\n typeof parsed.name === 'string'\n ) {\n user = parsed as { id: string; email: string; name: string };\n userId = user.id;\n }\n } catch (error) {\n console.error('[InsForge Auth] Failed to parse user cookie:', error);\n }\n }\n\n return {\n userId,\n token,\n user,\n };\n}\n"],"mappings":"AAAA,SAAS,eAAe;AA2FxB,eAAsB,OAA8B;AAClD,QAAM,cAAc,MAAM,QAAQ;AAGlC,QAAM,oBAAoB;AAC1B,QAAM,iBAAiB;AAGvB,QAAM,QAAQ,YAAY,IAAI,iBAAiB,GAAG,SAAS;AAG3D,QAAM,aAAa,YAAY,IAAI,cAAc,GAAG;AACpD,MAAI,OAA2D;AAC/D,MAAI,SAAwB;AAE5B,MAAI,YAAY;AACd,QAAI;AACF,YAAM,SAAS,KAAK,MAAM,UAAU;AAEpC,UACE,UACA,OAAO,WAAW,YAClB,QAAQ,UACR,WAAW,UACX,UAAU,UACV,OAAO,OAAO,OAAO,YACrB,OAAO,OAAO,UAAU,YACxB,OAAO,OAAO,SAAS,UACvB;AACA,eAAO;AACP,iBAAS,KAAK;AAAA,MAChB;AAAA,IACF,SAAS,OAAO;AACd,cAAQ,MAAM,gDAAgD,KAAK;AAAA,IACrE;AAAA,EACF;AAEA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;","names":[]}
|
package/package.json
CHANGED