@delmaredigital/payload-better-auth 0.3.8 → 0.3.10

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.
Files changed (206) hide show
  1. package/dist/adapter/collections.d.ts +0 -1
  2. package/dist/adapter/collections.js +0 -2
  3. package/dist/adapter/index.d.ts +0 -1
  4. package/dist/adapter/index.js +0 -2
  5. package/dist/components/BeforeLogin.d.ts +0 -1
  6. package/dist/components/BeforeLogin.js +0 -2
  7. package/dist/components/LoginView.d.ts +0 -1
  8. package/dist/components/LoginView.js +0 -2
  9. package/dist/components/LoginViewWrapper.d.ts +0 -1
  10. package/dist/components/LoginViewWrapper.js +0 -2
  11. package/dist/components/LogoutButton.d.ts +0 -1
  12. package/dist/components/LogoutButton.js +0 -2
  13. package/dist/components/PasskeyRegisterButton.d.ts +0 -1
  14. package/dist/components/PasskeyRegisterButton.js +0 -2
  15. package/dist/components/PasskeySignInButton.d.ts +0 -1
  16. package/dist/components/PasskeySignInButton.js +0 -2
  17. package/dist/components/auth/ForgotPasswordView.d.ts +0 -1
  18. package/dist/components/auth/ForgotPasswordView.js +0 -2
  19. package/dist/components/auth/ResetPasswordView.d.ts +0 -1
  20. package/dist/components/auth/ResetPasswordView.js +0 -2
  21. package/dist/components/auth/index.d.ts +0 -1
  22. package/dist/components/auth/index.js +0 -2
  23. package/dist/components/management/ApiKeysManagementClient.d.ts +0 -1
  24. package/dist/components/management/ApiKeysManagementClient.js +0 -2
  25. package/dist/components/management/PasskeysManagementClient.d.ts +0 -1
  26. package/dist/components/management/PasskeysManagementClient.js +0 -2
  27. package/dist/components/management/SecurityNavLinks.d.ts +0 -1
  28. package/dist/components/management/SecurityNavLinks.js +0 -2
  29. package/dist/components/management/TwoFactorManagementClient.d.ts +0 -1
  30. package/dist/components/management/TwoFactorManagementClient.js +0 -2
  31. package/dist/components/management/index.d.ts +0 -1
  32. package/dist/components/management/index.js +0 -2
  33. package/dist/components/management/views/ApiKeysView.d.ts +0 -1
  34. package/dist/components/management/views/ApiKeysView.js +0 -2
  35. package/dist/components/management/views/PasskeysView.d.ts +0 -1
  36. package/dist/components/management/views/PasskeysView.js +0 -2
  37. package/dist/components/management/views/TwoFactorView.d.ts +0 -1
  38. package/dist/components/management/views/TwoFactorView.js +0 -2
  39. package/dist/components/management/views/index.d.ts +0 -1
  40. package/dist/components/management/views/index.js +0 -2
  41. package/dist/components/twoFactor/TwoFactorSetupView.d.ts +0 -1
  42. package/dist/components/twoFactor/TwoFactorSetupView.js +0 -2
  43. package/dist/components/twoFactor/TwoFactorVerifyView.d.ts +0 -1
  44. package/dist/components/twoFactor/TwoFactorVerifyView.js +0 -2
  45. package/dist/components/twoFactor/index.d.ts +0 -1
  46. package/dist/components/twoFactor/index.js +0 -2
  47. package/dist/exports/client.d.ts +0 -1
  48. package/dist/exports/client.js +0 -2
  49. package/dist/exports/components.d.ts +0 -1
  50. package/dist/exports/components.js +0 -2
  51. package/dist/exports/management.d.ts +0 -1
  52. package/dist/exports/management.js +0 -2
  53. package/dist/exports/rsc.d.ts +0 -1
  54. package/dist/exports/rsc.js +0 -2
  55. package/dist/generated-types.d.ts +0 -1
  56. package/dist/generated-types.js +0 -2
  57. package/dist/index.d.ts +0 -1
  58. package/dist/index.js +0 -2
  59. package/dist/plugin/index.d.ts +0 -1
  60. package/dist/plugin/index.js +0 -2
  61. package/dist/scripts/generate-types.d.ts +0 -1
  62. package/dist/scripts/generate-types.js +0 -2
  63. package/dist/types/apiKey.d.ts +0 -1
  64. package/dist/types/apiKey.js +0 -2
  65. package/dist/types/betterAuth.d.ts +0 -1
  66. package/dist/types/betterAuth.js +0 -2
  67. package/dist/utils/access.d.ts +0 -1
  68. package/dist/utils/access.js +0 -2
  69. package/dist/utils/apiKeyAccess.d.ts +0 -1
  70. package/dist/utils/apiKeyAccess.js +0 -2
  71. package/dist/utils/betterAuthDefaults.d.ts +0 -1
  72. package/dist/utils/betterAuthDefaults.js +0 -2
  73. package/dist/utils/detectAuthConfig.d.ts +0 -1
  74. package/dist/utils/detectAuthConfig.js +0 -2
  75. package/dist/utils/detectEnabledPlugins.d.ts +0 -1
  76. package/dist/utils/detectEnabledPlugins.js +0 -2
  77. package/dist/utils/firstUserAdmin.d.ts +0 -1
  78. package/dist/utils/firstUserAdmin.js +0 -2
  79. package/dist/utils/generateScopes.d.ts +0 -1
  80. package/dist/utils/generateScopes.js +0 -2
  81. package/dist/utils/session.d.ts +0 -1
  82. package/dist/utils/session.js +0 -2
  83. package/package.json +34 -91
  84. package/dist/adapter/collections.d.ts.map +0 -1
  85. package/dist/adapter/collections.js.map +0 -1
  86. package/dist/adapter/index.d.ts.map +0 -1
  87. package/dist/adapter/index.js.map +0 -1
  88. package/dist/components/BeforeLogin.d.ts.map +0 -1
  89. package/dist/components/BeforeLogin.js.map +0 -1
  90. package/dist/components/LoginView.d.ts.map +0 -1
  91. package/dist/components/LoginView.js.map +0 -1
  92. package/dist/components/LoginViewWrapper.d.ts.map +0 -1
  93. package/dist/components/LoginViewWrapper.js.map +0 -1
  94. package/dist/components/LogoutButton.d.ts.map +0 -1
  95. package/dist/components/LogoutButton.js.map +0 -1
  96. package/dist/components/PasskeyRegisterButton.d.ts.map +0 -1
  97. package/dist/components/PasskeyRegisterButton.js.map +0 -1
  98. package/dist/components/PasskeySignInButton.d.ts.map +0 -1
  99. package/dist/components/PasskeySignInButton.js.map +0 -1
  100. package/dist/components/auth/ForgotPasswordView.d.ts.map +0 -1
  101. package/dist/components/auth/ForgotPasswordView.js.map +0 -1
  102. package/dist/components/auth/ResetPasswordView.d.ts.map +0 -1
  103. package/dist/components/auth/ResetPasswordView.js.map +0 -1
  104. package/dist/components/auth/index.d.ts.map +0 -1
  105. package/dist/components/auth/index.js.map +0 -1
  106. package/dist/components/management/ApiKeysManagementClient.d.ts.map +0 -1
  107. package/dist/components/management/ApiKeysManagementClient.js.map +0 -1
  108. package/dist/components/management/PasskeysManagementClient.d.ts.map +0 -1
  109. package/dist/components/management/PasskeysManagementClient.js.map +0 -1
  110. package/dist/components/management/SecurityNavLinks.d.ts.map +0 -1
  111. package/dist/components/management/SecurityNavLinks.js.map +0 -1
  112. package/dist/components/management/TwoFactorManagementClient.d.ts.map +0 -1
  113. package/dist/components/management/TwoFactorManagementClient.js.map +0 -1
  114. package/dist/components/management/index.d.ts.map +0 -1
  115. package/dist/components/management/index.js.map +0 -1
  116. package/dist/components/management/views/ApiKeysView.d.ts.map +0 -1
  117. package/dist/components/management/views/ApiKeysView.js.map +0 -1
  118. package/dist/components/management/views/PasskeysView.d.ts.map +0 -1
  119. package/dist/components/management/views/PasskeysView.js.map +0 -1
  120. package/dist/components/management/views/TwoFactorView.d.ts.map +0 -1
  121. package/dist/components/management/views/TwoFactorView.js.map +0 -1
  122. package/dist/components/management/views/index.d.ts.map +0 -1
  123. package/dist/components/management/views/index.js.map +0 -1
  124. package/dist/components/twoFactor/TwoFactorSetupView.d.ts.map +0 -1
  125. package/dist/components/twoFactor/TwoFactorSetupView.js.map +0 -1
  126. package/dist/components/twoFactor/TwoFactorVerifyView.d.ts.map +0 -1
  127. package/dist/components/twoFactor/TwoFactorVerifyView.js.map +0 -1
  128. package/dist/components/twoFactor/index.d.ts.map +0 -1
  129. package/dist/components/twoFactor/index.js.map +0 -1
  130. package/dist/exports/client.d.ts.map +0 -1
  131. package/dist/exports/client.js.map +0 -1
  132. package/dist/exports/components.d.ts.map +0 -1
  133. package/dist/exports/components.js.map +0 -1
  134. package/dist/exports/management.d.ts.map +0 -1
  135. package/dist/exports/management.js.map +0 -1
  136. package/dist/exports/rsc.d.ts.map +0 -1
  137. package/dist/exports/rsc.js.map +0 -1
  138. package/dist/generated-types.d.ts.map +0 -1
  139. package/dist/generated-types.js.map +0 -1
  140. package/dist/index.d.ts.map +0 -1
  141. package/dist/index.js.map +0 -1
  142. package/dist/plugin/index.d.ts.map +0 -1
  143. package/dist/plugin/index.js.map +0 -1
  144. package/dist/scripts/generate-types.d.ts.map +0 -1
  145. package/dist/scripts/generate-types.js.map +0 -1
  146. package/dist/types/apiKey.d.ts.map +0 -1
  147. package/dist/types/apiKey.js.map +0 -1
  148. package/dist/types/betterAuth.d.ts.map +0 -1
  149. package/dist/types/betterAuth.js.map +0 -1
  150. package/dist/utils/access.d.ts.map +0 -1
  151. package/dist/utils/access.js.map +0 -1
  152. package/dist/utils/apiKeyAccess.d.ts.map +0 -1
  153. package/dist/utils/apiKeyAccess.js.map +0 -1
  154. package/dist/utils/betterAuthDefaults.d.ts.map +0 -1
  155. package/dist/utils/betterAuthDefaults.js.map +0 -1
  156. package/dist/utils/detectAuthConfig.d.ts.map +0 -1
  157. package/dist/utils/detectAuthConfig.js.map +0 -1
  158. package/dist/utils/detectEnabledPlugins.d.ts.map +0 -1
  159. package/dist/utils/detectEnabledPlugins.js.map +0 -1
  160. package/dist/utils/firstUserAdmin.d.ts.map +0 -1
  161. package/dist/utils/firstUserAdmin.js.map +0 -1
  162. package/dist/utils/generateScopes.d.ts.map +0 -1
  163. package/dist/utils/generateScopes.js.map +0 -1
  164. package/dist/utils/session.d.ts.map +0 -1
  165. package/dist/utils/session.js.map +0 -1
  166. package/src/adapter/collections.ts +0 -621
  167. package/src/adapter/index.ts +0 -712
  168. package/src/components/BeforeLogin.tsx +0 -39
  169. package/src/components/LoginView.tsx +0 -1516
  170. package/src/components/LoginViewWrapper.tsx +0 -35
  171. package/src/components/LogoutButton.tsx +0 -58
  172. package/src/components/PasskeyRegisterButton.tsx +0 -105
  173. package/src/components/PasskeySignInButton.tsx +0 -96
  174. package/src/components/auth/ForgotPasswordView.tsx +0 -274
  175. package/src/components/auth/ResetPasswordView.tsx +0 -331
  176. package/src/components/auth/index.ts +0 -8
  177. package/src/components/management/ApiKeysManagementClient.tsx +0 -988
  178. package/src/components/management/PasskeysManagementClient.tsx +0 -409
  179. package/src/components/management/SecurityNavLinks.tsx +0 -117
  180. package/src/components/management/TwoFactorManagementClient.tsx +0 -560
  181. package/src/components/management/index.ts +0 -20
  182. package/src/components/management/views/ApiKeysView.tsx +0 -57
  183. package/src/components/management/views/PasskeysView.tsx +0 -42
  184. package/src/components/management/views/TwoFactorView.tsx +0 -42
  185. package/src/components/management/views/index.ts +0 -10
  186. package/src/components/twoFactor/TwoFactorSetupView.tsx +0 -515
  187. package/src/components/twoFactor/TwoFactorVerifyView.tsx +0 -238
  188. package/src/components/twoFactor/index.ts +0 -8
  189. package/src/exports/client.ts +0 -77
  190. package/src/exports/components.ts +0 -30
  191. package/src/exports/management.ts +0 -25
  192. package/src/exports/rsc.ts +0 -11
  193. package/src/generated-types.ts +0 -269
  194. package/src/index.ts +0 -135
  195. package/src/plugin/index.ts +0 -834
  196. package/src/scripts/generate-types.ts +0 -269
  197. package/src/types/apiKey.ts +0 -63
  198. package/src/types/betterAuth.ts +0 -253
  199. package/src/utils/access.ts +0 -410
  200. package/src/utils/apiKeyAccess.ts +0 -443
  201. package/src/utils/betterAuthDefaults.ts +0 -102
  202. package/src/utils/detectAuthConfig.ts +0 -47
  203. package/src/utils/detectEnabledPlugins.ts +0 -69
  204. package/src/utils/firstUserAdmin.ts +0 -164
  205. package/src/utils/generateScopes.ts +0 -150
  206. package/src/utils/session.ts +0 -91
@@ -1,164 +0,0 @@
1
- /**
2
- * First User Admin Hook Utility
3
- *
4
- * Provides a Better Auth databaseHooks configuration that automatically
5
- * makes the first registered user an admin.
6
- *
7
- * @packageDocumentation
8
- */
9
-
10
- import type { BetterAuthOptions } from 'better-auth'
11
-
12
- export type FirstUserAdminOptions = {
13
- /**
14
- * Role to assign to the first user
15
- * @default 'admin'
16
- */
17
- adminRole?: string
18
-
19
- /**
20
- * Role to assign to subsequent users (if not already set)
21
- * @default 'user'
22
- */
23
- defaultRole?: string
24
-
25
- /**
26
- * Field name for the role field
27
- * @default 'role'
28
- */
29
- roleField?: string
30
- }
31
-
32
- /**
33
- * Creates Better Auth databaseHooks configuration that makes the first
34
- * registered user an admin.
35
- *
36
- * @example Basic usage
37
- * ```ts
38
- * import { betterAuth } from 'better-auth'
39
- * import { payloadAdapter } from '@delmaredigital/payload-better-auth/adapter'
40
- * import { firstUserAdminHooks } from '@delmaredigital/payload-better-auth'
41
- *
42
- * export const auth = betterAuth({
43
- * database: payloadAdapter({ payloadClient: payload }),
44
- * databaseHooks: firstUserAdminHooks(),
45
- * })
46
- * ```
47
- *
48
- * @example Custom roles
49
- * ```ts
50
- * export const auth = betterAuth({
51
- * database: payloadAdapter({ payloadClient: payload }),
52
- * databaseHooks: firstUserAdminHooks({
53
- * adminRole: 'super-admin',
54
- * defaultRole: 'member',
55
- * }),
56
- * })
57
- * ```
58
- *
59
- * @example Merging with other hooks
60
- * ```ts
61
- * export const auth = betterAuth({
62
- * database: payloadAdapter({ payloadClient: payload }),
63
- * databaseHooks: {
64
- * user: {
65
- * create: {
66
- * before: async (user, ctx) => {
67
- * // First apply first-user-admin logic
68
- * const result = await firstUserAdminHooks().user.create.before(user, ctx)
69
- * const userData = result?.data ?? user
70
- *
71
- * // Then apply your custom logic
72
- * return {
73
- * data: {
74
- * ...userData,
75
- * createdVia: 'custom-signup',
76
- * },
77
- * }
78
- * },
79
- * after: async (user) => {
80
- * // Your after-create logic
81
- * console.log('User created:', user.email)
82
- * },
83
- * },
84
- * },
85
- * },
86
- * })
87
- * ```
88
- */
89
- export function firstUserAdminHooks(
90
- options?: FirstUserAdminOptions
91
- ): NonNullable<BetterAuthOptions['databaseHooks']> {
92
- const {
93
- adminRole = 'admin',
94
- defaultRole = 'user',
95
- roleField = 'role',
96
- } = options ?? {}
97
-
98
- // Using explicit any for the context type because Better Auth's
99
- // GenericEndpointContext type is complex and includes [x: string]: any.
100
- // The runtime behavior is what matters here.
101
- const beforeHook = async (
102
- user: Record<string, unknown>,
103
- ctx: unknown
104
- ): Promise<{ data: Record<string, unknown> }> => {
105
- try {
106
- // Access the adapter from context
107
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
108
- const context = ctx as { context?: { adapter?: any } } | null
109
- const adapter = context?.context?.adapter
110
-
111
- if (!adapter?.count) {
112
- // Adapter not available, fall back to default role
113
- return {
114
- data: {
115
- ...user,
116
- [roleField]: user[roleField] ?? defaultRole,
117
- },
118
- }
119
- }
120
-
121
- const userCount = await adapter.count({
122
- model: 'user',
123
- where: [],
124
- })
125
-
126
- if (userCount === 0) {
127
- // First user becomes admin
128
- return {
129
- data: {
130
- ...user,
131
- [roleField]: adminRole,
132
- },
133
- }
134
- }
135
-
136
- // Subsequent users get default role if not already set
137
- return {
138
- data: {
139
- ...user,
140
- [roleField]: user[roleField] ?? defaultRole,
141
- },
142
- }
143
- } catch (error) {
144
- // On error, don't block user creation - just use provided or default role
145
- console.warn('[firstUserAdminHooks] Failed to check user count:', error)
146
- return {
147
- data: {
148
- ...user,
149
- [roleField]: user[roleField] ?? defaultRole,
150
- },
151
- }
152
- }
153
- }
154
-
155
- return {
156
- user: {
157
- create: {
158
- // Cast needed because Better Auth's hook types are complex
159
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
160
- before: beforeHook as any,
161
- },
162
- },
163
- }
164
- }
@@ -1,150 +0,0 @@
1
- /**
2
- * Auto-generate API key scopes from Payload collections.
3
- */
4
-
5
- import type { CollectionConfig } from 'payload'
6
- import type {
7
- ScopeDefinition,
8
- ApiKeyScopesConfig,
9
- AvailableScope,
10
- } from '../types/apiKey.js'
11
-
12
- /** Default collections to exclude from auto-generated scopes */
13
- const DEFAULT_EXCLUDED_COLLECTIONS = [
14
- 'sessions',
15
- 'verifications',
16
- 'accounts',
17
- 'twoFactors',
18
- 'apiKeys',
19
- ]
20
-
21
- /**
22
- * Capitalize the first letter of a string.
23
- */
24
- function capitalize(str: string): string {
25
- return str.charAt(0).toUpperCase() + str.slice(1)
26
- }
27
-
28
- /**
29
- * Convert slug to human-readable label.
30
- * e.g., 'blog-posts' -> 'Blog Posts'
31
- */
32
- function slugToLabel(slug: string): string {
33
- return slug
34
- .split('-')
35
- .map(capitalize)
36
- .join(' ')
37
- }
38
-
39
- /**
40
- * Generate scopes from Payload collections.
41
- * Creates {collection}:read, {collection}:write, {collection}:delete for each collection.
42
- */
43
- export function generateScopesFromCollections(
44
- collections: CollectionConfig[],
45
- excludeCollections: string[] = DEFAULT_EXCLUDED_COLLECTIONS
46
- ): Record<string, ScopeDefinition> {
47
- const scopes: Record<string, ScopeDefinition> = {}
48
-
49
- for (const collection of collections) {
50
- if (excludeCollections.includes(collection.slug)) continue
51
-
52
- const slug = collection.slug
53
- const singularLabel =
54
- (typeof collection.labels?.singular === 'string'
55
- ? collection.labels.singular
56
- : null) ?? slugToLabel(slug)
57
- const pluralLabel =
58
- (typeof collection.labels?.plural === 'string'
59
- ? collection.labels.plural
60
- : null) ?? slugToLabel(slug) + 's'
61
-
62
- scopes[`${slug}:read`] = {
63
- label: `Read ${pluralLabel}`,
64
- description: `View ${pluralLabel.toLowerCase()}`,
65
- permissions: { [slug]: ['read'] },
66
- }
67
-
68
- scopes[`${slug}:write`] = {
69
- label: `Write ${pluralLabel}`,
70
- description: `Create and edit ${pluralLabel.toLowerCase()}`,
71
- permissions: { [slug]: ['read', 'create', 'update'] },
72
- }
73
-
74
- scopes[`${slug}:delete`] = {
75
- label: `Delete ${pluralLabel}`,
76
- description: `Delete ${pluralLabel.toLowerCase()}`,
77
- permissions: { [slug]: ['delete'] },
78
- }
79
- }
80
-
81
- return scopes
82
- }
83
-
84
- /**
85
- * Build the final scopes configuration from plugin options and collections.
86
- * Handles merging custom scopes with auto-generated collection scopes.
87
- */
88
- export function buildAvailableScopes(
89
- collections: CollectionConfig[],
90
- config?: ApiKeyScopesConfig
91
- ): AvailableScope[] {
92
- const customScopes = config?.scopes ?? {}
93
- const hasCustomScopes = Object.keys(customScopes).length > 0
94
-
95
- // Determine if we should include collection scopes
96
- // Default: true when no custom scopes, false when custom scopes provided
97
- const includeCollectionScopes =
98
- config?.includeCollectionScopes ?? !hasCustomScopes
99
-
100
- const excludeCollections = config?.excludeCollections ?? DEFAULT_EXCLUDED_COLLECTIONS
101
-
102
- // Build the combined scopes object
103
- let allScopes: Record<string, ScopeDefinition> = {}
104
-
105
- // Add collection scopes if enabled
106
- if (includeCollectionScopes) {
107
- allScopes = generateScopesFromCollections(collections, excludeCollections)
108
- }
109
-
110
- // Add custom scopes (they override collection scopes with same ID)
111
- for (const [id, scope] of Object.entries(customScopes)) {
112
- allScopes[id] = scope
113
- }
114
-
115
- // Convert to array format for the client
116
- return Object.entries(allScopes).map(([id, scope]) => ({
117
- id,
118
- ...scope,
119
- }))
120
- }
121
-
122
- /**
123
- * Convert selected scopes to Better Auth permission format.
124
- * Used when creating an API key.
125
- */
126
- export function scopesToPermissions(
127
- selectedScopeIds: string[],
128
- availableScopes: AvailableScope[]
129
- ): Record<string, string[]> {
130
- const permissions: Record<string, string[]> = {}
131
-
132
- for (const scopeId of selectedScopeIds) {
133
- const scope = availableScopes.find((s) => s.id === scopeId)
134
- if (!scope) continue
135
-
136
- for (const [resource, actions] of Object.entries(scope.permissions)) {
137
- if (!permissions[resource]) {
138
- permissions[resource] = []
139
- }
140
- // Add unique actions
141
- for (const action of actions) {
142
- if (!permissions[resource].includes(action)) {
143
- permissions[resource].push(action)
144
- }
145
- }
146
- }
147
- }
148
-
149
- return permissions
150
- }
@@ -1,91 +0,0 @@
1
- /**
2
- * Server-side session utilities
3
- *
4
- * @packageDocumentation
5
- */
6
-
7
- import type { BasePayload } from 'payload'
8
- import type { PayloadWithAuth } from '../plugin/index.js'
9
-
10
- export type Session = {
11
- user: {
12
- id: string
13
- email: string
14
- name?: string
15
- image?: string
16
- [key: string]: unknown
17
- }
18
- session: {
19
- id: string
20
- expiresAt: Date
21
- [key: string]: unknown
22
- }
23
- }
24
-
25
- /**
26
- * Get the current session from headers.
27
- *
28
- * @example
29
- * ```ts
30
- * import { headers } from 'next/headers'
31
- * import { getServerSession } from '@delmare/payload-better-auth'
32
- *
33
- * export default async function Page() {
34
- * const headersList = await headers()
35
- * const session = await getServerSession(payload, headersList)
36
- *
37
- * if (!session) {
38
- * redirect('/login')
39
- * }
40
- *
41
- * return <div>Hello {session.user.name}</div>
42
- * }
43
- * ```
44
- */
45
- export async function getServerSession(
46
- payload: BasePayload,
47
- headers: Headers
48
- ): Promise<Session | null> {
49
- try {
50
- const payloadWithAuth = payload as PayloadWithAuth
51
-
52
- if (!payloadWithAuth.betterAuth) {
53
- console.error('[session] Better Auth not initialized')
54
- return null
55
- }
56
-
57
- const session = await payloadWithAuth.betterAuth.api.getSession({ headers })
58
- return session as Session | null
59
- } catch (error) {
60
- console.error('[session] Error getting session:', error)
61
- return null
62
- }
63
- }
64
-
65
- /**
66
- * Get the current user from the session.
67
- *
68
- * @example
69
- * ```ts
70
- * import { headers } from 'next/headers'
71
- * import { getServerUser } from '@delmare/payload-better-auth'
72
- *
73
- * export default async function Page() {
74
- * const headersList = await headers()
75
- * const user = await getServerUser(payload, headersList)
76
- *
77
- * if (!user) {
78
- * redirect('/login')
79
- * }
80
- *
81
- * return <div>Hello {user.name}</div>
82
- * }
83
- * ```
84
- */
85
- export async function getServerUser(
86
- payload: BasePayload,
87
- headers: Headers
88
- ): Promise<Session['user'] | null> {
89
- const session = await getServerSession(payload, headers)
90
- return session?.user ?? null
91
- }