authentication-proxy
Skill1 starsChanged 4 months agoMITView on GitHub
Authentication proxy pattern reference. All browser-to-backend communication MUST go through Next.js API proxy routes. Never call Supabase auth or DaaS backend directly from client components. Automatically loaded as background context.
- Sends data out
In this repository59 other files it gives its agents.
- add-buildpadSkill
- add-external-oauthSkill
- add-filesSkill
- add-i18nSkill
- add-microappSkill
- add-microfrontendSkill
- add-microfrontend-zonesSkill
- add-multitenancySkill
- add-usersSkill
- amplify-env-varsSkill
- buildpad-debugSkill
- buildpad-discoverySkill
- buildpad-implSkill
- buildpad-referenceSkill
- buildpad-reviewSkill
- buildpad-spec-batchSkill
- buildpad-spec-designSkill
- buildpad-spec-initSkill
- buildpad-spec-quickSkill
- buildpad-spec-requirementsSkill
- buildpad-spec-statusSkill
- buildpad-spec-tasksSkill
- buildpad-steering-customSkill
- buildpad-steeringSkill
- buildpad-validate-designSkill
- buildpad-validate-gapSkill
- buildpad-validate-implSkill
- buildpad-verify-completionSkill
- code-simplificationSkill
- context-engineeringSkill
- create-api-routeSkill
- create-collectionSkill
- create-componentSkill
- create-cronSkill
- create-featureSkill
- create-form-builderSkill
- create-migrationSkill
- create-projectSkill
- create-rbacSkill
- create-serviceSkill
- create-testsSkill
- create-workflowSkill
- daas-platformSkill
- debugging-and-error-recoverySkill
- generate-docsSkill
- git-workflow-and-versioningSkill
- grant-module-accessSkill
- hooks-extensionsSkill
- idea-refineSkill
- incremental-implementationSkill
- manage-scopeSkill
- performance-optimizationSkill
- planning-and-task-breakdownSkill
- relational-permissionsSkill
- review-codeSkill
- security-and-hardeningSkill
- spec-driven-developmentSkill
- subagent-delegationSkill
- worker-messagingSkill
The file
Files
Authentication Proxy Pattern
CRITICAL RULE
❌ Browser → DaaS Backend directly (CORS error!)
❌ Browser → supabase.auth.signIn() (session cookie not set for proxy)
✅ Browser → /api/auth/login → Supabase (same-origin, no CORS)
✅ Browser → /api/items/* → DaaS Backend (proxied server-side)Auth Routes
| Action | Route | Method |
| Login | /api/auth/login | POST { email, password } |
| Logout | /api/auth/logout | POST |
| Get user | /api/auth/user | GET |
| OAuth callback | /api/auth/callback | GET (handles both Supabase and external OAuth) |
| External OAuth | /api/auth/oauth/[provider] | GET (initiates external SSO) |
These routes are installed by the CLI (pnpm cli add --all or --with-api).
Login Pattern
// ✅ CORRECT: Via proxy route
const response = await fetch("/api/auth/login", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ email, password }),
credentials: "include",
});
// ❌ WRONG: Direct Supabase client
const supabase = createClient();
await supabase.auth.signInWithPassword({ email, password });Logout Pattern
// ✅ CORRECT — clears both the Supabase session AND the scope cookie
await fetch("/api/auth/logout", { method: "POST", credentials: "include" });
router.push("/login");
router.refresh();Bug 20: The logout route must clear the
daas_resource_uricookie. This cookie persists after logout and contains the previous user’s scope URI. When the next user logs in, the stale URI is forwarded asX-Resource-Uri, causing an immediate 403 FORBIDDEN_SCOPE.In
app/api/auth/logout/route.ts, insideperformLogout, always add:cookieStore.delete('daas_resource_uri');
Additionally, ScopeContext should validate the cookie against the user’s actual available scopes after loading — if the stored URI is not in the user’s list, clear it (defensive second layer).
Data API Pattern
// ✅ CORRECT: Through proxy route
const res = await fetch("/api/items/todos?filter[status][_eq]=active");
// ❌ WRONG: Direct to DaaS backend from browser
const res = await fetch("http://localhost:3000/api/items/todos");Server-Side Auth Headers
In API routes, use the auth-headers helper to forward the user's JWT:
import { getAuthHeaders } from "@/lib/api/auth-headers";
export async function GET(request: Request) {
const headers = await getAuthHeaders();
const response = await fetch(`${DAAS_URL}/api/items/todos`, { headers });
return Response.json(await response.json());
}External OAuth (SSO)
For external identity providers (Azure AD, Okta, Auth0, Google, custom OIDC):
// ✅ CORRECT: Via OAuth initiation route
const handleSSOLogin = () => {
window.location.href = "/api/auth/oauth/generic";
// Or with return URL:
window.location.href = "/api/auth/oauth/azure?returnTo=/dashboard";
};
// ❌ WRONG: Supabase's signInWithOAuth (not configured for self-hosted)
await supabase.auth.signInWithOAuth({ provider: "azure" });See add-external-oauth for full implementation details.
Key points:
- Next.js acts as OAuth client (not Supabase)
- Session set via
supabase.auth.setSession()(not manual cookies) - Claims extracted from access_token, id_token, AND userinfo endpoint
References
- Authentication proxy details
- External OAuth implementation
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

