payload-auth 1.0.0
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.
- package/LICENSE.md +17 -0
- package/dist/authjs/index.d.ts +1 -0
- package/dist/authjs/index.js +3 -0
- package/dist/authjs/index.js.map +1 -0
- package/dist/better-auth/adapter/dev/bin/run.d.ts +1 -0
- package/dist/better-auth/adapter/dev/bin/run.js +64 -0
- package/dist/better-auth/adapter/dev/bin/run.js.map +1 -0
- package/dist/better-auth/adapter/dev/bin/schema.d.ts +22 -0
- package/dist/better-auth/adapter/dev/bin/schema.js +160 -0
- package/dist/better-auth/adapter/dev/bin/schema.js.map +1 -0
- package/dist/better-auth/adapter/dev/collections.d.ts +3 -0
- package/dist/better-auth/adapter/dev/collections.js +212 -0
- package/dist/better-auth/adapter/dev/collections.js.map +1 -0
- package/dist/better-auth/adapter/dev/index.d.ts +3 -0
- package/dist/better-auth/adapter/dev/index.js +168 -0
- package/dist/better-auth/adapter/dev/index.js.map +1 -0
- package/dist/better-auth/adapter/generate-schema/generate-schema-builder.d.ts +23 -0
- package/dist/better-auth/adapter/generate-schema/generate-schema-builder.js +326 -0
- package/dist/better-auth/adapter/generate-schema/generate-schema-builder.js.map +1 -0
- package/dist/better-auth/adapter/generate-schema/get-payload-schema.d.ts +1 -0
- package/dist/better-auth/adapter/generate-schema/get-payload-schema.js +23 -0
- package/dist/better-auth/adapter/generate-schema/get-payload-schema.js.map +1 -0
- package/dist/better-auth/adapter/generate-schema/index.d.ts +4 -0
- package/dist/better-auth/adapter/generate-schema/index.js +19 -0
- package/dist/better-auth/adapter/generate-schema/index.js.map +1 -0
- package/dist/better-auth/adapter/generate-schema/utils.d.ts +2 -0
- package/dist/better-auth/adapter/generate-schema/utils.js +20 -0
- package/dist/better-auth/adapter/generate-schema/utils.js.map +1 -0
- package/dist/better-auth/adapter/index.d.ts +5 -0
- package/dist/better-auth/adapter/index.js +578 -0
- package/dist/better-auth/adapter/index.js.map +1 -0
- package/dist/better-auth/adapter/test/adapter.test.d.ts +1 -0
- package/dist/better-auth/adapter/test/adapter.test.js +181 -0
- package/dist/better-auth/adapter/test/adapter.test.js.map +1 -0
- package/dist/better-auth/adapter/test/better-auth-adapter-test.d.ts +7 -0
- package/dist/better-auth/adapter/test/better-auth-adapter-test.js +425 -0
- package/dist/better-auth/adapter/test/better-auth-adapter-test.js.map +1 -0
- package/dist/better-auth/adapter/test/schema.test.d.ts +1 -0
- package/dist/better-auth/adapter/test/schema.test.js +796 -0
- package/dist/better-auth/adapter/test/schema.test.js.map +1 -0
- package/dist/better-auth/adapter/test/test_payload1/schema.d.ts +23 -0
- package/dist/better-auth/adapter/test/test_payload1/schema.js +177 -0
- package/dist/better-auth/adapter/test/test_payload1/schema.js.map +1 -0
- package/dist/better-auth/adapter/test/test_payload2/schema.d.ts +23 -0
- package/dist/better-auth/adapter/test/test_payload2/schema.js +167 -0
- package/dist/better-auth/adapter/test/test_payload2/schema.js.map +1 -0
- package/dist/better-auth/adapter/test/test_payload3/schema.d.ts +23 -0
- package/dist/better-auth/adapter/test/test_payload3/schema.js +198 -0
- package/dist/better-auth/adapter/test/test_payload3/schema.js.map +1 -0
- package/dist/better-auth/adapter/transform/index.d.ts +16 -0
- package/dist/better-auth/adapter/transform/index.js +252 -0
- package/dist/better-auth/adapter/transform/index.js.map +1 -0
- package/dist/better-auth/adapter/types.d.ts +6 -0
- package/dist/better-auth/adapter/types.js +3 -0
- package/dist/better-auth/adapter/types.js.map +1 -0
- package/dist/better-auth/index.d.ts +6 -0
- package/dist/better-auth/index.js +8 -0
- package/dist/better-auth/index.js.map +1 -0
- package/dist/better-auth/plugin/collections/accounts/hooks/sync-password-to-user.d.ts +7 -0
- package/dist/better-auth/plugin/collections/accounts/hooks/sync-password-to-user.js +47 -0
- package/dist/better-auth/plugin/collections/accounts/hooks/sync-password-to-user.js.map +1 -0
- package/dist/better-auth/plugin/collections/users/endpoints/refresh-token.d.ts +6 -0
- package/dist/better-auth/plugin/collections/users/endpoints/refresh-token.js +106 -0
- package/dist/better-auth/plugin/collections/users/endpoints/refresh-token.js.map +1 -0
- package/dist/better-auth/plugin/collections/users/hooks/after-login.d.ts +11 -0
- package/dist/better-auth/plugin/collections/users/hooks/after-login.js +71 -0
- package/dist/better-auth/plugin/collections/users/hooks/after-login.js.map +1 -0
- package/dist/better-auth/plugin/collections/users/hooks/after-logout.d.ts +6 -0
- package/dist/better-auth/plugin/collections/users/hooks/after-logout.js +49 -0
- package/dist/better-auth/plugin/collections/users/hooks/after-logout.js.map +1 -0
- package/dist/better-auth/plugin/collections/users/hooks/before-login.d.ts +5 -0
- package/dist/better-auth/plugin/collections/users/hooks/before-login.js +18 -0
- package/dist/better-auth/plugin/collections/users/hooks/before-login.js.map +1 -0
- package/dist/better-auth/plugin/collections/users/hooks/clean-up-user-after-delete.d.ts +5 -0
- package/dist/better-auth/plugin/collections/users/hooks/clean-up-user-after-delete.js +33 -0
- package/dist/better-auth/plugin/collections/users/hooks/clean-up-user-after-delete.js.map +1 -0
- package/dist/better-auth/plugin/collections/users/hooks/on-verified-change.d.ts +2 -0
- package/dist/better-auth/plugin/collections/users/hooks/on-verified-change.js +14 -0
- package/dist/better-auth/plugin/collections/users/hooks/on-verified-change.js.map +1 -0
- package/dist/better-auth/plugin/collections/users/hooks/sync-account.d.ts +7 -0
- package/dist/better-auth/plugin/collections/users/hooks/sync-account.js +82 -0
- package/dist/better-auth/plugin/collections/users/hooks/sync-account.js.map +1 -0
- package/dist/better-auth/plugin/helpers/generate-verify-email-url.d.ts +29 -0
- package/dist/better-auth/plugin/helpers/generate-verify-email-url.js +46 -0
- package/dist/better-auth/plugin/helpers/generate-verify-email-url.js.map +1 -0
- package/dist/better-auth/plugin/helpers/get-ip.d.ts +2 -0
- package/dist/better-auth/plugin/helpers/get-ip.js +31 -0
- package/dist/better-auth/plugin/helpers/get-ip.js.map +1 -0
- package/dist/better-auth/plugin/helpers/index.d.ts +1 -0
- package/dist/better-auth/plugin/helpers/index.js +3 -0
- package/dist/better-auth/plugin/helpers/index.js.map +1 -0
- package/dist/better-auth/plugin/helpers/serialize-cookie.d.ts +104 -0
- package/dist/better-auth/plugin/helpers/serialize-cookie.js +186 -0
- package/dist/better-auth/plugin/helpers/serialize-cookie.js.map +1 -0
- package/dist/better-auth/plugin/index.d.ts +7 -0
- package/dist/better-auth/plugin/index.js +64 -0
- package/dist/better-auth/plugin/index.js.map +1 -0
- package/dist/better-auth/plugin/lib/auth-strategy.d.ts +8 -0
- package/dist/better-auth/plugin/lib/auth-strategy.js +48 -0
- package/dist/better-auth/plugin/lib/auth-strategy.js.map +1 -0
- package/dist/better-auth/plugin/lib/build-collection-configs.d.ts +11 -0
- package/dist/better-auth/plugin/lib/build-collection-configs.js +1558 -0
- package/dist/better-auth/plugin/lib/build-collection-configs.js.map +1 -0
- package/dist/better-auth/plugin/lib/config.d.ts +41 -0
- package/dist/better-auth/plugin/lib/config.js +43 -0
- package/dist/better-auth/plugin/lib/config.js.map +1 -0
- package/dist/better-auth/plugin/lib/ensure-password-set-before-create.d.ts +7 -0
- package/dist/better-auth/plugin/lib/ensure-password-set-before-create.js +24 -0
- package/dist/better-auth/plugin/lib/ensure-password-set-before-create.js.map +1 -0
- package/dist/better-auth/plugin/lib/get-payload-auth.d.ts +5 -0
- package/dist/better-auth/plugin/lib/get-payload-auth.js +9 -0
- package/dist/better-auth/plugin/lib/get-payload-auth.js.map +1 -0
- package/dist/better-auth/plugin/lib/get-required-collection-slugs.d.ts +9 -0
- package/dist/better-auth/plugin/lib/get-required-collection-slugs.js +52 -0
- package/dist/better-auth/plugin/lib/get-required-collection-slugs.js.map +1 -0
- package/dist/better-auth/plugin/lib/init-better-auth.d.ts +6 -0
- package/dist/better-auth/plugin/lib/init-better-auth.js +13 -0
- package/dist/better-auth/plugin/lib/init-better-auth.js.map +1 -0
- package/dist/better-auth/plugin/lib/password.d.ts +25 -0
- package/dist/better-auth/plugin/lib/password.js +63 -0
- package/dist/better-auth/plugin/lib/password.js.map +1 -0
- package/dist/better-auth/plugin/lib/payload-access.d.ts +14 -0
- package/dist/better-auth/plugin/lib/payload-access.js +64 -0
- package/dist/better-auth/plugin/lib/payload-access.js.map +1 -0
- package/dist/better-auth/plugin/lib/prepare-session-data.d.ts +21 -0
- package/dist/better-auth/plugin/lib/prepare-session-data.js +42 -0
- package/dist/better-auth/plugin/lib/prepare-session-data.js.map +1 -0
- package/dist/better-auth/plugin/lib/respect-save-to-jwt-fields-middleware.d.ts +15 -0
- package/dist/better-auth/plugin/lib/respect-save-to-jwt-fields-middleware.js +30 -0
- package/dist/better-auth/plugin/lib/respect-save-to-jwt-fields-middleware.js.map +1 -0
- package/dist/better-auth/plugin/lib/sanitize-auth-options.d.ts +5 -0
- package/dist/better-auth/plugin/lib/sanitize-auth-options.js +298 -0
- package/dist/better-auth/plugin/lib/sanitize-auth-options.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/admin-buttons.d.ts +5 -0
- package/dist/better-auth/plugin/payload/components/admin-buttons.js +222 -0
- package/dist/better-auth/plugin/payload/components/admin-buttons.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/login-redirect.d.ts +1 -0
- package/dist/better-auth/plugin/payload/components/login-redirect.js +6 -0
- package/dist/better-auth/plugin/payload/components/login-redirect.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/logo.d.ts +1 -0
- package/dist/better-auth/plugin/payload/components/logo.js +36 -0
- package/dist/better-auth/plugin/payload/components/logo.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/logout.d.ts +1 -0
- package/dist/better-auth/plugin/payload/components/logout.js +61 -0
- package/dist/better-auth/plugin/payload/components/logout.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/sign-in.d.ts +3 -0
- package/dist/better-auth/plugin/payload/components/sign-in.js +384 -0
- package/dist/better-auth/plugin/payload/components/sign-in.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/sign-up.d.ts +6 -0
- package/dist/better-auth/plugin/payload/components/sign-up.js +502 -0
- package/dist/better-auth/plugin/payload/components/sign-up.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/styles.css +73 -0
- package/dist/better-auth/plugin/payload/components/ui/button.d.ts +10 -0
- package/dist/better-auth/plugin/payload/components/ui/button.js +42 -0
- package/dist/better-auth/plugin/payload/components/ui/button.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/ui/card.d.ts +9 -0
- package/dist/better-auth/plugin/payload/components/ui/card.js +55 -0
- package/dist/better-auth/plugin/payload/components/ui/card.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/ui/checkbox.d.ts +4 -0
- package/dist/better-auth/plugin/payload/components/ui/checkbox.js +23 -0
- package/dist/better-auth/plugin/payload/components/ui/checkbox.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/ui/cn.d.ts +2 -0
- package/dist/better-auth/plugin/payload/components/ui/cn.js +7 -0
- package/dist/better-auth/plugin/payload/components/ui/cn.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/ui/input.d.ts +3 -0
- package/dist/better-auth/plugin/payload/components/ui/input.js +14 -0
- package/dist/better-auth/plugin/payload/components/ui/input.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/ui/label.d.ts +4 -0
- package/dist/better-auth/plugin/payload/components/ui/label.js +15 -0
- package/dist/better-auth/plugin/payload/components/ui/label.js.map +1 -0
- package/dist/better-auth/plugin/payload/components/ui/password-input.d.ts +2 -0
- package/dist/better-auth/plugin/payload/components/ui/password-input.js +55 -0
- package/dist/better-auth/plugin/payload/components/ui/password-input.js.map +1 -0
- package/dist/better-auth/plugin/payload/exports/client.d.ts +3 -0
- package/dist/better-auth/plugin/payload/exports/client.js +5 -0
- package/dist/better-auth/plugin/payload/exports/client.js.map +1 -0
- package/dist/better-auth/plugin/payload/exports/rsc.d.ts +5 -0
- package/dist/better-auth/plugin/payload/exports/rsc.js +7 -0
- package/dist/better-auth/plugin/payload/exports/rsc.js.map +1 -0
- package/dist/better-auth/plugin/payload/views/create-first-admin/index.d.ts +4 -0
- package/dist/better-auth/plugin/payload/views/create-first-admin/index.js +112 -0
- package/dist/better-auth/plugin/payload/views/create-first-admin/index.js.map +1 -0
- package/dist/better-auth/plugin/payload/views/login/index.d.ts +4 -0
- package/dist/better-auth/plugin/payload/views/login/index.js +78 -0
- package/dist/better-auth/plugin/payload/views/login/index.js.map +1 -0
- package/dist/better-auth/plugin/types.d.ts +224 -0
- package/dist/better-auth/plugin/types.js +3 -0
- package/dist/better-auth/plugin/types.js.map +1 -0
- package/dist/better-auth/types.d.ts +2 -0
- package/dist/better-auth/types.js +4 -0
- package/dist/better-auth/types.js.map +1 -0
- package/dist/clerk/index.d.ts +1 -0
- package/dist/clerk/index.js +3 -0
- package/dist/clerk/index.js.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/kinde/index.d.ts +1 -0
- package/dist/kinde/index.js +3 -0
- package/dist/kinde/index.js.map +1 -0
- package/package.json +87 -0
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* EXAMPLE COLLECTIONS FOR BETTER AUTH
|
|
3
|
+
*
|
|
4
|
+
* Below is what your Payload collections should look like.
|
|
5
|
+
* Please copy these to your actual collection configs.
|
|
6
|
+
* Make sure to add an authStrategy for the users collection if there is one.
|
|
7
|
+
*
|
|
8
|
+
* Example auth strategy:
|
|
9
|
+
* auth: {
|
|
10
|
+
* disableLocalStrategy: true,
|
|
11
|
+
* strategies: [
|
|
12
|
+
* betterAuthStrategy(),
|
|
13
|
+
* // Add other strategies as needed
|
|
14
|
+
* ],
|
|
15
|
+
* },
|
|
16
|
+
*/ const User = {
|
|
17
|
+
slug: 'user',
|
|
18
|
+
admin: {
|
|
19
|
+
useAsTitle: 'name'
|
|
20
|
+
},
|
|
21
|
+
fields: [
|
|
22
|
+
{
|
|
23
|
+
name: 'name',
|
|
24
|
+
type: 'text',
|
|
25
|
+
required: true
|
|
26
|
+
},
|
|
27
|
+
{
|
|
28
|
+
name: 'email',
|
|
29
|
+
type: 'email',
|
|
30
|
+
required: true,
|
|
31
|
+
unique: true
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'emailVerified',
|
|
35
|
+
type: 'checkbox',
|
|
36
|
+
required: true
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
name: 'image',
|
|
40
|
+
type: 'upload',
|
|
41
|
+
relationTo: 'media'
|
|
42
|
+
}
|
|
43
|
+
],
|
|
44
|
+
timestamps: true
|
|
45
|
+
};
|
|
46
|
+
const Session = {
|
|
47
|
+
slug: 'session',
|
|
48
|
+
admin: {
|
|
49
|
+
useAsTitle: 'expiresAt'
|
|
50
|
+
},
|
|
51
|
+
fields: [
|
|
52
|
+
{
|
|
53
|
+
name: 'expiresAt',
|
|
54
|
+
type: 'date',
|
|
55
|
+
required: true
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: 'token',
|
|
59
|
+
type: 'text',
|
|
60
|
+
required: true,
|
|
61
|
+
unique: true
|
|
62
|
+
},
|
|
63
|
+
{
|
|
64
|
+
name: 'ipAddress',
|
|
65
|
+
type: 'text'
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'userAgent',
|
|
69
|
+
type: 'text'
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
name: 'userId',
|
|
73
|
+
type: 'relationship',
|
|
74
|
+
required: true,
|
|
75
|
+
relationTo: 'user'
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
timestamps: true
|
|
79
|
+
};
|
|
80
|
+
const Account = {
|
|
81
|
+
slug: 'account',
|
|
82
|
+
admin: {
|
|
83
|
+
useAsTitle: 'accountId'
|
|
84
|
+
},
|
|
85
|
+
fields: [
|
|
86
|
+
{
|
|
87
|
+
name: 'accountId',
|
|
88
|
+
type: 'text',
|
|
89
|
+
required: true
|
|
90
|
+
},
|
|
91
|
+
{
|
|
92
|
+
name: 'providerId',
|
|
93
|
+
type: 'text',
|
|
94
|
+
required: true
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: 'userId',
|
|
98
|
+
type: 'relationship',
|
|
99
|
+
required: true,
|
|
100
|
+
relationTo: 'user'
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
name: 'accessToken',
|
|
104
|
+
type: 'text'
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: 'refreshToken',
|
|
108
|
+
type: 'text'
|
|
109
|
+
},
|
|
110
|
+
{
|
|
111
|
+
name: 'idToken',
|
|
112
|
+
type: 'text'
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
name: 'accessTokenExpiresAt',
|
|
116
|
+
type: 'date'
|
|
117
|
+
},
|
|
118
|
+
{
|
|
119
|
+
name: 'refreshTokenExpiresAt',
|
|
120
|
+
type: 'date'
|
|
121
|
+
},
|
|
122
|
+
{
|
|
123
|
+
name: 'scope',
|
|
124
|
+
type: 'text'
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: 'password',
|
|
128
|
+
type: 'text'
|
|
129
|
+
}
|
|
130
|
+
],
|
|
131
|
+
timestamps: true
|
|
132
|
+
};
|
|
133
|
+
const Verification = {
|
|
134
|
+
slug: 'verification',
|
|
135
|
+
admin: {
|
|
136
|
+
useAsTitle: 'identifier'
|
|
137
|
+
},
|
|
138
|
+
fields: [
|
|
139
|
+
{
|
|
140
|
+
name: 'identifier',
|
|
141
|
+
type: 'text',
|
|
142
|
+
required: true
|
|
143
|
+
},
|
|
144
|
+
{
|
|
145
|
+
name: 'value',
|
|
146
|
+
type: 'text',
|
|
147
|
+
required: true
|
|
148
|
+
},
|
|
149
|
+
{
|
|
150
|
+
name: 'expiresAt',
|
|
151
|
+
type: 'date',
|
|
152
|
+
required: true
|
|
153
|
+
}
|
|
154
|
+
],
|
|
155
|
+
timestamps: true
|
|
156
|
+
};
|
|
157
|
+
const Admin = {
|
|
158
|
+
slug: 'admin',
|
|
159
|
+
admin: {
|
|
160
|
+
useAsTitle: 'name'
|
|
161
|
+
},
|
|
162
|
+
fields: [
|
|
163
|
+
{
|
|
164
|
+
name: 'name',
|
|
165
|
+
type: 'text',
|
|
166
|
+
required: true
|
|
167
|
+
},
|
|
168
|
+
{
|
|
169
|
+
name: 'isAdmin',
|
|
170
|
+
type: 'checkbox',
|
|
171
|
+
required: true
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: 'status',
|
|
175
|
+
type: 'text'
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
name: 'date',
|
|
179
|
+
type: 'date'
|
|
180
|
+
},
|
|
181
|
+
{
|
|
182
|
+
name: 'number',
|
|
183
|
+
type: 'number'
|
|
184
|
+
},
|
|
185
|
+
{
|
|
186
|
+
name: 'str_array',
|
|
187
|
+
type: 'text'
|
|
188
|
+
},
|
|
189
|
+
{
|
|
190
|
+
name: 'num_array',
|
|
191
|
+
type: 'text'
|
|
192
|
+
}
|
|
193
|
+
],
|
|
194
|
+
timestamps: true
|
|
195
|
+
};
|
|
196
|
+
export { User, Session, Account, Verification, Admin };
|
|
197
|
+
|
|
198
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../src/adapter/test/test_payload3/schema.ts"],"sourcesContent":["/**\n * EXAMPLE COLLECTIONS FOR BETTER AUTH\n *\n * Below is what your Payload collections should look like.\n * Please copy these to your actual collection configs.\n * Make sure to add an authStrategy for the users collection if there is one.\n *\n * Example auth strategy:\n * auth: {\n * disableLocalStrategy: true,\n * strategies: [\n * betterAuthStrategy(),\n * // Add other strategies as needed\n * ],\n * },\n */\nimport type { CollectionConfig } from 'payload'\n\nconst User: CollectionConfig = {\n slug: 'user',\n admin: {\n useAsTitle: 'name',\n },\n fields: [\n {\n name: 'name',\n type: 'text',\n required: true,\n },\n {\n name: 'email',\n type: 'email',\n required: true,\n unique: true,\n },\n {\n name: 'emailVerified',\n type: 'checkbox',\n required: true,\n },\n {\n name: 'image',\n type: 'upload',\n relationTo: 'media',\n },\n ],\n timestamps: true,\n} as const\n\nconst Session: CollectionConfig = {\n slug: 'session',\n admin: {\n useAsTitle: 'expiresAt',\n },\n fields: [\n {\n name: 'expiresAt',\n type: 'date',\n required: true,\n },\n {\n name: 'token',\n type: 'text',\n required: true,\n unique: true,\n },\n {\n name: 'ipAddress',\n type: 'text',\n },\n {\n name: 'userAgent',\n type: 'text',\n },\n {\n name: 'userId',\n type: 'relationship',\n required: true,\n relationTo: 'user',\n },\n ],\n timestamps: true,\n} as const\n\nconst Account: CollectionConfig = {\n slug: 'account',\n admin: {\n useAsTitle: 'accountId',\n },\n fields: [\n {\n name: 'accountId',\n type: 'text',\n required: true,\n },\n {\n name: 'providerId',\n type: 'text',\n required: true,\n },\n {\n name: 'userId',\n type: 'relationship',\n required: true,\n relationTo: 'user',\n },\n {\n name: 'accessToken',\n type: 'text',\n },\n {\n name: 'refreshToken',\n type: 'text',\n },\n {\n name: 'idToken',\n type: 'text',\n },\n {\n name: 'accessTokenExpiresAt',\n type: 'date',\n },\n {\n name: 'refreshTokenExpiresAt',\n type: 'date',\n },\n {\n name: 'scope',\n type: 'text',\n },\n {\n name: 'password',\n type: 'text',\n },\n ],\n timestamps: true,\n} as const\n\nconst Verification: CollectionConfig = {\n slug: 'verification',\n admin: {\n useAsTitle: 'identifier',\n },\n fields: [\n {\n name: 'identifier',\n type: 'text',\n required: true,\n },\n {\n name: 'value',\n type: 'text',\n required: true,\n },\n {\n name: 'expiresAt',\n type: 'date',\n required: true,\n },\n ],\n timestamps: true,\n} as const\n\nconst Admin: CollectionConfig = {\n slug: 'admin',\n admin: {\n useAsTitle: 'name',\n },\n fields: [\n {\n name: 'name',\n type: 'text',\n required: true,\n },\n {\n name: 'isAdmin',\n type: 'checkbox',\n required: true,\n },\n {\n name: 'status',\n type: 'text',\n },\n {\n name: 'date',\n type: 'date',\n },\n {\n name: 'number',\n type: 'number',\n },\n {\n name: 'str_array',\n type: 'text',\n },\n {\n name: 'num_array',\n type: 'text',\n },\n ],\n timestamps: true,\n} as const\n\nexport { User, Session, Account, Verification, Admin }\n"],"names":["User","slug","admin","useAsTitle","fields","name","type","required","unique","relationTo","timestamps","Session","Account","Verification","Admin"],"mappings":"AAAA;;;;;;;;;;;;;;;CAeC,GAGD,MAAMA,OAAyB;IAC7BC,MAAM;IACNC,OAAO;QACLC,YAAY;IACd;IACAC,QAAQ;QACN;YACEC,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNC,UAAU;YACVC,QAAQ;QACV;QACA;YACEH,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNG,YAAY;QACd;KACD;IACDC,YAAY;AACd;AAEA,MAAMC,UAA4B;IAChCV,MAAM;IACNC,OAAO;QACLC,YAAY;IACd;IACAC,QAAQ;QACN;YACEC,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNC,UAAU;YACVC,QAAQ;QACV;QACA;YACEH,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;YACNC,UAAU;YACVE,YAAY;QACd;KACD;IACDC,YAAY;AACd;AAEA,MAAME,UAA4B;IAChCX,MAAM;IACNC,OAAO;QACLC,YAAY;IACd;IACAC,QAAQ;QACN;YACEC,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNC,UAAU;YACVE,YAAY;QACd;QACA;YACEJ,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;KACD;IACDI,YAAY;AACd;AAEA,MAAMG,eAAiC;IACrCZ,MAAM;IACNC,OAAO;QACLC,YAAY;IACd;IACAC,QAAQ;QACN;YACEC,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;KACD;IACDG,YAAY;AACd;AAEA,MAAMI,QAA0B;IAC9Bb,MAAM;IACNC,OAAO;QACLC,YAAY;IACd;IACAC,QAAQ;QACN;YACEC,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;YACNC,UAAU;QACZ;QACA;YACEF,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;QACA;YACED,MAAM;YACNC,MAAM;QACR;KACD;IACDI,YAAY;AACd;AAEA,SAASV,IAAI,EAAEW,OAAO,EAAEC,OAAO,EAAEC,YAAY,EAAEC,KAAK,GAAE"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { BetterAuthOptions, Where } from 'better-auth';
|
|
2
|
+
import type { CollectionSlug, Where as PayloadWhere } from 'payload';
|
|
3
|
+
export declare const createTransform: (options: BetterAuthOptions, enableDebugLogs: boolean) => {
|
|
4
|
+
getField: (model: string, field: string) => any;
|
|
5
|
+
getModelName: (model: string) => CollectionSlug;
|
|
6
|
+
singleIdQuery: (where: PayloadWhere) => string | number | null;
|
|
7
|
+
multipleIdsQuery: (where: PayloadWhere) => (string | number)[] | null;
|
|
8
|
+
transformInput: (data: Record<string, any>, model: string, action: "create" | "update") => Record<string, any>;
|
|
9
|
+
transformOutput: <T extends Record<string, any> | undefined>(doc: T) => T;
|
|
10
|
+
convertWhereClause: (model: string, where?: Where[]) => PayloadWhere;
|
|
11
|
+
convertSelect: (model: string, select?: string[]) => {} | undefined;
|
|
12
|
+
convertSort: (model: string, sortBy?: {
|
|
13
|
+
field: string;
|
|
14
|
+
direction: "asc" | "desc";
|
|
15
|
+
}) => string | undefined;
|
|
16
|
+
};
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
import { BetterAuthError } from 'better-auth';
|
|
2
|
+
import { getAuthTables } from 'better-auth/db';
|
|
3
|
+
export const createTransform = (options, enableDebugLogs)=>{
|
|
4
|
+
const schema = getAuthTables(options);
|
|
5
|
+
function debugLog(message) {
|
|
6
|
+
if (enableDebugLogs) {
|
|
7
|
+
console.log(`[payload-db-adapter]`, ...message);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function getField(model, field) {
|
|
11
|
+
if (field === 'id') {
|
|
12
|
+
return field;
|
|
13
|
+
}
|
|
14
|
+
const f = schema[model]?.fields[field];
|
|
15
|
+
const fieldName = f?.fieldName || field;
|
|
16
|
+
debugLog([
|
|
17
|
+
'getField: ',
|
|
18
|
+
model,
|
|
19
|
+
fieldName
|
|
20
|
+
]);
|
|
21
|
+
return fieldName;
|
|
22
|
+
}
|
|
23
|
+
function getModelName(model) {
|
|
24
|
+
const collection = schema[model]?.modelName || model;
|
|
25
|
+
if (!collection) {
|
|
26
|
+
throw new BetterAuthError(`Model ${model} does not exist in the database.`);
|
|
27
|
+
}
|
|
28
|
+
return collection;
|
|
29
|
+
}
|
|
30
|
+
function singleIdQuery(where) {
|
|
31
|
+
if (!where || 'and' in where || 'or' in where) return null;
|
|
32
|
+
// For a single id query like { id: { equals: 15 } }
|
|
33
|
+
// First, check if there's an id field in the where clause
|
|
34
|
+
if ('id' in where || '_id' in where) {
|
|
35
|
+
const idField = 'id' in where ? 'id' : '_id';
|
|
36
|
+
const condition = where[idField];
|
|
37
|
+
// Check if condition is an object with equals operator
|
|
38
|
+
if (condition && typeof condition === 'object' && !Array.isArray(condition) && 'equals' in condition) {
|
|
39
|
+
const value = condition.equals;
|
|
40
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// Check for contains operator with single value
|
|
45
|
+
if (condition && typeof condition === 'object' && !Array.isArray(condition) && 'contains' in condition && Array.isArray(condition.contains) && condition.contains.length === 1) {
|
|
46
|
+
const value = condition.contains[0];
|
|
47
|
+
if (typeof value === 'string' || typeof value === 'number') {
|
|
48
|
+
return value;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
function multipleIdsQuery(where) {
|
|
55
|
+
if (!where || 'and' in where || 'or' in where) return null;
|
|
56
|
+
if ('id' in where || '_id' in where) {
|
|
57
|
+
const idField = 'id' in where ? 'id' : '_id';
|
|
58
|
+
const condition = where[idField];
|
|
59
|
+
// Check if this is an 'in' operator with id field and array of values
|
|
60
|
+
if (condition && typeof condition === 'object' && !Array.isArray(condition) && 'in' in condition && Array.isArray(condition.in) && condition.in.length > 1 && condition.in.every((id)=>typeof id === 'string' || typeof id === 'number')) {
|
|
61
|
+
return condition.in;
|
|
62
|
+
}
|
|
63
|
+
// Also check for contains operator with array of IDs
|
|
64
|
+
if (condition && typeof condition === 'object' && !Array.isArray(condition) && 'contains' in condition && Array.isArray(condition.contains) && condition.contains.length > 1 && condition.contains.every((id)=>typeof id === 'string' || typeof id === 'number')) {
|
|
65
|
+
return condition.contains;
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
function transformInput(data, model, action) {
|
|
71
|
+
const transformedData = {};
|
|
72
|
+
const schemaFields = schema[model].fields;
|
|
73
|
+
for(const dataField in data){
|
|
74
|
+
if (data[dataField] === undefined && action === 'update') {
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
const updatedFieldName = schemaFields[dataField]?.fieldName;
|
|
78
|
+
if (updatedFieldName) {
|
|
79
|
+
if (schemaFields[dataField].type === 'string' && typeof data[dataField] === 'number' && updatedFieldName.endsWith('Id')) {
|
|
80
|
+
debugLog([
|
|
81
|
+
'Incoming data is typeof number but stored as typeof string',
|
|
82
|
+
dataField,
|
|
83
|
+
data[dataField].toString()
|
|
84
|
+
]);
|
|
85
|
+
transformedData[updatedFieldName] = data[dataField].toString();
|
|
86
|
+
} else {
|
|
87
|
+
transformedData[updatedFieldName] = data[dataField];
|
|
88
|
+
}
|
|
89
|
+
} else {
|
|
90
|
+
transformedData[dataField] = data[dataField];
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
return transformedData;
|
|
94
|
+
}
|
|
95
|
+
function transformOutput(doc) {
|
|
96
|
+
if (!doc || typeof doc !== 'object') return doc;
|
|
97
|
+
const result = {
|
|
98
|
+
...doc
|
|
99
|
+
};
|
|
100
|
+
// Scan for relationship fields that contain objects with IDs
|
|
101
|
+
Object.entries(doc).forEach(([key, value])=>{
|
|
102
|
+
// If the value is an object with an id property, it's likely a relationship
|
|
103
|
+
if (value && typeof value === 'object' && 'id' in value) {
|
|
104
|
+
// Create a new field with Id suffix containing just the ID
|
|
105
|
+
const newKey = `${key}Id`;
|
|
106
|
+
result[newKey] = value.id;
|
|
107
|
+
//also remove the object and just keep the id
|
|
108
|
+
result[key] = value.id;
|
|
109
|
+
// Keep the original value as well for backward compatibility
|
|
110
|
+
} else if (Array.isArray(value)) {
|
|
111
|
+
// Handle arrays of relationships
|
|
112
|
+
if (value.length > 0 && typeof value[0] === 'object' && 'id' in value[0]) {
|
|
113
|
+
const newKey = `${key}Ids`;
|
|
114
|
+
result[newKey] = value.map((item)=>item.id);
|
|
115
|
+
//also remove the object and just keep the id
|
|
116
|
+
result[key] = value.map((item)=>item.id);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
});
|
|
120
|
+
// Scan for date fields and convert them to Date objects
|
|
121
|
+
Object.entries(result).forEach(([key, value])=>{
|
|
122
|
+
// Check if the field is a date string (ISO format)
|
|
123
|
+
if (typeof value === 'string' && /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(\.\d{3})?Z$/.test(value)) {
|
|
124
|
+
result[key] = new Date(value);
|
|
125
|
+
} else if (// Also check for date-like field names
|
|
126
|
+
(key.endsWith('At') || key.endsWith('Date') || key === 'date') && typeof value === 'string' && !isNaN(Date.parse(value))) {
|
|
127
|
+
result[key] = new Date(value);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
return result;
|
|
131
|
+
}
|
|
132
|
+
function operatorToPayload(operator, value) {
|
|
133
|
+
switch(operator){
|
|
134
|
+
case 'eq':
|
|
135
|
+
return {
|
|
136
|
+
equals: value
|
|
137
|
+
};
|
|
138
|
+
case 'ne':
|
|
139
|
+
return {
|
|
140
|
+
not_equals: value
|
|
141
|
+
};
|
|
142
|
+
case 'gt':
|
|
143
|
+
return {
|
|
144
|
+
greater_than: value
|
|
145
|
+
};
|
|
146
|
+
case 'gte':
|
|
147
|
+
return {
|
|
148
|
+
greater_than_equal: value
|
|
149
|
+
};
|
|
150
|
+
case 'lt':
|
|
151
|
+
return {
|
|
152
|
+
less_than: value
|
|
153
|
+
};
|
|
154
|
+
case 'lte':
|
|
155
|
+
return {
|
|
156
|
+
less_than_equal: value
|
|
157
|
+
};
|
|
158
|
+
case 'contains':
|
|
159
|
+
return {
|
|
160
|
+
contains: value
|
|
161
|
+
};
|
|
162
|
+
case 'in':
|
|
163
|
+
return {
|
|
164
|
+
in: value
|
|
165
|
+
};
|
|
166
|
+
case 'starts_with':
|
|
167
|
+
return {
|
|
168
|
+
like: `${value}%`
|
|
169
|
+
};
|
|
170
|
+
case 'ends_with':
|
|
171
|
+
return {
|
|
172
|
+
like: `%${value}`
|
|
173
|
+
};
|
|
174
|
+
default:
|
|
175
|
+
return {
|
|
176
|
+
equals: value
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
function convertWhereValue(value, field) {
|
|
181
|
+
if (field === 'id' || field === '_id') {
|
|
182
|
+
if (typeof value === 'object') {
|
|
183
|
+
return value.id;
|
|
184
|
+
}
|
|
185
|
+
return value;
|
|
186
|
+
}
|
|
187
|
+
return value;
|
|
188
|
+
}
|
|
189
|
+
function convertWhereClause(model, where) {
|
|
190
|
+
if (!where) return {};
|
|
191
|
+
if (where.length === 1) {
|
|
192
|
+
const w = where[0];
|
|
193
|
+
if (!w) {
|
|
194
|
+
return {};
|
|
195
|
+
}
|
|
196
|
+
const field = getField(model, w.field);
|
|
197
|
+
const value = convertWhereValue(w.value, field);
|
|
198
|
+
const res = {
|
|
199
|
+
[field]: operatorToPayload(w.operator ?? '', value)
|
|
200
|
+
};
|
|
201
|
+
return res;
|
|
202
|
+
}
|
|
203
|
+
const and = where.filter((w)=>w.connector === 'AND' || !w.connector);
|
|
204
|
+
const or = where.filter((w)=>w.connector === 'OR');
|
|
205
|
+
const andClause = and.map((w)=>{
|
|
206
|
+
const field = getField(model, w.field);
|
|
207
|
+
const value = convertWhereValue(w.value, field);
|
|
208
|
+
return {
|
|
209
|
+
[field]: operatorToPayload(w.operator ?? '', value)
|
|
210
|
+
};
|
|
211
|
+
});
|
|
212
|
+
const orClause = or.map((w)=>{
|
|
213
|
+
const field = getField(model, w.field);
|
|
214
|
+
const value = convertWhereValue(w.value, field);
|
|
215
|
+
return {
|
|
216
|
+
[field]: operatorToPayload(w.operator ?? '', value)
|
|
217
|
+
};
|
|
218
|
+
});
|
|
219
|
+
return {
|
|
220
|
+
...andClause.length ? {
|
|
221
|
+
AND: andClause
|
|
222
|
+
} : {},
|
|
223
|
+
...orClause.length ? {
|
|
224
|
+
OR: orClause
|
|
225
|
+
} : {}
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function convertSelect(model, select) {
|
|
229
|
+
if (!select || select.length === 0) return undefined;
|
|
230
|
+
return select.reduce((acc, field)=>({
|
|
231
|
+
...acc,
|
|
232
|
+
[getField(model, field)]: true
|
|
233
|
+
}), {});
|
|
234
|
+
}
|
|
235
|
+
function convertSort(model, sortBy) {
|
|
236
|
+
if (!sortBy) return undefined;
|
|
237
|
+
return `${sortBy.direction === 'desc' ? '-' : ''}${getField(model, sortBy.field)}`;
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
getField,
|
|
241
|
+
getModelName,
|
|
242
|
+
singleIdQuery,
|
|
243
|
+
multipleIdsQuery,
|
|
244
|
+
transformInput,
|
|
245
|
+
transformOutput,
|
|
246
|
+
convertWhereClause,
|
|
247
|
+
convertSelect,
|
|
248
|
+
convertSort
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../src/adapter/transform/index.ts"],"sourcesContent":["import { BetterAuthError } from 'better-auth'\nimport { getAuthTables } from 'better-auth/db'\nimport type { BetterAuthOptions, Where } from 'better-auth'\nimport type { CollectionSlug, Where as PayloadWhere } from 'payload'\n\nexport const createTransform = (options: BetterAuthOptions, enableDebugLogs: boolean) => {\n const schema = getAuthTables(options)\n\n function debugLog(message: any[]) {\n if (enableDebugLogs) {\n console.log(`[payload-db-adapter]`, ...message)\n }\n }\n\n function getField(model: string, field: string) {\n if (field === 'id') {\n return field\n }\n const f = (schema as Record<string, any>)[model]?.fields[field]\n const fieldName = f?.fieldName || field\n debugLog(['getField: ', model, fieldName])\n return fieldName\n }\n\n function getModelName(model: string): CollectionSlug {\n const collection = (schema as Record<string, any>)[model]?.modelName || model\n if (!collection) {\n throw new BetterAuthError(`Model ${model} does not exist in the database.`)\n }\n return collection as CollectionSlug\n }\n\n function singleIdQuery(where: PayloadWhere) {\n if (!where || 'and' in where || 'or' in where) return null\n\n // For a single id query like { id: { equals: 15 } }\n // First, check if there's an id field in the where clause\n if ('id' in where || '_id' in where) {\n const idField = 'id' in where ? 'id' : '_id'\n const condition = where[idField]\n\n // Check if condition is an object with equals operator\n if (\n condition &&\n typeof condition === 'object' &&\n !Array.isArray(condition) &&\n 'equals' in condition\n ) {\n const value = condition.equals\n if (typeof value === 'string' || typeof value === 'number') {\n return value\n }\n }\n\n // Check for contains operator with single value\n if (\n condition &&\n typeof condition === 'object' &&\n !Array.isArray(condition) &&\n 'contains' in condition &&\n Array.isArray(condition.contains) &&\n condition.contains.length === 1\n ) {\n const value = condition.contains[0]\n if (typeof value === 'string' || typeof value === 'number') {\n return value\n }\n }\n }\n\n return null\n }\n\n function multipleIdsQuery(where: PayloadWhere) {\n if (!where || 'and' in where || 'or' in where) return null\n if ('id' in where || '_id' in where) {\n const idField = 'id' in where ? 'id' : '_id'\n const condition = where[idField]\n\n // Check if this is an 'in' operator with id field and array of values\n if (\n condition &&\n typeof condition === 'object' &&\n !Array.isArray(condition) &&\n 'in' in condition &&\n Array.isArray(condition.in) &&\n condition.in.length > 1 &&\n condition.in.every((id: unknown) => typeof id === 'string' || typeof id === 'number')\n ) {\n return condition.in as (number | string)[]\n }\n\n // Also check for contains operator with array of IDs\n if (\n condition &&\n typeof condition === 'object' &&\n !Array.isArray(condition) &&\n 'contains' in condition &&\n Array.isArray(condition.contains) &&\n condition.contains.length > 1 &&\n condition.contains.every((id: unknown) => typeof id === 'string' || typeof id === 'number')\n ) {\n return condition.contains as (number | string)[]\n }\n }\n\n return null\n }\n\n function transformInput(data: Record<string, any>, model: string, action: 'create' | 'update') {\n const transformedData: Record<string, any> = {}\n const schemaFields = (schema as Record<string, any>)[model].fields\n for (const dataField in data) {\n if (data[dataField] === undefined && action === 'update') {\n continue\n }\n const updatedFieldName = schemaFields[dataField]?.fieldName\n\n if (updatedFieldName) {\n if (\n schemaFields[dataField].type === 'string' &&\n typeof data[dataField] === 'number' &&\n updatedFieldName.endsWith('Id')\n ) {\n debugLog([\n 'Incoming data is typeof number but stored as typeof string',\n dataField,\n data[dataField].toString(),\n ])\n transformedData[updatedFieldName] = data[dataField].toString()\n } else {\n transformedData[updatedFieldName] = data[dataField]\n }\n } else {\n transformedData[dataField] = data[dataField]\n }\n }\n\n return transformedData\n }\n\n function transformOutput<T extends Record<string, any> | undefined>(doc: T): T {\n if (!doc || typeof doc !== 'object') return doc\n\n const result = { ...doc } as any\n\n // Scan for relationship fields that contain objects with IDs\n Object.entries(doc).forEach(([key, value]) => {\n // If the value is an object with an id property, it's likely a relationship\n if (value && typeof value === 'object' && 'id' in value) {\n // Create a new field with Id suffix containing just the ID\n const newKey = `${key}Id`\n result[newKey] = value.id\n\n //also remove the object and just keep the id\n result[key] = value.id\n\n // Keep the original value as well for backward compatibility\n } else if (Array.isArray(value)) {\n // Handle arrays of relationships\n if (value.length > 0 && typeof value[0] === 'object' && 'id' in value[0]) {\n const newKey = `${key}Ids`\n result[newKey] = value.map((item) => item.id)\n\n //also remove the object and just keep the id\n result[key] = value.map((item) => item.id)\n }\n }\n })\n\n // Scan for date fields and convert them to Date objects\n Object.entries(result).forEach(([key, value]) => {\n // Check if the field is a date string (ISO format)\n if (\n typeof value === 'string' &&\n /^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(\\.\\d{3})?Z$/.test(value)\n ) {\n result[key] = new Date(value)\n } else if (\n // Also check for date-like field names\n (key.endsWith('At') || key.endsWith('Date') || key === 'date') &&\n typeof value === 'string' &&\n !isNaN(Date.parse(value))\n ) {\n result[key] = new Date(value)\n }\n })\n\n return result as T\n }\n\n function operatorToPayload(operator: string, value: any) {\n switch (operator) {\n case 'eq':\n return { equals: value }\n case 'ne':\n return { not_equals: value }\n case 'gt':\n return { greater_than: value }\n case 'gte':\n return { greater_than_equal: value }\n case 'lt':\n return { less_than: value }\n case 'lte':\n return { less_than_equal: value }\n case 'contains':\n return { contains: value }\n case 'in':\n return { in: value }\n case 'starts_with':\n return { like: `${value}%` }\n case 'ends_with':\n return { like: `%${value}` }\n default:\n return { equals: value }\n }\n }\n\n function convertWhereValue(value: any, field: string) {\n if (field === 'id' || field === '_id') {\n if (typeof value === 'object') {\n return value.id\n }\n return value\n }\n return value\n }\n\n function convertWhereClause(model: string, where?: Where[]): PayloadWhere {\n if (!where) return {}\n if (where.length === 1) {\n const w = where[0]\n if (!w) {\n return {}\n }\n\n const field = getField(model, w.field)\n const value = convertWhereValue(w.value, field)\n\n const res = {\n [field]: operatorToPayload(w.operator ?? '', value),\n }\n\n return res\n }\n const and = where.filter((w) => w.connector === 'AND' || !w.connector)\n const or = where.filter((w) => w.connector === 'OR')\n const andClause = and.map((w) => {\n const field = getField(model, w.field)\n const value = convertWhereValue(w.value, field)\n return {\n [field]: operatorToPayload(w.operator ?? '', value),\n }\n })\n const orClause = or.map((w) => {\n const field = getField(model, w.field)\n const value = convertWhereValue(w.value, field)\n return {\n [field]: operatorToPayload(w.operator ?? '', value),\n }\n })\n\n return {\n ...(andClause.length ? { AND: andClause } : {}),\n ...(orClause.length ? { OR: orClause } : {}),\n }\n }\n\n function convertSelect(model: string, select?: string[]) {\n if (!select || select.length === 0) return undefined\n return select.reduce((acc, field) => ({ ...acc, [getField(model, field)]: true }), {})\n }\n\n function convertSort(model: string, sortBy?: { field: string; direction: 'asc' | 'desc' }) {\n if (!sortBy) return undefined\n return `${sortBy.direction === 'desc' ? '-' : ''}${getField(model, sortBy.field)}`\n }\n\n return {\n getField,\n getModelName,\n singleIdQuery,\n multipleIdsQuery,\n transformInput,\n transformOutput,\n convertWhereClause,\n convertSelect,\n convertSort,\n }\n}\n"],"names":["BetterAuthError","getAuthTables","createTransform","options","enableDebugLogs","schema","debugLog","message","console","log","getField","model","field","f","fields","fieldName","getModelName","collection","modelName","singleIdQuery","where","idField","condition","Array","isArray","value","equals","contains","length","multipleIdsQuery","in","every","id","transformInput","data","action","transformedData","schemaFields","dataField","undefined","updatedFieldName","type","endsWith","toString","transformOutput","doc","result","Object","entries","forEach","key","newKey","map","item","test","Date","isNaN","parse","operatorToPayload","operator","not_equals","greater_than","greater_than_equal","less_than","less_than_equal","like","convertWhereValue","convertWhereClause","w","res","and","filter","connector","or","andClause","orClause","AND","OR","convertSelect","select","reduce","acc","convertSort","sortBy","direction"],"mappings":"AAAA,SAASA,eAAe,QAAQ,cAAa;AAC7C,SAASC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AE3C,KAAK,EAAEb;YACzC,OAAO;gBACL,CAACA,MAAM,EAAE8C,kBAAkBU,EAAET,QAAQ,IAAI,IAAIlC;YAC/C;QACF;QAEA,OAAO;YACL,GAAIiD,UAAU9C,MAAM,GAAG;gBAAEgD,KAAKF;YAAU,IAAI,CAAC,CAAC;YAC9C,GAAIC,SAAS/C,MAAM,GAAG;gBAAEiD,IAAIF;YAAS,IAAI,CAAC,CAAC;QAC7C;IACF;IAEA,SAASG,cAAcnE,KAAa,EAAEoE,MAAiB;QACrD,IAAI,CAACA,UAAUA,OAAOnD,MAAM,KAAK,GAAG,OAAOW;QAC3C,OAAOwC,OAAOC,MAAM,CAAC,CAACC,KAAKrE,QAAW,CAAA;gBAAE,GAAGqE,GAAG;gBAAE,CAACvE,SAASC,OAAOC,OAAO,EAAE;YAAK,CAAA,GAAI,CAAC;IACtF;IAEA,SAASsE,YAAYvE,KAAa,EAAEwE,MAAqD;QACvF,IAAI,CAACA,QAAQ,OAAO5C;QACpB,OAAO,GAAG4C,OAAOC,SAAS,KAAK,SAAS,MAAM,KAAK1E,SAASC,OAAOwE,OAAOvE,KAAK,GAAG;IACpF;IAEA,OAAO;QACLF;QACAM;QACAG;QACAU;QACAI;QACAW;QACAuB;QACAW;QACAI;IACF;AACF,EAAC"}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { BasePayload } from "payload";
|
|
2
|
+
import type { AdapterInstance } from "better-auth";
|
|
3
|
+
export type PayloadAdapterOptions = {
|
|
4
|
+
enableDebugLogs?: boolean;
|
|
5
|
+
};
|
|
6
|
+
export type PayloadAdapter = (payloadClient: BasePayload | Promise<BasePayload> | (() => Promise<BasePayload>), config?: PayloadAdapterOptions) => AdapterInstance;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/adapter/types.ts"],"sourcesContent":["import type { BasePayload } from \"payload\";\nimport type { AdapterInstance } from \"better-auth\";\n\nexport type PayloadAdapterOptions = {\n enableDebugLogs?: boolean;\n};\n\nexport type PayloadAdapter = (\n payloadClient: BasePayload | Promise<BasePayload> | (() => Promise<BasePayload>),\n config?: PayloadAdapterOptions\n) => AdapterInstance;\n"],"names":[],"mappings":"AAOA,WAGqB"}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './adapter';
|
|
2
|
+
export * from './plugin';
|
|
3
|
+
export * from './types';
|
|
4
|
+
import { payloadAdapter } from './adapter';
|
|
5
|
+
import { payloadBetterAuth } from './plugin';
|
|
6
|
+
export { payloadAdapter, payloadBetterAuth as betterAuthPlugin };
|
|
7
|
+
|
|
8
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["export * from './adapter'\nexport * from './plugin'\nexport * from './types'\nimport { payloadAdapter } from './adapter'\nimport { payloadBetterAuth } from './plugin'\n\nexport { payloadAdapter, payloadBetterAuth as betterAuthPlugin }\n"],"names":["payloadAdapter","payloadBetterAuth","betterAuthPlugin"],"mappings":"AAAA,cAAc,YAAW;AACzB,cAAc,WAAU;AACxB,cAAc,UAAS;AACvB,SAASA,cAAc,QAAQ,YAAW;AAC1C,SAASC,iBAAiB,QAAQ,WAAU;AAE5C,SAASD,cAAc,EAAEC,qBAAqBC,gBAAgB,GAAE"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { CollectionAfterChangeHook } from 'payload';
|
|
2
|
+
type SyncPasswordToUserOptions = {
|
|
3
|
+
userSlug: string;
|
|
4
|
+
accountSlug: string;
|
|
5
|
+
};
|
|
6
|
+
export declare const getSyncPasswordToUserHook: (options: SyncPasswordToUserOptions) => CollectionAfterChangeHook;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
export const getSyncPasswordToUserHook = (options)=>{
|
|
2
|
+
const hook = async ({ doc, req, operation, context })=>{
|
|
3
|
+
if (context?.syncAccountHook) return doc;
|
|
4
|
+
if (operation !== 'create' && operation !== 'update') {
|
|
5
|
+
return doc;
|
|
6
|
+
}
|
|
7
|
+
const userField = req.payload.betterAuth.options.account?.fields?.userId || 'userId';
|
|
8
|
+
if (!doc[userField]) {
|
|
9
|
+
return doc;
|
|
10
|
+
}
|
|
11
|
+
const account = await req.payload.findByID({
|
|
12
|
+
collection: options.accountSlug,
|
|
13
|
+
id: doc.id,
|
|
14
|
+
depth: 0,
|
|
15
|
+
req,
|
|
16
|
+
showHiddenFields: true
|
|
17
|
+
});
|
|
18
|
+
if (!account || !account.password) {
|
|
19
|
+
return doc;
|
|
20
|
+
}
|
|
21
|
+
const [salt, hash] = account.password.split(':');
|
|
22
|
+
if (!salt || !hash) {
|
|
23
|
+
return doc;
|
|
24
|
+
}
|
|
25
|
+
const userId = typeof doc[userField] === 'string' ? doc[userField] : doc[userField]?.id;
|
|
26
|
+
try {
|
|
27
|
+
await req.payload.update({
|
|
28
|
+
collection: options.userSlug,
|
|
29
|
+
id: userId,
|
|
30
|
+
data: {
|
|
31
|
+
salt,
|
|
32
|
+
hash
|
|
33
|
+
},
|
|
34
|
+
req,
|
|
35
|
+
context: {
|
|
36
|
+
syncPasswordToUser: true
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
console.error('Failed to sync password to user:', error);
|
|
41
|
+
}
|
|
42
|
+
return doc;
|
|
43
|
+
};
|
|
44
|
+
return hook;
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
//# sourceMappingURL=sync-password-to-user.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../../../../src/plugin/collections/accounts/hooks/sync-password-to-user.ts"],"sourcesContent":["import type { CollectionAfterChangeHook } from 'payload'\nimport type { CollectionHookWithBetterAuth } from '../../../types'\n\ntype CollectionAfterChangeHookWithBetterAuth =\n CollectionHookWithBetterAuth<CollectionAfterChangeHook>\n\ntype SyncPasswordToUserOptions = {\n userSlug: string\n accountSlug: string\n}\n\nexport const getSyncPasswordToUserHook = (\n options: SyncPasswordToUserOptions,\n): CollectionAfterChangeHook => {\n const hook: CollectionAfterChangeHookWithBetterAuth = async ({\n doc,\n req,\n operation,\n context,\n }) => {\n if (context?.syncAccountHook) return doc\n\n if (operation !== 'create' && operation !== 'update') {\n return doc\n }\n\n const userField = req.payload.betterAuth.options.account?.fields?.userId || 'userId'\n\n if (!doc[userField]) {\n return doc\n }\n\n const account = await req.payload.findByID({\n collection: options.accountSlug,\n id: doc.id,\n depth: 0,\n req,\n showHiddenFields: true,\n })\n\n if (!account || !account.password) {\n return doc\n }\n\n const [salt, hash] = account.password.split(':')\n\n if (!salt || !hash) {\n return doc\n }\n\n const userId = typeof doc[userField] === 'string' ? doc[userField] : doc[userField]?.id\n\n try {\n await req.payload.update({\n collection: options.userSlug,\n id: userId,\n data: {\n salt,\n hash,\n },\n req,\n context: { syncPasswordToUser: true },\n })\n } catch (error) {\n console.error('Failed to sync password to user:', error)\n }\n\n return doc\n }\n\n return hook as CollectionAfterChangeHook\n}\n"],"names":["getSyncPasswordToUserHook","options","hook","doc","req","operation","context","syncAccountHook","userField","payload","betterAuth","account","fields","userId","findByID","collection","accountSlug","id","depth","showHiddenFields","password","salt","hash","split","update","userSlug","data","syncPasswordToUser","error","console"],"mappings":"AAWA,OAAO,MAAMA,4BAA4B,CACvCC;IAEA,MAAMC,OAAgD,OAAO,EAC3DC,GAAG,EACHC,GAAG,EACHC,SAAS,EACTC,OAAO,EACR;QACC,IAAIA,SAASC,iBAAiB,OAAOJ;QAErC,IAAIE,cAAc,YAAYA,cAAc,UAAU;YACpD,OAAOF;QACT;QAEA,MAAMK,YAAYJ,IAAIK,OAAO,CAACC,UAAU,CAACT,OAAO,CAACU,OAAO,EAAEC,QAAQC,UAAU;QAE5E,IAAI,CAACV,GAAG,CAACK,UAAU,EAAE;YACnB,OAAOL;QACT;QAEA,MAAMQ,UAAU,MAAMP,IAAIK,OAAO,CAACK,QAAQ,CAAC;YACzCC,YAAYd,QAAQe,WAAW;YAC/BC,IAAId,IAAIc,EAAE;YACVC,OAAO;YACPd;YACAe,kBAAkB;QACpB;QAEA,IAAI,CAACR,WAAW,CAACA,QAAQS,QAAQ,EAAE;YACjC,OAAOjB;QACT;QAEA,MAAM,CAACkB,MAAMC,KAAK,GAAGX,QAAQS,QAAQ,CAACG,KAAK,CAAC;QAE5C,IAAI,CAACF,QAAQ,CAACC,MAAM;YAClB,OAAOnB;QACT;QAEA,MAAMU,SAAS,OAAOV,GAAG,CAACK,UAAU,KAAK,WAAWL,GAAG,CAACK,UAAU,GAAGL,GAAG,CAACK,UAAU,EAAES;QAErF,IAAI;YACF,MAAMb,IAAIK,OAAO,CAACe,MAAM,CAAC;gBACvBT,YAAYd,QAAQwB,QAAQ;gBAC5BR,IAAIJ;gBACJa,MAAM;oBACJL;oBACAC;gBACF;gBACAlB;gBACAE,SAAS;oBAAEqB,oBAAoB;gBAAK;YACtC;QACF,EAAE,OAAOC,OAAO;YACdC,QAAQD,KAAK,CAAC,oCAAoCA;QACpD;QAEA,OAAOzB;IACT;IAEA,OAAOD;AACT,EAAC"}
|