autogpt_platform/frontend/src/lib/supabase/SESSION_VALIDATION.md
This implementation ensures that Supabase session validation is always performed on the server side using httpOnly cookies for improved security.
All API requests made through BackendAPI are automatically proxied through server actions that:
This includes both regular API calls and file uploads, all handled transparently!
No changes needed! The existing useSupabase hook and useBackendAPI continue to work:
"use client";
import { useSupabase } from "@/lib/supabase/hooks/useSupabase";
import { useBackendAPI } from "@/lib/autogpt-server-api/context";
function MyComponent() {
const { user, isLoggedIn, isUserLoading, logOut } = useSupabase();
const api = useBackendAPI();
if (isUserLoading) return <div>Loading...</div>;
if (!isLoggedIn) return <div>Please log in</div>;
// Regular API calls use secure server-side authentication
const handleGetGraphs = async () => {
const graphs = await api.listGraphs();
console.log(graphs);
};
// File uploads also work with secure authentication
const handleFileUpload = async (file: File) => {
try {
const mediaUrl = await api.uploadStoreSubmissionMedia(file);
console.log("Uploaded:", mediaUrl);
} catch (error) {
console.error("Upload failed:", error);
}
};
return (
<div>
<p>Welcome, {user?.email}!</p>
<button onClick={handleGetGraphs}>Get Graphs</button>
<input
type="file"
onChange={(e) =>
e.target.files?.[0] && handleFileUpload(e.target.files[0])
}
/>
<button onClick={logOut}>Log Out</button>
</div>
);
}
No changes needed! Server components continue to work as before:
import { validateSession, getCurrentUser } from "@/lib/supabase/actions";
import { redirect } from "next/navigation";
async function MyServerComponent() {
const { user, error } = await getCurrentUser();
if (error || !user) {
redirect("/login");
}
return <div>Welcome, {user.email}!</div>;
}
No changes needed! Server actions continue to work as before:
"use server";
import { validateSession } from "@/lib/supabase/actions";
import BackendAPI from "@/lib/autogpt-server-api";
import { redirect } from "next/navigation";
export async function myServerAction() {
const { user, isValid } = await validateSession("/current-path");
if (!isValid || !user) {
redirect("/login");
return;
}
// This automatically uses secure server-side authentication
const api = new BackendAPI();
const graphs = await api.listGraphs();
return graphs;
}
All operations use the same simple code everywhere:
// Works the same in both client and server contexts
const api = new BackendAPI();
// Regular API requests
const graphs = await api.listGraphs();
const user = await api.createUser();
const onboarding = await api.getUserOnboarding();
// File uploads
const file = new File(["content"], "example.txt", { type: "text/plain" });
const mediaUrl = await api.uploadStoreSubmissionMedia(file);
validateSession(currentPath) - Validates the current session and returns user datagetCurrentUser() - Gets the current user without path validationserverLogout() - Logs out the user server-siderefreshSession() - Refreshes the current sessionAll API requests (including file uploads) follow this flow:
api.listGraphs() or api.uploadStoreSubmissionMedia(file)proxyApiRequest() or proxyFileUpload() handles the requestFile uploads are handled through a dedicated proxyFileUpload server action that: