From 2fef76f5e225e5d581f4194d2afaaf15ff9559ac Mon Sep 17 00:00:00 2001 From: Tim van den Eijnden Date: Thu, 12 Dec 2024 15:36:01 +0100 Subject: [PATCH] docs: add Nuxt server API route getSession example (#863) --- docs/content/docs/integrations/nuxt.mdx | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/docs/content/docs/integrations/nuxt.mdx b/docs/content/docs/integrations/nuxt.mdx index f074ad13b5..df5de5fc6c 100644 --- a/docs/content/docs/integrations/nuxt.mdx +++ b/docs/content/docs/integrations/nuxt.mdx @@ -68,6 +68,27 @@ const session = authClient.useSession() ``` +### Server Usage + +The `api` object exported from the auth instance contains all the actions that you can perform on the server. Every endpoint made inside Better Auth is a invocable as a function. Including plugins endpoints. + +**Example: Getting Session on a server API route** + +```tsx title="server/api/example.ts" +import { auth } from "~/lib/auth"; + +export default defineEventHandler((event) => { + const session = await auth.api.getSession({ + headers: event.headers + }); + + if(session) { + // access the session.session && session.user + } +}); +``` + + ### SSR Usage If you are using Nuxt with SSR, you can use the `useSession` function in the `setup` function of your page component and pass `useFetch` to make it work with SSR. @@ -107,4 +128,4 @@ export default defineNuxtRouteMiddleware(async (to, from) => { - [Nuxt and Nuxt Hub example](https://github.com/atinux/nuxthub-better-auth) on GitHub. - [NuxtZzle is Nuxt,Drizzle ORM example](https://github.com/leamsigc/nuxt-better-auth-drizzle) on github [preview](https://nuxt-better-auth.giessen.dev/) -- [Nuxt example](https://stackblitz.com/github/better-auth/better-auth/tree/main/examples/nuxt-example) on StackBlitz. \ No newline at end of file +- [Nuxt example](https://stackblitz.com/github/better-auth/better-auth/tree/main/examples/nuxt-example) on StackBlitz.