Fix SuperTokens build error with lazy initialization
- Move SuperTokens.init() to runtime (not build time) - Add dynamic route config to prevent build-time evaluation - Move appInfo inside backendConfig function - Update default URLs to vibnai.com Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -3,36 +3,58 @@ import SuperTokens from "supertokens-node";
|
|||||||
import { backendConfig } from "@/lib/supertokens/backendConfig";
|
import { backendConfig } from "@/lib/supertokens/backendConfig";
|
||||||
import { getAppDirRequestHandler } from "supertokens-node/nextjs";
|
import { getAppDirRequestHandler } from "supertokens-node/nextjs";
|
||||||
|
|
||||||
SuperTokens.init(backendConfig());
|
// Tell Next.js this is a dynamic route (don't evaluate at build time)
|
||||||
|
export const dynamic = 'force-dynamic';
|
||||||
|
export const runtime = 'nodejs';
|
||||||
|
|
||||||
const handleRequest = getAppDirRequestHandler(NextResponse);
|
// Initialize SuperTokens lazily (only when first request comes in)
|
||||||
|
let initialized = false;
|
||||||
|
|
||||||
|
function ensureInitialized() {
|
||||||
|
if (!initialized && typeof window === 'undefined') {
|
||||||
|
SuperTokens.init(backendConfig());
|
||||||
|
initialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export async function GET(request: NextRequest) {
|
export async function GET(request: NextRequest) {
|
||||||
|
ensureInitialized();
|
||||||
|
const handleRequest = getAppDirRequestHandler(NextResponse);
|
||||||
const response = await handleRequest(request);
|
const response = await handleRequest(request);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function POST(request: NextRequest) {
|
export async function POST(request: NextRequest) {
|
||||||
|
ensureInitialized();
|
||||||
|
const handleRequest = getAppDirRequestHandler(NextResponse);
|
||||||
const response = await handleRequest(request);
|
const response = await handleRequest(request);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function DELETE(request: NextRequest) {
|
export async function DELETE(request: NextRequest) {
|
||||||
|
ensureInitialized();
|
||||||
|
const handleRequest = getAppDirRequestHandler(NextResponse);
|
||||||
const response = await handleRequest(request);
|
const response = await handleRequest(request);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function PUT(request: NextRequest) {
|
export async function PUT(request: NextRequest) {
|
||||||
|
ensureInitialized();
|
||||||
|
const handleRequest = getAppDirRequestHandler(NextResponse);
|
||||||
const response = await handleRequest(request);
|
const response = await handleRequest(request);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function PATCH(request: NextRequest) {
|
export async function PATCH(request: NextRequest) {
|
||||||
|
ensureInitialized();
|
||||||
|
const handleRequest = getAppDirRequestHandler(NextResponse);
|
||||||
const response = await handleRequest(request);
|
const response = await handleRequest(request);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function HEAD(request: NextRequest) {
|
export async function HEAD(request: NextRequest) {
|
||||||
|
ensureInitialized();
|
||||||
|
const handleRequest = getAppDirRequestHandler(NextResponse);
|
||||||
const response = await handleRequest(request);
|
const response = await handleRequest(request);
|
||||||
return response;
|
return response;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,20 +4,22 @@ import SessionNode from "supertokens-node/recipe/session";
|
|||||||
import { TypeInput } from "supertokens-node/types";
|
import { TypeInput } from "supertokens-node/types";
|
||||||
import { AppInfoUserInput } from "supertokens-node/types";
|
import { AppInfoUserInput } from "supertokens-node/types";
|
||||||
|
|
||||||
const appInfo: AppInfoUserInput = {
|
export const backendConfig = (): TypeInput => {
|
||||||
|
const appUrl = process.env.NEXT_PUBLIC_APP_URL || "https://vibnai.com";
|
||||||
|
|
||||||
|
const appInfo: AppInfoUserInput = {
|
||||||
appName: "Vib'n",
|
appName: "Vib'n",
|
||||||
apiDomain: process.env.NEXT_PUBLIC_APP_URL || "https://app.vibnai.com",
|
apiDomain: appUrl,
|
||||||
websiteDomain: process.env.NEXT_PUBLIC_APP_URL || "https://app.vibnai.com",
|
websiteDomain: appUrl,
|
||||||
apiBasePath: "/api/auth",
|
apiBasePath: "/api/auth",
|
||||||
websiteBasePath: "/auth",
|
websiteBasePath: "/auth",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const backendConfig = (): TypeInput => {
|
|
||||||
return {
|
return {
|
||||||
framework: "custom",
|
framework: "custom",
|
||||||
supertokens: {
|
supertokens: {
|
||||||
connectionURI: process.env.SUPERTOKENS_CONNECTION_URI || "https://auth.vibnai.com",
|
connectionURI: process.env.SUPERTOKENS_CONNECTION_URI || "http://j04ckwg0k040o08gc04gs80o:3567",
|
||||||
apiKey: process.env.SUPERTOKENS_API_KEY,
|
apiKey: process.env.SUPERTOKENS_API_KEY || "",
|
||||||
},
|
},
|
||||||
appInfo,
|
appInfo,
|
||||||
recipeList: [
|
recipeList: [
|
||||||
|
|||||||
Reference in New Issue
Block a user