payload-subscribers-plugin 0.0.1 → 0.0.4

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 (54) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +2 -3
  3. package/dist/components/BeforeDashboardClient.d.ts +0 -1
  4. package/dist/components/BeforeDashboardClient.js +0 -40
  5. package/dist/components/BeforeDashboardClient.js.map +0 -1
  6. package/dist/components/BeforeDashboardServer.d.ts +0 -2
  7. package/dist/components/BeforeDashboardServer.js +0 -22
  8. package/dist/components/BeforeDashboardServer.js.map +0 -1
  9. package/dist/components/BeforeDashboardServer.module.css +0 -5
  10. package/dist/components/app/RequestMagicLink.module.css +0 -5
  11. package/dist/components/app/SelectOptInChannels.module.css +0 -5
  12. package/dist/components/app/Subscribe.module.css +0 -5
  13. package/dist/components/app/VerifyMagicLink.module.css +0 -5
  14. package/dist/copied/payload.config.d.ts +0 -2
  15. package/dist/endpoints/customEndpointHandler.d.ts +0 -2
  16. package/dist/endpoints/customEndpointHandler.js +0 -7
  17. package/dist/endpoints/customEndpointHandler.js.map +0 -1
  18. package/dist/exports/client.d.ts +0 -1
  19. package/dist/exports/client.js +0 -3
  20. package/dist/exports/client.js.map +0 -1
  21. package/dist/exports/rsc.d.ts +0 -1
  22. package/dist/exports/rsc.js +0 -3
  23. package/dist/exports/rsc.js.map +0 -1
  24. package/dist/helpers/serverConfig.d.ts +0 -4
  25. package/dist/helpers/serverConfig.js +0 -22
  26. package/dist/helpers/serverConfig.js.map +0 -1
  27. package/dist/server-functions/subscriberAuth.d.ts +0 -11
  28. package/src/collections/OptInChannels.ts +0 -45
  29. package/src/collections/Subscribers.ts +0 -99
  30. package/src/collections/fields/OptedInChannels.ts +0 -12
  31. package/src/components/app/RequestMagicLink.tsx +0 -129
  32. package/src/components/app/RequestOrSubscribe.tsx +0 -58
  33. package/src/components/app/SelectOptInChannels.tsx +0 -147
  34. package/src/components/app/Subscribe.tsx +0 -190
  35. package/src/components/app/SubscriberMenu.tsx +0 -46
  36. package/src/components/app/VerifyMagicLink.tsx +0 -197
  37. package/src/components/app/helpers.ts +0 -6
  38. package/src/components/app/shared.module.css +0 -14
  39. package/src/contexts/SubscriberProvider.tsx +0 -122
  40. package/src/copied/payload-types.ts +0 -478
  41. package/src/endpoints/getOptInChannels.ts +0 -56
  42. package/src/endpoints/logout.ts +0 -104
  43. package/src/endpoints/requestMagicLink.ts +0 -139
  44. package/src/endpoints/subscribe.ts +0 -435
  45. package/src/endpoints/subscriberAuth.ts +0 -100
  46. package/src/endpoints/verifyMagicLink.ts +0 -164
  47. package/src/exports/index.ts +0 -1
  48. package/src/exports/ui.ts +0 -17
  49. package/src/helpers/testData.ts +0 -2
  50. package/src/helpers/token.ts +0 -14
  51. package/src/helpers/verifyOptIns.ts +0 -39
  52. package/src/index.ts +0 -207
  53. package/src/react-hooks/useServerUrl.tsx +0 -18
  54. package/src/server-functions/serverUrl.ts +0 -38
@@ -1,122 +0,0 @@
1
- 'use client'
2
-
3
- import { PayloadSDK } from '@payloadcms/sdk'
4
- import { type ReactNode, useCallback, useEffect } from 'react'
5
- import { createContext, useContext, useMemo, useState } from 'react'
6
-
7
- import type { Config, Subscriber } from '../copied/payload-types.js'
8
-
9
- import { useServerUrl } from '../react-hooks/useServerUrl.js'
10
-
11
- export type SubscriberContextType = {
12
- isLoaded: boolean
13
- logOut: () => void
14
- permissions: any
15
- refreshSubscriber: () => void
16
- subscriber: null | Subscriber
17
- }
18
-
19
- const SubscriberContext = createContext<SubscriberContextType | undefined>(undefined)
20
-
21
- interface ProviderProps {
22
- children?: ReactNode // Recommended type for children
23
- }
24
-
25
- export function SubscriberProvider({ children }: ProviderProps) {
26
- // eslint-disable-next-line
27
- const [subscriber, setSubscriber] = useState<null | (Subscriber & { optIns: string[] })>(null)
28
-
29
- const { serverURL } = useServerUrl()
30
-
31
- // Keep track of if the selection content is loaded yet
32
- const [isLoaded, setIsLoaded] = useState(false)
33
-
34
- const [permissions, setPermissions] = useState<any>()
35
-
36
- const refreshSubscriber = useCallback(async () => {
37
- const initSubscriber = async () => {
38
- setIsLoaded(false)
39
- try {
40
- const sdk = new PayloadSDK<Config>({
41
- baseURL: serverURL || '',
42
- })
43
- const authResponse = await sdk.request({
44
- json: {},
45
- method: 'POST',
46
- path: '/api/subscriberAuth',
47
- })
48
-
49
- if (authResponse.ok) {
50
- // Call the server function to get the user data
51
- const { permissions, subscriber } = await authResponse.json()
52
- // console.log(`subscriber = `, subscriber)
53
- // console.log(`permissions = `, permissions)
54
- setPermissions(permissions)
55
- setSubscriber(subscriber)
56
- } else {
57
- setPermissions(null)
58
- setSubscriber(null)
59
- }
60
- } catch (error: unknown) {
61
- console.log(`authResponse error`, error)
62
- }
63
- setIsLoaded(true)
64
- }
65
- await initSubscriber()
66
- }, [serverURL])
67
-
68
- const logOut = useCallback(async () => {
69
- setIsLoaded(false)
70
- try {
71
- // const sdk = new PayloadSDK<Config>({
72
- // baseURL: serverURL || '',
73
- // })
74
- // const logoutResponse = await sdk.request({
75
- // json: {},
76
- // method: 'POST',
77
- // path: '/api/logout',
78
- // })
79
- // Unsure why sdk isn't working here
80
- const logoutResponse = await fetch('/api/logout', {
81
- method: 'POST',
82
- })
83
-
84
- // console.log(`logoutResponse`, logoutResponse)
85
-
86
- if (logoutResponse.ok) {
87
- setSubscriber(null)
88
- setPermissions(null)
89
- }
90
- } catch (error: unknown) {
91
- console.log(`logoutResponse error`, error)
92
- }
93
- setIsLoaded(true)
94
- }, [])
95
-
96
- useEffect(() => {
97
- void refreshSubscriber()
98
- }, [refreshSubscriber]) // Empty dependency array for mount/unmount
99
-
100
- // Memoize the value to prevent unnecessary re-renders in consumers
101
- const contextValue: SubscriberContextType = useMemo(
102
- () => ({
103
- isLoaded,
104
- logOut,
105
- permissions,
106
- refreshSubscriber,
107
- subscriber,
108
- }),
109
- [isLoaded, logOut, permissions, refreshSubscriber, subscriber],
110
- )
111
-
112
- return <SubscriberContext.Provider value={contextValue}>{children}</SubscriberContext.Provider>
113
- }
114
-
115
- // Custom hook to easily consume the context and add error handling
116
- export function useSubscriber() {
117
- const context = useContext(SubscriberContext)
118
- if (context === undefined) {
119
- throw new Error('useSubscriber must be used within a SubscriberProvider')
120
- }
121
- return context
122
- }
@@ -1,478 +0,0 @@
1
- /* tslint:disable */
2
- /* eslint-disable */
3
- /**
4
- * This file was automatically generated by Payload.
5
- * DO NOT MODIFY IT BY HAND. Instead, modify your source Payload config,
6
- * and re-run `payload generate:types` to regenerate this file.
7
- */
8
-
9
- /**
10
- * Supported timezones in IANA format.
11
- *
12
- * This interface was referenced by `Config`'s JSON-Schema
13
- * via the `definition` "supportedTimezones".
14
- */
15
- export type SupportedTimezones =
16
- | 'Pacific/Midway'
17
- | 'Pacific/Niue'
18
- | 'Pacific/Honolulu'
19
- | 'Pacific/Rarotonga'
20
- | 'America/Anchorage'
21
- | 'Pacific/Gambier'
22
- | 'America/Los_Angeles'
23
- | 'America/Tijuana'
24
- | 'America/Denver'
25
- | 'America/Phoenix'
26
- | 'America/Chicago'
27
- | 'America/Guatemala'
28
- | 'America/New_York'
29
- | 'America/Bogota'
30
- | 'America/Caracas'
31
- | 'America/Santiago'
32
- | 'America/Buenos_Aires'
33
- | 'America/Sao_Paulo'
34
- | 'Atlantic/South_Georgia'
35
- | 'Atlantic/Azores'
36
- | 'Atlantic/Cape_Verde'
37
- | 'Europe/London'
38
- | 'Europe/Berlin'
39
- | 'Africa/Lagos'
40
- | 'Europe/Athens'
41
- | 'Africa/Cairo'
42
- | 'Europe/Moscow'
43
- | 'Asia/Riyadh'
44
- | 'Asia/Dubai'
45
- | 'Asia/Baku'
46
- | 'Asia/Karachi'
47
- | 'Asia/Tashkent'
48
- | 'Asia/Calcutta'
49
- | 'Asia/Dhaka'
50
- | 'Asia/Almaty'
51
- | 'Asia/Jakarta'
52
- | 'Asia/Bangkok'
53
- | 'Asia/Shanghai'
54
- | 'Asia/Singapore'
55
- | 'Asia/Tokyo'
56
- | 'Asia/Seoul'
57
- | 'Australia/Brisbane'
58
- | 'Australia/Sydney'
59
- | 'Pacific/Guam'
60
- | 'Pacific/Noumea'
61
- | 'Pacific/Auckland'
62
- | 'Pacific/Fiji';
63
-
64
- export interface Config {
65
- auth: {
66
- users: UserAuthOperations;
67
- subscribers: SubscriberAuthOperations;
68
- };
69
- blocks: {};
70
- collections: {
71
- posts: Post;
72
- media: Media;
73
- users: User;
74
- 'opt-in-channels': OptInChannel;
75
- subscribers: Subscriber;
76
- 'payload-kv': PayloadKv;
77
- 'payload-locked-documents': PayloadLockedDocument;
78
- 'payload-preferences': PayloadPreference;
79
- 'payload-migrations': PayloadMigration;
80
- };
81
- collectionsJoins: {};
82
- collectionsSelect: {
83
- posts: PostsSelect<false> | PostsSelect<true>;
84
- media: MediaSelect<false> | MediaSelect<true>;
85
- users: UsersSelect<false> | UsersSelect<true>;
86
- 'opt-in-channels': OptInChannelsSelect<false> | OptInChannelsSelect<true>;
87
- subscribers: SubscribersSelect<false> | SubscribersSelect<true>;
88
- 'payload-kv': PayloadKvSelect<false> | PayloadKvSelect<true>;
89
- 'payload-locked-documents': PayloadLockedDocumentsSelect<false> | PayloadLockedDocumentsSelect<true>;
90
- 'payload-preferences': PayloadPreferencesSelect<false> | PayloadPreferencesSelect<true>;
91
- 'payload-migrations': PayloadMigrationsSelect<false> | PayloadMigrationsSelect<true>;
92
- };
93
- db: {
94
- defaultIDType: string;
95
- };
96
- fallbackLocale: null;
97
- globals: {};
98
- globalsSelect: {};
99
- locale: null;
100
- user:
101
- | (User & {
102
- collection: 'users';
103
- })
104
- | (Subscriber & {
105
- collection: 'subscribers';
106
- });
107
- jobs: {
108
- tasks: unknown;
109
- workflows: unknown;
110
- };
111
- }
112
- export interface UserAuthOperations {
113
- forgotPassword: {
114
- email: string;
115
- password: string;
116
- };
117
- login: {
118
- email: string;
119
- password: string;
120
- };
121
- registerFirstUser: {
122
- email: string;
123
- password: string;
124
- };
125
- unlock: {
126
- email: string;
127
- password: string;
128
- };
129
- }
130
- export interface SubscriberAuthOperations {
131
- forgotPassword: {
132
- email: string;
133
- password: string;
134
- };
135
- login: {
136
- email: string;
137
- password: string;
138
- };
139
- registerFirstUser: {
140
- email: string;
141
- password: string;
142
- };
143
- unlock: {
144
- email: string;
145
- password: string;
146
- };
147
- }
148
- /**
149
- * This interface was referenced by `Config`'s JSON-Schema
150
- * via the `definition` "posts".
151
- */
152
- export interface Post {
153
- id: string;
154
- optIns?: (string | OptInChannel)[] | null;
155
- updatedAt: string;
156
- createdAt: string;
157
- }
158
- /**
159
- * This interface was referenced by `Config`'s JSON-Schema
160
- * via the `definition` "opt-in-channels".
161
- */
162
- export interface OptInChannel {
163
- id: string;
164
- title: string;
165
- description?: string | null;
166
- active: boolean;
167
- slug?: string | null;
168
- updatedAt: string;
169
- createdAt: string;
170
- }
171
- /**
172
- * This interface was referenced by `Config`'s JSON-Schema
173
- * via the `definition` "media".
174
- */
175
- export interface Media {
176
- id: string;
177
- updatedAt: string;
178
- createdAt: string;
179
- url?: string | null;
180
- thumbnailURL?: string | null;
181
- filename?: string | null;
182
- mimeType?: string | null;
183
- filesize?: number | null;
184
- width?: number | null;
185
- height?: number | null;
186
- focalX?: number | null;
187
- focalY?: number | null;
188
- }
189
- /**
190
- * This interface was referenced by `Config`'s JSON-Schema
191
- * via the `definition` "users".
192
- */
193
- export interface User {
194
- id: string;
195
- updatedAt: string;
196
- createdAt: string;
197
- email: string;
198
- resetPasswordToken?: string | null;
199
- resetPasswordExpiration?: string | null;
200
- salt?: string | null;
201
- hash?: string | null;
202
- loginAttempts?: number | null;
203
- lockUntil?: string | null;
204
- sessions?:
205
- | {
206
- id: string;
207
- createdAt?: string | null;
208
- expiresAt: string;
209
- }[]
210
- | null;
211
- password?: string | null;
212
- }
213
- /**
214
- * This interface was referenced by `Config`'s JSON-Schema
215
- * via the `definition` "subscribers".
216
- */
217
- export interface Subscriber {
218
- id: string;
219
- firstName?: string | null;
220
- status: 'subscribed' | 'unsubscribed' | 'pending';
221
- source?: string | null;
222
- verificationToken?: string | null;
223
- verificationTokenExpires?: string | null;
224
- optIns?: (string | OptInChannel)[] | null;
225
- updatedAt: string;
226
- createdAt: string;
227
- email: string;
228
- resetPasswordToken?: string | null;
229
- resetPasswordExpiration?: string | null;
230
- salt?: string | null;
231
- hash?: string | null;
232
- loginAttempts?: number | null;
233
- lockUntil?: string | null;
234
- sessions?:
235
- | {
236
- id: string;
237
- createdAt?: string | null;
238
- expiresAt: string;
239
- }[]
240
- | null;
241
- password?: string | null;
242
- }
243
- /**
244
- * This interface was referenced by `Config`'s JSON-Schema
245
- * via the `definition` "payload-kv".
246
- */
247
- export interface PayloadKv {
248
- id: string;
249
- key: string;
250
- data:
251
- | {
252
- [k: string]: unknown;
253
- }
254
- | unknown[]
255
- | string
256
- | number
257
- | boolean
258
- | null;
259
- }
260
- /**
261
- * This interface was referenced by `Config`'s JSON-Schema
262
- * via the `definition` "payload-locked-documents".
263
- */
264
- export interface PayloadLockedDocument {
265
- id: string;
266
- document?:
267
- | ({
268
- relationTo: 'posts';
269
- value: string | Post;
270
- } | null)
271
- | ({
272
- relationTo: 'media';
273
- value: string | Media;
274
- } | null)
275
- | ({
276
- relationTo: 'users';
277
- value: string | User;
278
- } | null)
279
- | ({
280
- relationTo: 'opt-in-channels';
281
- value: string | OptInChannel;
282
- } | null)
283
- | ({
284
- relationTo: 'subscribers';
285
- value: string | Subscriber;
286
- } | null);
287
- globalSlug?: string | null;
288
- user:
289
- | {
290
- relationTo: 'users';
291
- value: string | User;
292
- }
293
- | {
294
- relationTo: 'subscribers';
295
- value: string | Subscriber;
296
- };
297
- updatedAt: string;
298
- createdAt: string;
299
- }
300
- /**
301
- * This interface was referenced by `Config`'s JSON-Schema
302
- * via the `definition` "payload-preferences".
303
- */
304
- export interface PayloadPreference {
305
- id: string;
306
- user:
307
- | {
308
- relationTo: 'users';
309
- value: string | User;
310
- }
311
- | {
312
- relationTo: 'subscribers';
313
- value: string | Subscriber;
314
- };
315
- key?: string | null;
316
- value?:
317
- | {
318
- [k: string]: unknown;
319
- }
320
- | unknown[]
321
- | string
322
- | number
323
- | boolean
324
- | null;
325
- updatedAt: string;
326
- createdAt: string;
327
- }
328
- /**
329
- * This interface was referenced by `Config`'s JSON-Schema
330
- * via the `definition` "payload-migrations".
331
- */
332
- export interface PayloadMigration {
333
- id: string;
334
- name?: string | null;
335
- batch?: number | null;
336
- updatedAt: string;
337
- createdAt: string;
338
- }
339
- /**
340
- * This interface was referenced by `Config`'s JSON-Schema
341
- * via the `definition` "posts_select".
342
- */
343
- export interface PostsSelect<T extends boolean = true> {
344
- optIns?: T;
345
- updatedAt?: T;
346
- createdAt?: T;
347
- }
348
- /**
349
- * This interface was referenced by `Config`'s JSON-Schema
350
- * via the `definition` "media_select".
351
- */
352
- export interface MediaSelect<T extends boolean = true> {
353
- updatedAt?: T;
354
- createdAt?: T;
355
- url?: T;
356
- thumbnailURL?: T;
357
- filename?: T;
358
- mimeType?: T;
359
- filesize?: T;
360
- width?: T;
361
- height?: T;
362
- focalX?: T;
363
- focalY?: T;
364
- }
365
- /**
366
- * This interface was referenced by `Config`'s JSON-Schema
367
- * via the `definition` "users_select".
368
- */
369
- export interface UsersSelect<T extends boolean = true> {
370
- updatedAt?: T;
371
- createdAt?: T;
372
- email?: T;
373
- resetPasswordToken?: T;
374
- resetPasswordExpiration?: T;
375
- salt?: T;
376
- hash?: T;
377
- loginAttempts?: T;
378
- lockUntil?: T;
379
- sessions?:
380
- | T
381
- | {
382
- id?: T;
383
- createdAt?: T;
384
- expiresAt?: T;
385
- };
386
- }
387
- /**
388
- * This interface was referenced by `Config`'s JSON-Schema
389
- * via the `definition` "opt-in-channels_select".
390
- */
391
- export interface OptInChannelsSelect<T extends boolean = true> {
392
- title?: T;
393
- description?: T;
394
- active?: T;
395
- slug?: T;
396
- updatedAt?: T;
397
- createdAt?: T;
398
- }
399
- /**
400
- * This interface was referenced by `Config`'s JSON-Schema
401
- * via the `definition` "subscribers_select".
402
- */
403
- export interface SubscribersSelect<T extends boolean = true> {
404
- firstName?: T;
405
- status?: T;
406
- source?: T;
407
- verificationToken?: T;
408
- verificationTokenExpires?: T;
409
- optIns?: T;
410
- updatedAt?: T;
411
- createdAt?: T;
412
- email?: T;
413
- resetPasswordToken?: T;
414
- resetPasswordExpiration?: T;
415
- salt?: T;
416
- hash?: T;
417
- loginAttempts?: T;
418
- lockUntil?: T;
419
- sessions?:
420
- | T
421
- | {
422
- id?: T;
423
- createdAt?: T;
424
- expiresAt?: T;
425
- };
426
- }
427
- /**
428
- * This interface was referenced by `Config`'s JSON-Schema
429
- * via the `definition` "payload-kv_select".
430
- */
431
- export interface PayloadKvSelect<T extends boolean = true> {
432
- key?: T;
433
- data?: T;
434
- }
435
- /**
436
- * This interface was referenced by `Config`'s JSON-Schema
437
- * via the `definition` "payload-locked-documents_select".
438
- */
439
- export interface PayloadLockedDocumentsSelect<T extends boolean = true> {
440
- document?: T;
441
- globalSlug?: T;
442
- user?: T;
443
- updatedAt?: T;
444
- createdAt?: T;
445
- }
446
- /**
447
- * This interface was referenced by `Config`'s JSON-Schema
448
- * via the `definition` "payload-preferences_select".
449
- */
450
- export interface PayloadPreferencesSelect<T extends boolean = true> {
451
- user?: T;
452
- key?: T;
453
- value?: T;
454
- updatedAt?: T;
455
- createdAt?: T;
456
- }
457
- /**
458
- * This interface was referenced by `Config`'s JSON-Schema
459
- * via the `definition` "payload-migrations_select".
460
- */
461
- export interface PayloadMigrationsSelect<T extends boolean = true> {
462
- name?: T;
463
- batch?: T;
464
- updatedAt?: T;
465
- createdAt?: T;
466
- }
467
- /**
468
- * This interface was referenced by `Config`'s JSON-Schema
469
- * via the `definition` "auth".
470
- */
471
- export interface Auth {
472
- [k: string]: unknown;
473
- }
474
-
475
-
476
- declare module 'payload' {
477
- export interface GeneratedTypes extends Config {}
478
- }
@@ -1,56 +0,0 @@
1
- import type { OptInChannel } from '../copied/payload-types.js'
2
- import type { CollectionSlug, Endpoint, PayloadHandler } from 'payload'
3
-
4
- import { OptInChannels as OptInChannelCollection } from '../collections/OptInChannels.js'
5
-
6
- export type GetOptInChannelsResponse =
7
- | {
8
- error: string
9
- now: string
10
- }
11
- | {
12
- now: string
13
- optInChannels: OptInChannel[]
14
- }
15
-
16
- /**
17
- *
18
- * @returns
19
- */
20
- export const getOptInChannelsHandler: PayloadHandler = async (req) => {
21
- const findResults = await req.payload.find({
22
- collection: OptInChannelCollection.slug as CollectionSlug,
23
- depth: 2,
24
- where: {
25
- active: { equals: true },
26
- },
27
- })
28
- // .catch((error) => {
29
- // return Response.json({ error, now: new Date().toISOString() } as GetOptInChannelsResponse, {
30
- // status: 400,
31
- // })
32
- // })
33
-
34
- if (!findResults) {
35
- return Response.json(
36
- { error: 'Unknown find result', now: new Date().toISOString() } as GetOptInChannelsResponse,
37
- { status: 400 },
38
- )
39
- }
40
-
41
- return Response.json({
42
- now: new Date().toISOString(),
43
- optInChannels: findResults.docs,
44
- } as GetOptInChannelsResponse)
45
- }
46
-
47
- /**
48
- * getOptInChannels Endpoint Config
49
- */
50
- const getOptInChannelsEndpoint: Endpoint = {
51
- handler: getOptInChannelsHandler,
52
- method: 'get',
53
- path: '/optinchannels',
54
- }
55
-
56
- export default getOptInChannelsEndpoint