Files
CRAPP/src/integrations/supabase/auth-middleware.ts
T

106 lines
3.1 KiB
TypeScript
Raw Normal View History

2026-08-06 08:36:47 +02:00
// This file is automatically generated. Do not edit it directly.
import { createMiddleware } from "@tanstack/react-start";
import { getRequest } from "@tanstack/react-start/server";
import { createClient } from "@supabase/supabase-js";
import type { Database } from "./types";
2026-08-06 08:36:47 +02:00
function isNewSupabaseApiKey(value: string): boolean {
return value.startsWith("sb_publishable_") || value.startsWith("sb_secret_");
2026-08-06 08:36:47 +02:00
}
function createSupabaseFetch(supabaseKey: string): typeof fetch {
return (input, init) => {
const headers = new Headers(
typeof Request !== "undefined" && input instanceof Request ? input.headers : undefined,
2026-08-06 08:36:47 +02:00
);
if (init?.headers) {
new Headers(init.headers).forEach((value, key) => headers.set(key, value));
}
// New Supabase API keys are opaque strings, not bearer JWTs.
if (
isNewSupabaseApiKey(supabaseKey) &&
headers.get("Authorization") === `Bearer ${supabaseKey}`
) {
headers.delete("Authorization");
2026-08-06 08:36:47 +02:00
}
headers.set("apikey", supabaseKey);
2026-08-06 08:36:47 +02:00
return fetch(input, { ...init, headers });
};
}
export const requireSupabaseAuth = createMiddleware({ type: "function" }).server(
2026-08-06 08:36:47 +02:00
async ({ next }) => {
const SUPABASE_URL = process.env["SUPABASE_URL"];
const SUPABASE_PUBLISHABLE_KEY = process.env["SUPABASE_PUBLISHABLE_KEY"];
2026-08-06 08:36:47 +02:00
if (!SUPABASE_URL || !SUPABASE_PUBLISHABLE_KEY) {
const missing = [
...(!SUPABASE_URL ? ["SUPABASE_URL"] : []),
...(!SUPABASE_PUBLISHABLE_KEY ? ["SUPABASE_PUBLISHABLE_KEY"] : []),
2026-08-06 08:36:47 +02:00
];
const message = `Missing Supabase environment variable(s): ${missing.join(", ")}. Connect Supabase in Lovable Cloud.`;
2026-08-06 08:36:47 +02:00
console.error(`[Supabase] ${message}`);
throw new Error(message);
}
2026-08-06 08:36:47 +02:00
const request = getRequest();
if (!request?.headers) {
throw new Error("Unauthorized: No request headers available");
2026-08-06 08:36:47 +02:00
}
const authHeader = request.headers.get("authorization");
2026-08-06 08:36:47 +02:00
if (!authHeader) {
throw new Error("Unauthorized: No authorization header provided");
2026-08-06 08:36:47 +02:00
}
if (!authHeader.startsWith("Bearer ")) {
throw new Error("Unauthorized: Only Bearer tokens are supported");
2026-08-06 08:36:47 +02:00
}
const token = authHeader.replace("Bearer ", "");
2026-08-06 08:36:47 +02:00
if (!token) {
throw new Error("Unauthorized: No token provided");
2026-08-06 08:36:47 +02:00
}
if (token.split(".").length !== 3) {
throw new Error("Unauthorized: Invalid token");
2026-08-06 08:36:47 +02:00
}
const supabase = createClient<Database>(SUPABASE_URL!, SUPABASE_PUBLISHABLE_KEY!, {
global: {
fetch: createSupabaseFetch(SUPABASE_PUBLISHABLE_KEY!),
headers: {
Authorization: `Bearer ${token}`,
2026-08-06 08:36:47 +02:00
},
},
auth: {
storage: undefined,
persistSession: false,
autoRefreshToken: false,
},
});
2026-08-06 08:36:47 +02:00
const { data, error } = await supabase.auth.getClaims(token);
if (error || !data?.claims) {
throw new Error("Unauthorized: Invalid token");
2026-08-06 08:36:47 +02:00
}
if (!data.claims.sub) {
throw new Error("Unauthorized: No user ID found in token");
2026-08-06 08:36:47 +02:00
}
return next({
context: {
supabase,
userId: data.claims.sub,
claims: data.claims,
},
});
},
);