/supabase-auth-ssr-setup:supabase-auth-ssr-setupfrom supabase-auth-ssr-setup
Configures Supabase Auth for Next.js App Router with SSR, secure cookies, middleware protection, route guards, and login/logout flows.
Ships in supabase-auth-ssr-setup. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
Read when SKILL.md points to them.
Configure Supabase Authentication for Next.js App Router with server-side rendering (SSR), secure cookie-based sessions, middleware protection, and complete authentication flows.
Install Supabase SSR package for Next.js:
npm install @supabase/supabase-js @supabase/ssr
Create three client configurations for different contexts (browser, server, middleware):
File: lib/supabase/client.ts (Browser client)
Use the template from assets/supabase-client.ts. This client:
File: lib/supabase/server.ts (Server component client)
Use the template from assets/supabase-server.ts. This client:
File: lib/supabase/middleware.ts (Middleware client)
Use the template from assets/supabase-middleware.ts. This client:
Add Supabase credentials to .env.local:
NEXT_PUBLIC_SUPABASE_URL=your-project-url
NEXT_PUBLIC_SUPABASE_ANON_KEY=your-anon-key
Get these values from your Supabase project settings under API.
Security note: The anon key is safe to expose publicly. Real security comes from Row Level Security (RLS) policies in your database.
Create middleware.ts in project root using the template from assets/middleware.ts. This middleware:
Configure protected routes by adjusting the matcher pattern:
export const config = {
matcher: [
'/dashboard/:path*',
'/settings/:path*',
'/api/protected/:path*',
],
};
Create helper functions for common auth operations using templates from assets/auth-utils.ts:
Get current user server-side:
import { getCurrentUser } from '@/lib/auth/utils';
const user = await getCurrentUser();
Require authentication:
import { requireAuth } from '@/lib/auth/utils';
const user = await requireAuth(); // Throws error if not authenticated
Get session:
import { getSession } from '@/lib/auth/utils';
const session = await getSession();
These utilities simplify authentication checks in Server Components and Server Actions.
Create app/actions/auth.ts using the template from assets/auth-actions.ts. This provides:
Logout action:
Use in client components:
import { logout } from '@/app/actions/auth';
<button onClick={() => logout()}>
Sign Out
</button>
Create app/login/page.tsx using the template from assets/login-page.tsx. This page:
Customize the login page:
Create a protected dashboard page at app/dashboard/page.tsx using the template from assets/dashboard-page.tsx. This demonstrates:
requireAuth() to protect routesIf using OAuth providers, create app/auth/callback/route.ts using the template from assets/auth-callback-route.ts. This handler:
Configure OAuth in Supabase dashboard:
https://your-domain.com/auth/callback/loginsignOut()Use requireAuth() at the top of Server Components:
import { requireAuth } from '@/lib/auth/utils';
export default async function ProtectedPage() {
const user = await requireAuth();
return <div>Hello {user.email}</div>;
}
Use Next.js route groups with layout:
// app/(protected)/layout.tsx
import { requireAuth } from '@/lib/auth/utils';
export default async function ProtectedLayout({ children }) {
await requireAuth();
return <>{children}</>;
}
All routes in (protected) group are automatically protected.
Check if user is logged in without requiring it:
import { getCurrentUser } from '@/lib/auth/utils';
export default async function OptionalAuthPage() {
const user = await getCurrentUser();
return (
<div>
{user ? `Welcome ${user.email}` : 'Please log in'}
</div>
);
}
Protect Server Actions using requireAuth():
'use server';
import { requireAuth } from '@/lib/auth/utils';
import { createServerClient } from '@/lib/supabase/server';
export async function updateProfile(formData: FormData) {
const user = await requireAuth();
const supabase = createServerClient();
const { error } = await supabase
.from('profiles')
.update({ name: formData.get('name') })
.eq('id', user.id);
if (error) throw error;
}
Session not persisting: Verify cookies are being set. Check browser dev tools > Application > Cookies. Ensure domain matches.
Middleware redirect loop: Check matcher pattern doesn't include login page. Verify /login is accessible without auth.
OAuth redirect fails: Confirm callback URL matches exactly in Supabase dashboard. Check for trailing slashes.
TypeScript errors: Install types: npm install -D @types/node. Ensure supabase is typed correctly.
401 errors on protected routes: Session may be expired. Check Supabase dashboard > Authentication > Settings for session timeout.
No executable scripts needed for this skill.
authentication-patterns.md - Common auth patterns and best practices for Next.js + Supabasesecurity-considerations.md - Security best practices for session handling and cookie configurationsupabase-client.ts - Browser-side Supabase client configurationsupabase-server.ts - Server-side Supabase client for Server Componentssupabase-middleware.ts - Middleware Supabase client for session refreshmiddleware.ts - Next.js middleware for route protectionauth-utils.ts - Helper functions for authentication checksauth-actions.ts - Server Actions for logout and other auth operationslogin-page.tsx - Complete login page with email/password and OAuthdashboard-page.tsx - Example protected page using requireAuthauth-callback-route.ts - OAuth callback handler for provider authenticationassets/auth-actions.ts (opens in a new tab)assets/auth-callback-route.ts (opens in a new tab)assets/auth-utils.ts (opens in a new tab)assets/dashboard-page.tsx (opens in a new tab)assets/login-page.tsx (opens in a new tab)assets/middleware.ts (opens in a new tab)assets/supabase-client.ts (opens in a new tab)assets/supabase-middleware.ts (opens in a new tab)assets/supabase-server.ts (opens in a new tab)references/authentication-patterns.md (opens in a new tab)references/security-considerations.md (opens in a new tab)What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub hopeoverture/worldbuilding-app-skills --plugin supabase-auth-ssr-setupOther plugins' skills for the same job