diff --git a/apps/docs/content/guides/auth/server-side/creating-a-client.mdx b/apps/docs/content/guides/auth/server-side/creating-a-client.mdx index a98ce79fa25..294d672d068 100644 --- a/apps/docs/content/guides/auth/server-side/creating-a-client.mdx +++ b/apps/docs/content/guides/auth/server-side/creating-a-client.mdx @@ -79,6 +79,29 @@ SUPABASE_URL=supabase_project_url SUPABASE_PUBLISHABLE_KEY=supabase_publishable_key ``` + + + +```bash .env +NUXT_PUBLIC_SUPABASE_URL=supabase_project_url +NUXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY=supabase_publishable_key +``` + +In `nuxt.config.ts`, map these public env vars into runtime config keys used by the examples below: + +```ts nuxt.config.ts +export default defineNuxtConfig({ + runtimeConfig: { + public: { + // These defaults will be overridden by NUXT_PUBLIC_SUPABASE_URL and + // NUXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY environment variables at runtime. + supabaseUrl: '', + supabasePublishableKey: '', + }, + }, +}) +``` + @@ -551,6 +574,79 @@ You can now use any Supabase features from your client or server code! + + + + + +```ts server/api/hello.ts +import { createServerClient, parseCookieHeader, serializeCookieHeader } from '@supabase/ssr' +import { appendHeader, defineEventHandler, getHeader } from 'h3' + +export default defineEventHandler(async (event) => { + const config = useRuntimeConfig() + + const supabase = createServerClient( + config.public.supabaseUrl, + config.public.supabasePublishableKey, + { + cookies: { + getAll() { + return parseCookieHeader(getHeader(event, 'Cookie') ?? '') + }, + setAll(cookiesToSet) { + cookiesToSet.forEach(({ name, value, options }) => { + appendHeader(event, 'Set-Cookie', serializeCookieHeader(name, value, options)) + }) + }, + }, + } + ) + + await supabase.auth.getUser() + + return { ok: true } +}) +``` + + + + + +```ts plugins/supabase.client.ts +import { createBrowserClient } from '@supabase/ssr' + +export default defineNuxtPlugin(() => { + const config = useRuntimeConfig() + + const supabase = createBrowserClient( + config.public.supabaseUrl, + config.public.supabasePublishableKey + ) + + return { + provide: { + supabase, + }, + } +}) +``` + + + + +## Congratulations + +You can now use any Supabase features from your client or server code! + + +