@onmax/nuxt-better-auth 0.0.2-alpha.14 → 0.0.2-alpha.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,142 +0,0 @@
1
- # TypeScript Types
2
-
3
- ## Module Alias
4
-
5
- Import types from the module alias:
6
-
7
- ```ts
8
- import type { AuthUser, AuthSession, ServerAuthContext, AppAuthClient } from '#nuxt-better-auth'
9
- ```
10
-
11
- ## Core Types
12
-
13
- ### AuthUser
14
-
15
- User object returned by `useUserSession()` and `requireUserSession()`:
16
-
17
- ```ts
18
- interface AuthUser {
19
- id: string
20
- email: string
21
- name?: string
22
- image?: string
23
- emailVerified: boolean
24
- createdAt: Date
25
- updatedAt: Date
26
- // Plus any fields from plugins (role, etc.)
27
- }
28
- ```
29
-
30
- ### AuthSession
31
-
32
- Session object:
33
-
34
- ```ts
35
- interface AuthSession {
36
- id: string
37
- userId: string
38
- expiresAt: Date
39
- // token is filtered from exposed data
40
- }
41
- ```
42
-
43
- ## Type Inference
44
-
45
- Types are automatically inferred from your server config. The module uses `InferUser` and `InferSession` from Better Auth:
46
-
47
- ```ts
48
- // Inferred from server/auth.config.ts
49
- type AuthUser = InferUser<typeof authConfig>
50
- type AuthSession = InferSession<typeof authConfig>
51
- ```
52
-
53
- ## Plugin Type Augmentation
54
-
55
- When using plugins, types extend automatically:
56
-
57
- ```ts
58
- // With admin plugin
59
- interface AuthUser {
60
- // ... base fields
61
- role: 'user' | 'admin'
62
- }
63
-
64
- // With 2FA plugin
65
- interface AuthUser {
66
- // ... base fields
67
- twoFactorEnabled: boolean
68
- }
69
- ```
70
-
71
- ## ServerAuthContext
72
-
73
- Available in `defineServerAuth()` callback:
74
-
75
- ```ts
76
- interface ServerAuthContext {
77
- runtimeConfig: RuntimeConfig
78
- db?: DrizzleDatabase // When NuxtHub enabled
79
- }
80
- ```
81
-
82
- ## Using Types in Components
83
-
84
- ```vue
85
- <script setup lang="ts">
86
- import type { AuthUser } from '#nuxt-better-auth'
87
-
88
- const { user } = useUserSession()
89
- // user is Ref<AuthUser | null>
90
-
91
- function greet(u: AuthUser) {
92
- return `Hello, ${u.name}`
93
- }
94
- </script>
95
- ```
96
-
97
- ## Using Types in Server
98
-
99
- ```ts
100
- // server/utils/helpers.ts
101
- import type { AuthUser, AuthSession } from '#nuxt-better-auth'
102
-
103
- export function isAdmin(user: AuthUser): boolean {
104
- return user.role === 'admin'
105
- }
106
- ```
107
-
108
- ## Custom User Fields
109
-
110
- Extend user type via Better Auth config:
111
-
112
- ```ts
113
- // server/auth.config.ts
114
- export default defineServerAuth(() => ({
115
- user: {
116
- additionalFields: {
117
- plan: { type: 'string' },
118
- credits: { type: 'number' }
119
- }
120
- }
121
- }))
122
- ```
123
-
124
- Types automatically include these fields:
125
-
126
- ```ts
127
- // AuthUser now includes:
128
- interface AuthUser {
129
- // ... base fields
130
- plan: string
131
- credits: number
132
- }
133
- ```
134
-
135
- ## Type-Safe User Matching
136
-
137
- ```ts
138
- // Fully typed
139
- await requireUserSession(event, {
140
- user: { role: 'admin' } // TypeScript knows valid fields
141
- })
142
- ```