2035Builder
2035Builder4mo ago

I notice Authorization is been removed from my header on my dev environment

api.createClient({
transformer: superjson,
links: [
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
async headers(header) {
const { data } = await supabase.auth.getSession();
const token = data.session?.access_token;
console.log({ token })
return {
Authorization: `Bearer ${token}`,
'x-trpc-source': 'expo-react',
"app-token": token,

};
},
})
]
})
api.createClient({
transformer: superjson,
links: [
httpBatchLink({
url: `${getBaseUrl()}/api/trpc`,
async headers(header) {
const { data } = await supabase.auth.getSession();
const token = data.session?.access_token;
console.log({ token })
return {
Authorization: `Bearer ${token}`,
'x-trpc-source': 'expo-react',
"app-token": token,

};
},
})
]
})
my server handler [trpc].ts
import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
import { createPagesServerClient } from "@supabase/auth-helpers-nextjs";
import { createNextApiHandler } from "@trpc/server/adapters/next";
import Cors from "cors";

import { appRouter, createTRPCContext } from "@lumi/api";

// Initialize CORS middleware
const cors = Cors({
methods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["Authorization", "Content-Type", 'x-trpc-source', 'app-token'],
origin: '*'
});

// Helper function to run middleware
function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: any) {
return new Promise<void>((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result);
}
return resolve();
});
});
}

// Middleware wrapper for CORS
export function withCors(handler: NextApiHandler) {
return async (req: NextApiRequest, res: NextApiResponse) => {
await runMiddleware(req, res, cors);
return handler(req, res);
};
}

// Create TRPC handler
const trpcHandler = createNextApiHandler({
router: appRouter,
createContext: async ({ req, res }) => {
const supabase = createPagesServerClient({ req, res });
const context = createTRPCContext({ req, res, supabase });
return context;
}
});

// Export API route handler with CORS
const handler: NextApiHandler = async (req, res) => {
return withCors(trpcHandler)(req, res);
};

export default handler;
import type { NextApiHandler, NextApiRequest, NextApiResponse } from "next";
import { createPagesServerClient } from "@supabase/auth-helpers-nextjs";
import { createNextApiHandler } from "@trpc/server/adapters/next";
import Cors from "cors";

import { appRouter, createTRPCContext } from "@lumi/api";

// Initialize CORS middleware
const cors = Cors({
methods: ["GET", "POST", "OPTIONS"],
allowedHeaders: ["Authorization", "Content-Type", 'x-trpc-source', 'app-token'],
origin: '*'
});

// Helper function to run middleware
function runMiddleware(req: NextApiRequest, res: NextApiResponse, fn: any) {
return new Promise<void>((resolve, reject) => {
fn(req, res, (result: any) => {
if (result instanceof Error) {
return reject(result);
}
return resolve();
});
});
}

// Middleware wrapper for CORS
export function withCors(handler: NextApiHandler) {
return async (req: NextApiRequest, res: NextApiResponse) => {
await runMiddleware(req, res, cors);
return handler(req, res);
};
}

// Create TRPC handler
const trpcHandler = createNextApiHandler({
router: appRouter,
createContext: async ({ req, res }) => {
const supabase = createPagesServerClient({ req, res });
const context = createTRPCContext({ req, res, supabase });
return context;
}
});

// Export API route handler with CORS
const handler: NextApiHandler = async (req, res) => {
return withCors(trpcHandler)(req, res);
};

export default handler;
0 Replies
No replies yetBe the first to reply to this messageJoin