[{"data":1,"prerenderedAt":608,"buildId":609},["ShallowReactive",2],{"-core-concepts-server-auth":3,"-core-concepts-server-auth-surround":390,"navigation_docs":402},{"id":4,"title":5,"body":6,"description":382,"extension":383,"links":384,"meta":385,"navigation":36,"path":386,"seo":387,"stem":388,"__hash__":389},"docs\u002F2.core-concepts\u002F1.server-auth.md","serverAuth()",{"type":7,"value":8,"toc":378},"minimark",[9,80,84,116,187,192,309,314,318,322,361,374],[10,11,12],"code-collapse",{},[13,14,20],"pre",{"className":15,"code":16,"filename":17,"language":18,"meta":19,"style":19},"language-txt shiki shiki-themes one-light synthwave-84 synthwave-84","Use server-side auth utilities in @nuxtjs\u002Fbetter-auth.\n\n- `serverAuth(event?)` — returns the Better Auth instance. Pass `event` in Nitro handlers for request-scoped database access and configuration context\n- `getUserSession(event)` — get current session (auto-imported in `server\u002F`)\n- `requireUserSession(event, options?)` — throws 401 if not authenticated, supports role matching\n- `getRequestSession(event)` — request-cached session, preferred over repeated `getUserSession` in same request\n- `setRequestSession(event, session)` — supply an authenticated session to downstream helpers for the current request\n- `refreshSessionCookieCache(event)` — refresh Better Auth's cached session cookie after server-side session data changes\n- All server utils are auto-imported, no import statements needed\n","Prompt","txt","",[21,22,23,31,38,44,50,56,62,68,74],"code",{"__ignoreMap":19},[24,25,28],"span",{"class":26,"line":27},"line",1,[24,29,30],{},"Use server-side auth utilities in @nuxtjs\u002Fbetter-auth.\n",[24,32,34],{"class":26,"line":33},2,[24,35,37],{"emptyLinePlaceholder":36},true,"\n",[24,39,41],{"class":26,"line":40},3,[24,42,43],{},"- `serverAuth(event?)` — returns the Better Auth instance. Pass `event` in Nitro handlers for request-scoped database access and configuration context\n",[24,45,47],{"class":26,"line":46},4,[24,48,49],{},"- `getUserSession(event)` — get current session (auto-imported in `server\u002F`)\n",[24,51,53],{"class":26,"line":52},5,[24,54,55],{},"- `requireUserSession(event, options?)` — throws 401 if not authenticated, supports role matching\n",[24,57,59],{"class":26,"line":58},6,[24,60,61],{},"- `getRequestSession(event)` — request-cached session, preferred over repeated `getUserSession` in same request\n",[24,63,65],{"class":26,"line":64},7,[24,66,67],{},"- `setRequestSession(event, session)` — supply an authenticated session to downstream helpers for the current request\n",[24,69,71],{"class":26,"line":70},8,[24,72,73],{},"- `refreshSessionCookieCache(event)` — refresh Better Auth's cached session cookie after server-side session data changes\n",[24,75,77],{"class":26,"line":76},9,[24,78,79],{},"- All server utils are auto-imported, no import statements needed\n",[81,82,83],"p",{},"Use this page when you need authentication state or Better Auth APIs inside Nitro handlers, middleware, plugins, or other server-side code.",[81,85,86,89,90,93,94,97,98,101,102,104,105,108,109,112,113,115],{},[21,87,88],{},"serverAuth(event?)"," returns the Better Auth instance. Pass ",[21,91,92],{},"event"," in Nitro handlers for request-scoped database access and ",[21,95,96],{},"ctx.requestOrigin",". In production, the canonical auth URL comes from ",[21,99,100],{},"runtimeConfig.public.siteUrl"," or platform environment variables. Development can infer it from the request. Outside request contexts, such as seed scripts and tasks, call ",[21,103,5],{}," without an event. On Nuxt 4.6+, when the auth secret is derived from ",[21,106,107],{},"NUXT_APP_SECRET",", it is resolved asynchronously: the module starts it when the server starts and awaits it in its handlers, middleware, and session utilities. Code that runs at server startup, such as a Nitro plugin, should use ",[21,110,111],{},"await ensureServerAuth()",", which waits for the secret and then returns the same instance as ",[21,114,5],{},".",[13,117,122],{"className":118,"code":119,"filename":120,"language":121,"meta":19,"style":19},"language-ts shiki shiki-themes one-light synthwave-84 synthwave-84","export default defineNitroPlugin(async () => {\n  const auth = await ensureServerAuth()\n  \u002F\u002F ...\n})\n","server\u002Fplugins\u002Fseed.ts","ts",[21,123,124,154,176,182],{"__ignoreMap":19},[24,125,126,130,134,138,142,145,148,151],{"class":26,"line":27},[24,127,129],{"class":128},"sqe1H","export",[24,131,133],{"class":132},"sKg8T"," default",[24,135,137],{"class":136},"sfT9l"," defineNitroPlugin",[24,139,141],{"class":140},"s17Py","(",[24,143,144],{"class":128},"async",[24,146,147],{"class":140}," () ",[24,149,150],{"class":128},"=>",[24,152,153],{"class":140}," {\n",[24,155,156,159,163,167,170,173],{"class":26,"line":33},[24,157,158],{"class":128},"  const",[24,160,162],{"class":161},"s6Rhl"," auth",[24,164,166],{"class":165},"sQBpM"," =",[24,168,169],{"class":128}," await",[24,171,172],{"class":136}," ensureServerAuth",[24,174,175],{"class":140},"()\n",[24,177,178],{"class":26,"line":40},[24,179,181],{"class":180},"st7cf","  \u002F\u002F ...\n",[24,183,184],{"class":26,"line":46},[24,185,186],{"class":140},"})\n",[188,189,191],"h2",{"id":190},"when-to-use-what","When to Use What",[193,194,195,211],"table",{},[196,197,198],"thead",{},[199,200,201,205,208],"tr",{},[202,203,204],"th",{},"Task",[202,206,207],{},"Use",[202,209,210],{},"Example",[212,213,214,228,241,254,267,280,296],"tbody",{},[199,215,216,220,225],{},[217,218,219],"td",{},"Get request-cached session context",[217,221,222],{},[21,223,224],{},"getRequestSession(event)",[217,226,227],{},"Cache once per request with context-backed storage when available",[199,229,230,233,238],{},[217,231,232],{},"Supply a request session",[217,234,235],{},[21,236,237],{},"setRequestSession(event, session)",[217,239,240],{},"Reuse a session resolved by trusted server authentication",[199,242,243,246,251],{},[217,244,245],{},"Get current session",[217,247,248],{},[21,249,250],{},"getUserSession(event)",[217,252,253],{},"Check if user is logged in",[199,255,256,259,264],{},[217,257,258],{},"Refresh cached session cookie",[217,260,261],{},[21,262,263],{},"refreshSessionCookieCache(event)",[217,265,266],{},"Use after updating data returned by Better Auth session helpers",[199,268,269,272,277],{},[217,270,271],{},"Require authentication",[217,273,274],{},[21,275,276],{},"requireUserSession(event)",[217,278,279],{},"Protect an API route",[199,281,282,285,290],{},[217,283,284],{},"Access Better Auth API",[217,286,287],{},[21,288,289],{},"serverAuth(event)",[217,291,292,293],{},"Call ",[21,294,295],{},"auth.api.listSessions()",[199,297,298,301,306],{},[217,299,300],{},"Get session with options",[217,302,303],{},[21,304,305],{},"requireUserSession(event, { user: { role: 'admin' } })",[217,307,308],{},"Role-based protection",[81,310,311,313],{},[21,312,263],{}," refreshes the cached session cookie. It does not update the session or user record; perform that update first.",[315,316],"read-more",{"to":317},"\u002Fapi\u002Fserver-utils",[188,319,321],{"id":320},"server-endpoint-path","Server endpoint path",[81,323,324,325,328,329,332,333,336,337,340,341,344,345,347,348,351,352,354,355,357,358,360],{},"The module registers Better Auth at ",[21,326,327],{},"\u002Fapi\u002Fauth"," within the Nuxt app. If ",[21,330,331],{},"app.baseURL"," is ",[21,334,335],{},"\u002Fapp\u002F",", the public endpoints and generated callback URLs use ",[21,338,339],{},"\u002Fapp\u002Fapi\u002Fauth",". ",[21,342,343],{},"defineServerAuth"," accepts only ",[21,346,327],{}," as ",[21,349,350],{},"basePath"," and rejects other values when resolving the config. Remove a custom server ",[21,353,350],{}," or set it to ",[21,356,327],{},"; the module includes ",[21,359,331],{}," automatically.",[81,362,363,364,367,368,370,371,115],{},"External backends configured with ",[21,365,366],{},"auth.clientOnly: true"," can still use a custom ",[21,369,350],{}," in ",[21,372,373],{},"defineClientAuth",[375,376,377],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sqe1H, html code.shiki .sqe1H{--shiki-light:#A626A4;--shiki-default:#FEDE5D;--shiki-dark:#FEDE5D}html pre.shiki code .sKg8T, html code.shiki .sKg8T{--shiki-light:#E45649;--shiki-default:#FEDE5D;--shiki-dark:#FEDE5D}html pre.shiki code .sfT9l, html code.shiki .sfT9l{--shiki-light:#4078F2;--shiki-default:#36F9F6;--shiki-dark:#36F9F6}html pre.shiki code .s17Py, html code.shiki .s17Py{--shiki-light:#383A42;--shiki-default:#BBBBBB;--shiki-dark:#BBBBBB}html pre.shiki code .s6Rhl, html code.shiki .s6Rhl{--shiki-light:#986801;--shiki-default:#FF7EDB;--shiki-dark:#FF7EDB}html pre.shiki code .sQBpM, html code.shiki .sQBpM{--shiki-light:#0184BC;--shiki-default:#FFFFFFEE;--shiki-dark:#FFFFFFEE}html pre.shiki code .st7cf, html code.shiki .st7cf{--shiki-light:#A0A1A7;--shiki-light-font-style:italic;--shiki-default:#848BBD;--shiki-default-font-style:italic;--shiki-dark:#848BBD;--shiki-dark-font-style:italic}",{"title":19,"searchDepth":33,"depth":33,"links":379},[380,381],{"id":190,"depth":33,"text":191},{"id":320,"depth":33,"text":321},"When to reach for the full Better Auth server instance.","md",null,{},"\u002Fcore-concepts\u002Fserver-auth",{"title":5,"description":382},"2.core-concepts\u002F1.server-auth","OG3cMivIiR0raRbf-x6iwz1oQA5NMp0SIE2IVtyF35U",[391,397],{"title":392,"path":393,"stem":394,"description":395,"icon":396,"children":-1},"How It Works","\u002Fgetting-started\u002Fhow-it-works","1.getting-started\u002F6.how-it-works","Understand the architecture after completing setup.","i-lucide-workflow",{"title":398,"path":399,"stem":400,"description":401,"children":-1},"Sessions","\u002Fcore-concepts\u002Fsessions","2.core-concepts\u002F2.sessions","Access reactive session state with SSR support using `useUserSession()`.",[403,437,457,504,529,549],{"title":404,"path":405,"stem":406,"children":407,"icon":410},"Getting Started","\u002Fgetting-started","1.getting-started\u002F0.index",[408,411,416,421,426,431,436],{"title":409,"path":405,"stem":406,"icon":410},"Introduction","i-lucide-sparkles",{"title":412,"path":413,"stem":414,"icon":415},"Installation","\u002Fgetting-started\u002Finstallation","1.getting-started\u002F1.installation","i-lucide-download",{"title":417,"path":418,"stem":419,"icon":420},"Configuration","\u002Fgetting-started\u002Fconfiguration","1.getting-started\u002F2.configuration","i-lucide-settings",{"title":422,"path":423,"stem":424,"icon":425},"Client Setup","\u002Fgetting-started\u002Fclient-setup","1.getting-started\u002F3.client-setup","i-lucide-monitor",{"title":427,"path":428,"stem":429,"icon":430},"Type Augmentation","\u002Fgetting-started\u002Ftype-augmentation","1.getting-started\u002F4.type-augmentation","i-lucide-type",{"title":432,"path":433,"stem":434,"icon":435},"Schema Generation (NuxtHub)","\u002Fgetting-started\u002Fschema-generation","1.getting-started\u002F5.schema-generation","i-lucide-database",{"title":392,"path":393,"stem":394,"icon":396},{"title":438,"path":439,"stem":440,"children":441,"page":456},"Core Concepts","\u002Fcore-concepts","2.core-concepts",[442,443,444,448,452],{"title":5,"path":386,"stem":388},{"title":398,"path":399,"stem":400},{"title":445,"path":446,"stem":447},"Route Protection","\u002Fcore-concepts\u002Froute-protection","2.core-concepts\u002F3.route-protection",{"title":449,"path":450,"stem":451},"Auto‑Imports and Aliases","\u002Fcore-concepts\u002Fauto-imports-aliases","2.core-concepts\u002F4.auto-imports-aliases",{"title":453,"path":454,"stem":455},"Security & Caveats","\u002Fcore-concepts\u002Fsecurity-caveats","2.core-concepts\u002F5.security-caveats",false,{"title":458,"path":459,"stem":460,"children":461,"page":456},"Guides","\u002Fguides","3.guides",[462,466,471,475,479,483,488,492,496,500],{"title":463,"path":464,"stem":465},"Role‑Based Access","\u002Fguides\u002Frole-based-access","3.guides\u002F1.role-based-access",{"title":467,"path":468,"stem":469,"icon":470},"Setup diagnostics","\u002Fguides\u002Fdiagnostics","3.guides\u002F10.diagnostics","i-lucide-circle-alert",{"title":472,"path":473,"stem":474},"OAuth Providers","\u002Fguides\u002Foauth-providers","3.guides\u002F2.oauth-providers",{"title":476,"path":477,"stem":478},"Custom Database","\u002Fguides\u002Fcustom-database","3.guides\u002F3.custom-database",{"title":480,"path":481,"stem":482},"Database-less Mode","\u002Fguides\u002Fdatabase-less-mode","3.guides\u002F4.database-less-mode",{"title":484,"path":485,"stem":486,"icon":487},"External Auth Backend","\u002Fguides\u002Fexternal-auth-backend","3.guides\u002F5.external-auth-backend","i-lucide-server",{"title":489,"path":490,"stem":491},"Migrating from nuxt-auth-utils","\u002Fguides\u002Fmigrate-from-nuxt-auth-utils","3.guides\u002F6.migrate-from-nuxt-auth-utils",{"title":493,"path":494,"stem":495},"Two-Factor Authentication (TOTP + Backup Codes)","\u002Fguides\u002Ftwo-factor-auth","3.guides\u002F7.two-factor-auth",{"title":497,"path":498,"stem":499},"Testing","\u002Fguides\u002Ftesting","3.guides\u002F8.testing",{"title":501,"path":502,"stem":503},"Production Deployment","\u002Fguides\u002Fproduction-deployment","3.guides\u002F9.production-deployment",{"title":505,"path":506,"stem":507,"children":508,"page":456},"Integrations","\u002Fintegrations","4.integrations",[509,513,517,521,525],{"title":510,"path":511,"stem":512},"NuxtHub","\u002Fintegrations\u002Fnuxthub","4.integrations\u002F1.nuxthub",{"title":514,"path":515,"stem":516},"DevTools","\u002Fintegrations\u002Fdevtools","4.integrations\u002F2.devtools",{"title":518,"path":519,"stem":520},"Convex","\u002Fintegrations\u002Fconvex","4.integrations\u002F3.convex",{"title":522,"path":523,"stem":524},"i18n","\u002Fintegrations\u002Fi18n","4.integrations\u002F4.i18n",{"title":526,"path":527,"stem":528},"MCP","\u002Fintegrations\u002Fmcp","4.integrations\u002F5.mcp",{"title":530,"path":531,"stem":532,"children":533,"page":456},"API Reference","\u002Fapi","5.api",[534,538,541,545],{"title":535,"path":536,"stem":537},"Composables","\u002Fapi\u002Fcomposables","5.api\u002F1.composables",{"title":539,"path":317,"stem":540},"Server Utilities","5.api\u002F2.server-utils",{"title":542,"path":543,"stem":544},"Components","\u002Fapi\u002Fcomponents","5.api\u002F3.components",{"title":546,"path":547,"stem":548},"Types","\u002Fapi\u002Ftypes","5.api\u002F4.types",{"title":550,"path":551,"stem":552,"children":553,"icon":470},"Errors","\u002Ferrors","6.errors\u002F0.index",[554,556,560,564,568,572,576,580,584,588,592,596,600,604],{"title":555,"path":551,"stem":552},"Overview",{"title":557,"path":558,"stem":559},"Better Auth CLI is not installed","\u002Ferrors\u002Fnuxt-auth-cli-missing","6.errors\u002Fnuxt-auth-cli-missing",{"title":561,"path":562,"stem":563},"Better Auth CLI version does not match better-auth","\u002Ferrors\u002Fnuxt-auth-cli-version-mismatch","6.errors\u002Fnuxt-auth-cli-version-mismatch",{"title":565,"path":566,"stem":567},"Auth configuration failed to load","\u002Ferrors\u002Fnuxt-auth-config-load-failed","6.errors\u002Fnuxt-auth-config-load-failed",{"title":569,"path":570,"stem":571},"Schema generator returned no code","\u002Ferrors\u002Fnuxt-auth-empty-schema","6.errors\u002Fnuxt-auth-empty-schema",{"title":573,"path":574,"stem":575},"Invalid server auth config export","\u002Ferrors\u002Fnuxt-auth-invalid-config-export","6.errors\u002Fnuxt-auth-invalid-config-export",{"title":577,"path":578,"stem":579},"Auth plugin path must be absolute","\u002Ferrors\u002Fnuxt-auth-invalid-plugin-source","6.errors\u002Fnuxt-auth-invalid-plugin-source",{"title":581,"path":582,"stem":583},"Missing auth configuration","\u002Ferrors\u002Fnuxt-auth-missing-config","6.errors\u002Fnuxt-auth-missing-config",{"title":585,"path":586,"stem":587},"NuxtHub database alias is missing","\u002Ferrors\u002Fnuxt-auth-missing-hub-db","6.errors\u002Fnuxt-auth-missing-hub-db",{"title":589,"path":590,"stem":591},"No database provider is enabled","\u002Ferrors\u002Fnuxt-auth-no-database-provider","6.errors\u002Fnuxt-auth-no-database-provider",{"title":593,"path":594,"stem":595},"Auth schema generation failed","\u002Ferrors\u002Fnuxt-auth-schema-generation-failed","6.errors\u002Fnuxt-auth-schema-generation-failed",{"title":597,"path":598,"stem":599},"Custom session storage is missing","\u002Ferrors\u002Fnuxt-auth-schema-storage-required","6.errors\u002Fnuxt-auth-schema-storage-required",{"title":601,"path":602,"stem":603},"Secondary storage in client-only mode","\u002Ferrors\u002Fnuxt-auth-storage-client-only","6.errors\u002Fnuxt-auth-storage-client-only",{"title":605,"path":606,"stem":607},"Unsupported database dialect","\u002Ferrors\u002Fnuxt-auth-unsupported-dialect","6.errors\u002Fnuxt-auth-unsupported-dialect",1796421057685,"4985160f-6317-408d-be23-822b99983e78"]