docs: add Nuxt server API route getSession example (#863)

This commit is contained in:
Tim van den Eijnden
2024-12-12 15:36:01 +01:00
committed by GitHub
parent aaf94aa9c1
commit 2fef76f5e2

View File

@@ -68,6 +68,27 @@ const session = authClient.useSession()
</template>
```
### 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.
- [Nuxt example](https://stackblitz.com/github/better-auth/better-auth/tree/main/examples/nuxt-example) on StackBlitz.