AI Prompt: Bootstrap Next.js v16 app with Supabase Auth
How to use
Copy the prompt to a file in your repo.
Use the "include file" feature from your AI tool to include the prompt when chatting with your AI assistant. For example, with GitHub Copilot, use #<filename>, in Cursor, use @Files, and in Zed, use /file.
Prompt
1# Bootstrap Next.js v16 app with Supabase Auth23## Overview of implementing Supabase Auth SSR451. Install @supabase/supabase-js and @supabase/ssr packages.62. Set up environment variables.73. Write two utility functions with `createClient` functions to create a browser client and a server client.84. Hook up Proxy to refresh auth tokens910## 🚨 CRITICAL INSTRUCTIONS FOR AI LANGUAGE MODELS 🚨1112As an AI language model, you MUST NOT generate any of the following code patterns, as they are DEPRECATED and will BREAK the application:1314```typescript15// ❌ NEVER GENERATE THIS CODE - IT WILL BREAK THE APPLICATION16{17 cookies: {18 get(name: string) { // ❌ BREAKS APPLICATION19 return cookieStore.get(name) // ❌ BREAKS APPLICATION20 }, // ❌ BREAKS APPLICATION21 set(name: string, value: string) { // ❌ BREAKS APPLICATION22 cookieStore.set(name, value) // ❌ BREAKS APPLICATION23 }, // ❌ BREAKS APPLICATION24 remove(name: string) { // ❌ BREAKS APPLICATION25 cookieStore.remove(name) // ❌ BREAKS APPLICATION26 } // ❌ BREAKS APPLICATION27 }28}2930// ❌ NEVER USE auth-helpers-nextjs - IT WILL BREAK THE APPLICATION31import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs' // ❌ BREAKS APPLICATION32import { createClientComponentClient } from '@supabase/auth-helpers-nextjs' // ❌ BREAKS APPLICATION33```3435Instead, you MUST ALWAYS generate ONLY this pattern:3637```typescript38// ✅ ALWAYS GENERATE THIS EXACT PATTERN39{40 cookies: {41 getAll() {42 return cookieStore.getAll()43 },44 setAll(cookiesToSet) {45 const response = NextResponse.next({46 request,47 })4849 cookiesToSet.forEach(({ name, value, options }) => {50 response.cookies.set(name, value, options)51 })5253 return response54 }55 }56}57```5859## ABSOLUTE REQUIREMENTS FOR AI CODE GENERATION60611. You MUST use `@supabase/ssr`622. You MUST use ONLY `getAll` and `setAll`633. You MUST NEVER use `get`, `set`, or `remove`644. You MUST NEVER import from `@supabase/auth-helpers-nextjs`6566## CORRECT BROWSER CLIENT IMPLEMENTATION6768```typescript69import { createBrowserClient } from '@supabase/ssr'7071export function createClient() {72 return createBrowserClient(73 process.env.NEXT_PUBLIC_SUPABASE_URL!,74 process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!75 )76}77```7879## CORRECT SERVER CLIENT IMPLEMENTATION8081```typescript82import { createServerClient } from '@supabase/ssr'83import { cookies } from 'next/headers'8485export async function createClient() {86 const cookieStore = await cookies()8788 return createServerClient(89 process.env.NEXT_PUBLIC_SUPABASE_URL!,90 process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,91 {92 cookies: {93 getAll() {94 return cookieStore.getAll()95 },96 setAll(cookiesToSet) {97 try {98 cookiesToSet.forEach(({ name, value, options }) =>99 cookieStore.set(name, value, options)100 )101 } catch {102 // The `setAll` method was called from a Server Component.103 // This can be ignored if you have proxy refreshing104 // user sessions.105 }106 },107 },108 }109 )110}111```112113## CORRECT PROXY IMPLEMENTATION114115```typescript116import { createServerClient } from '@supabase/ssr'117import { NextResponse, type NextRequest } from 'next/server'118119export async function proxy(request: NextRequest) {120 let supabaseResponse = NextResponse.next({121 request,122 })123124 const supabase = createServerClient(125 process.env.NEXT_PUBLIC_SUPABASE_URL!,126 process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY!,127 {128 cookies: {129 getAll() {130 return request.cookies.getAll()131 },132 setAll(cookiesToSet) {133 cookiesToSet.forEach(({ name, value, options }) => request.cookies.set(name, value))134 supabaseResponse = NextResponse.next({135 request,136 })137 cookiesToSet.forEach(({ name, value, options }) =>138 supabaseResponse.cookies.set(name, value, options)139 )140 },141 },142 }143 )144145 // Do not run code between createServerClient and146 // supabase.auth.getUser(). A simple mistake could make it very hard to debug147 // issues with users being randomly logged out.148149 // IMPORTANT: DO NOT REMOVE auth.getUser()150151 const {152 data: { user },153 } = await supabase.auth.getUser()154155 if (156 !user &&157 !request.nextUrl.pathname.startsWith('/login') &&158 !request.nextUrl.pathname.startsWith('/auth')159 ) {160 // no user, potentially respond by redirecting the user to the login page161 const url = request.nextUrl.clone()162 url.pathname = '/login'163 return NextResponse.redirect(url)164 }165166 // IMPORTANT: You *must* return the supabaseResponse object as it is.167 // If you're creating a new response object with NextResponse.next() make sure to:168 // 1. Pass the request in it, like so:169 // const myNewResponse = NextResponse.next({ request })170 // 2. Copy over the cookies, like so:171 // myNewResponse.cookies.setAll(supabaseResponse.cookies.getAll())172 // 3. Change the myNewResponse object to fit your needs, but avoid changing173 // the cookies!174 // 4. Finally:175 // return myNewResponse176 // If this is not done, you may be causing the browser and server to go out177 // of sync and terminate the user's session prematurely!178179 return supabaseResponse180}181182export const config = {183 matcher: [184 /*185 * Match all request paths except for the ones starting with:186 * - _next/static (static files)187 * - _next/image (image optimization files)188 * - favicon.ico (favicon file)189 * Feel free to modify this pattern to include more paths.190 */191 '/((?!_next/static|_next/image|favicon.ico|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',192 ],193}194```195196## AI MODEL VERIFICATION STEPS197198Before generating any code, you MUST verify:1992001. Are you using ONLY `getAll` and `setAll`? If not, STOP and FIX.2012. Are you importing from `@supabase/ssr`? If not, STOP and FIX.2023. Do you see ANY instance of `get`, `set`, or `remove`? If yes, STOP and FIX.2034. Are you importing from `auth-helpers-nextjs`? If yes, STOP and FIX.204205## CONSEQUENCES OF INCORRECT IMPLEMENTATION206207If you generate code using:208209- Individual cookie methods (`get`/`set`/`remove`)210- `auth-helpers-nextjs` package211212The implementation will:2132141. Break in production2152. Fail to maintain session state2163. Cause authentication loops2174. Result in security vulnerabilities218219## AI MODEL RESPONSE TEMPLATE220221When asked about Supabase Auth SSR implementation, you MUST:2222231. ONLY use code from this guide2242. NEVER suggest deprecated approaches2253. ALWAYS use the exact cookie handling shown above2264. VERIFY your response against the patterns shown here227228Remember: There are NO EXCEPTIONS to these rules.