Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions docs/content/1.getting-started/2.configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ Define your authentication logic in `server/auth.config.ts`, including plugins,

Use the `defineServerAuth` helper to ensure type safety and access context.

```ts [server/auth.config.ts]
```ts twoslash [server/auth.config.ts]
import { defineServerAuth } from '@onmax/nuxt-better-auth'

export default defineServerAuth(() => {
Expand All @@ -103,7 +103,7 @@ The module automatically injects `secret` and `baseURL`. You don't need to confi

The `defineServerAuth` callback receives a context object with useful properties:

```ts [server/auth.config.ts]
```ts twoslash [server/auth.config.ts]
import { defineServerAuth } from '@onmax/nuxt-better-auth'
import { drizzleAdapter } from 'better-auth/adapters/drizzle'

Expand Down Expand Up @@ -163,8 +163,9 @@ export default defineNuxtModule({

Access sessions from server handlers:

```ts
```ts twoslash
const { user, session } = await getUserSession(event)
// ^?
if (!user) throw createError({ statusCode: 401 })
```

Expand Down
5 changes: 3 additions & 2 deletions docs/content/1.getting-started/3.client-setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,20 @@ Create `app/auth.config.ts` and export a `createAppAuthClient` function.
This factory function exists because the module needs to inject the correct `baseURL` at runtime. The module calls this function and provides the URL automatically.
::

```ts [app/auth.config.ts]
```ts twoslash [app/auth.config.ts]
import { createAuthClient } from 'better-auth/vue'

export function createAppAuthClient(baseURL: string) {
return createAuthClient({ baseURL })
// ^?
}
```

## Using Plugins

If you added a plugin in your server config (`server/auth.config.ts`), make sure to add its client equivalent here.

```ts [app/auth.config.ts]
```ts twoslash [app/auth.config.ts]
import { createAuthClient } from 'better-auth/vue'
import { adminClient } from 'better-auth/client/plugins'

Expand Down
2 changes: 1 addition & 1 deletion docs/content/1.getting-started/4.type-augmentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default defineServerAuth(() => ({

You can immediately access the `role` property in your Vue components:

```vue
```vue twoslash
<script setup lang="ts">
const { user } = useUserSession()

Expand Down
2 changes: 1 addition & 1 deletion docs/content/2.core-concepts/2.sessions.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ title: Sessions
description: Access reactive session state with SSR support using `useUserSession()`.
---

```ts [pages/dashboard.vue]
```ts twoslash [pages/dashboard.vue]
const {
user,
session,
Expand Down
7 changes: 4 additions & 3 deletions docs/content/2.core-concepts/3.route-protection.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,12 @@ definePageMeta({

Protecting your API endpoints is critical. Use `requireUserSession` to enforce authentication on server routes.

```ts [server/api/secret.get.ts]
```ts twoslash [server/api/secret.get.ts]
export default defineEventHandler(async (event) => {
// Throws 401 if not logged in
const { user } = await requireUserSession(event)

// ^?

return { secret: 'data' }
})
```
Expand Down Expand Up @@ -162,7 +163,7 @@ Always validate redirect URLs. Accepting arbitrary URLs allows attackers to redi

For complex authorization logic:

```ts [server/api/admin/report.ts]
```ts twoslash [server/api/admin/report.ts]
const session = await requireUserSession(event, {
user: { emailVerified: true },
rule: ({ user }) => user.subscriptionActive
Expand Down
2 changes: 1 addition & 1 deletion docs/content/3.guides/1.role-based-access.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export default defineNuxtConfig({
})
```

```ts [server/api/reports.ts]
```ts twoslash [server/api/reports.ts]
export default defineEventHandler(async (event) => {
await requireUserSession(event, {
rule: (session) => {
Expand Down
3 changes: 2 additions & 1 deletion docs/content/5.api/1.composables.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ description: API reference for client-side composables.

The primary composable for accessing authentication state. Returns reactive user, session, and auth client.

```ts [pages/login.vue]
```ts twoslash [pages/login.vue]
const { loggedIn, user, session, client, signIn, signOut } = useUserSession()
// ^?
```

::field-group
Expand Down
11 changes: 7 additions & 4 deletions docs/content/5.api/2.server-utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ These utilities are only available in **full mode**. In [clientOnly mode](/guide

Returns the initialized Better Auth instance (module-level singleton). Pass the event for accurate URL detection on first initialization.

```ts [server/api/admin/users.get.ts]
```ts twoslash [server/api/admin/users.get.ts]
export default defineEventHandler(async (event) => {
const auth = serverAuth(event)
const session = await auth.api.getSession({ headers: event.headers })
// ^?

if (!session) {
throw createError({ statusCode: 401 })
Expand All @@ -41,10 +42,11 @@ Use `serverAuth()` to access advanced Better Auth features not exposed by the he

Retrieves the current session from the request context without throwing an error if unauthenticated.

```ts [server/api/example.ts]
```ts twoslash [server/api/example.ts]
export default defineEventHandler(async (event) => {
const session = await getUserSession(event)

// ^?

if (session) {
// ...
}
Expand All @@ -59,8 +61,9 @@ export default defineEventHandler(async (event) => {

Ensures the user is authenticated and optionally matches specific criteria. Throws a `401 Unauthorized` or `403 Forbidden` error if checks fail.

```ts
```ts twoslash
const { user, session } = await requireUserSession(event, options?)
// ^?
```

### Options
Expand Down
2 changes: 1 addition & 1 deletion docs/nuxt.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import yaml from '@rollup/plugin-yaml'

export default defineNuxtConfig({
extends: ['docus'],
modules: ['@vueuse/nuxt', 'motion-v/nuxt'],
modules: ['nuxt-content-twoslash', '@vueuse/nuxt', 'motion-v/nuxt'],

css: ['~/assets/css/main.css'],

Expand Down
3 changes: 2 additions & 1 deletion docs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"@vercel/speed-insights": "^1.3.1",
"@vueuse/core": "^14.1.0",
"motion-v": "^1.7.4",
"nuxt": "^4.2.2"
"nuxt": "^4.2.2",
"nuxt-content-twoslash": "^0.1.2"
},
"devDependencies": {
"@iconify-json/solar": "^1.2.5",
Expand Down
Loading
Loading