-
Notifications
You must be signed in to change notification settings - Fork 338
/
Copy pathisomorphicClerk.ts
1288 lines (1129 loc) · 39.2 KB
/
isomorphicClerk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { inBrowser } from '@clerk/shared/browser';
import { clerkEvents, createClerkEventBus } from '@clerk/shared/clerkEventBus';
import { loadClerkJsScript } from '@clerk/shared/loadClerkJsScript';
import { handleValueOrFn } from '@clerk/shared/utils';
import type {
__experimental_CheckoutProps,
__experimental_CommerceNamespace,
__experimental_PricingTableProps,
__experimental_SubscriptionDetailsProps,
__internal_UserVerificationModalProps,
__internal_UserVerificationProps,
AuthenticateWithCoinbaseWalletParams,
AuthenticateWithGoogleOneTapParams,
AuthenticateWithMetamaskParams,
AuthenticateWithOKXWalletParams,
Clerk,
ClerkAuthenticateWithWeb3Params,
ClerkOptions,
ClerkStatus,
ClientResource,
CreateOrganizationParams,
CreateOrganizationProps,
DomainOrProxyUrl,
GoogleOneTapProps,
HandleEmailLinkVerificationParams,
HandleOAuthCallbackParams,
JoinWaitlistParams,
ListenerCallback,
LoadedClerk,
NextTaskParams,
OrganizationListProps,
OrganizationProfileProps,
OrganizationResource,
OrganizationSwitcherProps,
RedirectOptions,
SetActiveParams,
SignInProps,
SignInRedirectOptions,
SignInResource,
SignUpProps,
SignUpRedirectOptions,
SignUpResource,
UnsubscribeCallback,
UserButtonProps,
UserProfileProps,
WaitlistProps,
WaitlistResource,
Without,
} from '@clerk/types';
import { errorThrower } from './errors/errorThrower';
import { unsupportedNonBrowserDomainOrProxyUrlFunction } from './errors/messages';
import type {
BrowserClerk,
BrowserClerkConstructor,
ClerkProp,
HeadlessBrowserClerk,
HeadlessBrowserClerkConstructor,
IsomorphicClerkOptions,
} from './types';
import { isConstructor } from './utils';
if (typeof globalThis.__BUILD_DISABLE_RHC__ === 'undefined') {
globalThis.__BUILD_DISABLE_RHC__ = false;
}
const SDK_METADATA = {
name: PACKAGE_NAME,
version: PACKAGE_VERSION,
environment: process.env.NODE_ENV,
};
export interface Global {
Clerk?: HeadlessBrowserClerk | BrowserClerk;
}
declare const global: Global;
type GenericFunction<TArgs = never> = (...args: TArgs[]) => unknown;
type MethodName<T> = {
[P in keyof T]: T[P] extends GenericFunction ? P : never;
}[keyof T];
type MethodCallback = () => Promise<unknown> | unknown;
type WithVoidReturn<F extends (...args: any) => any> = (
...args: Parameters<F>
) => ReturnType<F> extends Promise<infer T> ? Promise<T | void> : ReturnType<F> | void;
type WithVoidReturnFunctions<T> = {
[K in keyof T]: T[K] extends (...args: any) => any ? WithVoidReturn<T[K]> : T[K];
};
type IsomorphicLoadedClerk = Without<
WithVoidReturnFunctions<LoadedClerk>,
| 'client'
| '__internal_addNavigationListener'
| '__internal_getCachedResources'
| '__internal_reloadInitialResources'
| '__experimental_commerce'
| '__internal_setComponentNavigationContext'
| '__internal_setActiveInProgress'
> & {
client: ClientResource | undefined;
__experimental_commerce: __experimental_CommerceNamespace | undefined;
};
export class IsomorphicClerk implements IsomorphicLoadedClerk {
private readonly mode: 'browser' | 'server';
private readonly options: IsomorphicClerkOptions;
private readonly Clerk: ClerkProp;
private clerkjs: BrowserClerk | HeadlessBrowserClerk | null = null;
private preopenOneTap?: null | GoogleOneTapProps = null;
private preopenUserVerification?: null | __internal_UserVerificationProps = null;
private preopenSignIn?: null | SignInProps = null;
private preopenCheckout?: null | __experimental_CheckoutProps = null;
private preopenSubscriptionDetails?: null | __experimental_SubscriptionDetailsProps = null;
private preopenSignUp?: null | SignUpProps = null;
private preopenUserProfile?: null | UserProfileProps = null;
private preopenOrganizationProfile?: null | OrganizationProfileProps = null;
private preopenCreateOrganization?: null | CreateOrganizationProps = null;
private preOpenWaitlist?: null | WaitlistProps = null;
private premountSignInNodes = new Map<HTMLDivElement, SignInProps | undefined>();
private premountSignUpNodes = new Map<HTMLDivElement, SignUpProps | undefined>();
private premountUserProfileNodes = new Map<HTMLDivElement, UserProfileProps | undefined>();
private premountUserButtonNodes = new Map<HTMLDivElement, UserButtonProps | undefined>();
private premountOrganizationProfileNodes = new Map<HTMLDivElement, OrganizationProfileProps | undefined>();
private premountCreateOrganizationNodes = new Map<HTMLDivElement, CreateOrganizationProps | undefined>();
private premountOrganizationSwitcherNodes = new Map<HTMLDivElement, OrganizationSwitcherProps | undefined>();
private premountOrganizationListNodes = new Map<HTMLDivElement, OrganizationListProps | undefined>();
private premountMethodCalls = new Map<MethodName<BrowserClerk>, MethodCallback>();
private premountWaitlistNodes = new Map<HTMLDivElement, WaitlistProps | undefined>();
private premountPricingTableNodes = new Map<HTMLDivElement, __experimental_PricingTableProps | undefined>();
// A separate Map of `addListener` method calls to handle multiple listeners.
private premountAddListenerCalls = new Map<
ListenerCallback,
{
unsubscribe: UnsubscribeCallback;
nativeUnsubscribe?: UnsubscribeCallback;
}
>();
private loadedListeners: Array<() => void> = [];
#status: ClerkStatus = 'loading';
#domain: DomainOrProxyUrl['domain'];
#proxyUrl: DomainOrProxyUrl['proxyUrl'];
#publishableKey: string;
#eventBus = createClerkEventBus();
get publishableKey(): string {
return this.#publishableKey;
}
get loaded(): boolean {
return this.clerkjs?.loaded || false;
}
get status(): ClerkStatus {
/**
* If clerk-js is not available the returned value can either be "loading" or "error".
*/
if (!this.clerkjs) {
return this.#status;
}
return (
this.clerkjs?.status ||
/**
* Support older clerk-js versions.
* If clerk-js is available but `.status` is missing it we need to fallback to `.loaded`.
* Since "degraded" an "error" did not exist before,
* map "loaded" to "ready" and "not loaded" to "loading".
*/
(this.clerkjs.loaded ? 'ready' : 'loading')
);
}
static #instance: IsomorphicClerk | null | undefined;
static getOrCreateInstance(options: IsomorphicClerkOptions) {
// During SSR: a new instance should be created for every request
// During CSR: use the cached instance for the whole lifetime of the app
// Also will recreate the instance if the provided Clerk instance changes
// This method should be idempotent in both scenarios
if (
!inBrowser() ||
!this.#instance ||
(options.Clerk && this.#instance.Clerk !== options.Clerk) ||
// Allow hot swapping PKs on the client
this.#instance.publishableKey !== options.publishableKey
) {
this.#instance = new IsomorphicClerk(options);
}
return this.#instance;
}
static clearInstance() {
this.#instance = null;
}
get domain(): string {
// This getter can run in environments where window is not available.
// In those cases we should expect and use domain as a string
if (typeof window !== 'undefined' && window.location) {
return handleValueOrFn(this.#domain, new URL(window.location.href), '');
}
if (typeof this.#domain === 'function') {
return errorThrower.throw(unsupportedNonBrowserDomainOrProxyUrlFunction);
}
return this.#domain || '';
}
get proxyUrl(): string {
// This getter can run in environments where window is not available.
// In those cases we should expect and use proxy as a string
if (typeof window !== 'undefined' && window.location) {
return handleValueOrFn(this.#proxyUrl, new URL(window.location.href), '');
}
if (typeof this.#proxyUrl === 'function') {
return errorThrower.throw(unsupportedNonBrowserDomainOrProxyUrlFunction);
}
return this.#proxyUrl || '';
}
/**
* Accesses private options from the `Clerk` instance and defaults to
* `IsomorphicClerk` options when in SSR context.
* @internal
*/
public __internal_getOption<K extends keyof ClerkOptions>(key: K): ClerkOptions[K] | undefined {
return this.clerkjs?.__internal_getOption ? this.clerkjs?.__internal_getOption(key) : this.options[key];
}
constructor(options: IsomorphicClerkOptions) {
const { Clerk = null, publishableKey } = options || {};
this.#publishableKey = publishableKey;
this.#proxyUrl = options?.proxyUrl;
this.#domain = options?.domain;
this.options = options;
this.Clerk = Clerk;
this.mode = inBrowser() ? 'browser' : 'server';
if (!this.options.sdkMetadata) {
this.options.sdkMetadata = SDK_METADATA;
}
this.#eventBus.emit(clerkEvents.Status, 'loading');
this.#eventBus.prioritizedOn(clerkEvents.Status, status => (this.#status = status as ClerkStatus));
if (this.#publishableKey) {
void this.loadClerkJS();
}
}
get sdkMetadata() {
return this.clerkjs?.sdkMetadata || this.options.sdkMetadata || undefined;
}
get instanceType() {
return this.clerkjs?.instanceType;
}
get frontendApi() {
return this.clerkjs?.frontendApi || '';
}
get isStandardBrowser() {
return this.clerkjs?.isStandardBrowser || this.options.standardBrowser || false;
}
get isSatellite() {
// This getter can run in environments where window is not available.
// In those cases we should expect and use domain as a string
if (typeof window !== 'undefined' && window.location) {
return handleValueOrFn(this.options.isSatellite, new URL(window.location.href), false);
}
if (typeof this.options.isSatellite === 'function') {
return errorThrower.throw(unsupportedNonBrowserDomainOrProxyUrlFunction);
}
return false;
}
buildSignInUrl = (opts?: RedirectOptions): string | void => {
const callback = () => this.clerkjs?.buildSignInUrl(opts) || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildSignInUrl', callback);
}
};
buildSignUpUrl = (opts?: RedirectOptions): string | void => {
const callback = () => this.clerkjs?.buildSignUpUrl(opts) || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildSignUpUrl', callback);
}
};
buildAfterSignInUrl = (...args: Parameters<Clerk['buildAfterSignInUrl']>): string | void => {
const callback = () => this.clerkjs?.buildAfterSignInUrl(...args) || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildAfterSignInUrl', callback);
}
};
buildAfterSignUpUrl = (...args: Parameters<Clerk['buildAfterSignUpUrl']>): string | void => {
const callback = () => this.clerkjs?.buildAfterSignUpUrl(...args) || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildAfterSignUpUrl', callback);
}
};
buildAfterSignOutUrl = (): string | void => {
const callback = () => this.clerkjs?.buildAfterSignOutUrl() || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildAfterSignOutUrl', callback);
}
};
buildAfterMultiSessionSingleSignOutUrl = (): string | void => {
const callback = () => this.clerkjs?.buildAfterMultiSessionSingleSignOutUrl() || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildAfterMultiSessionSingleSignOutUrl', callback);
}
};
buildUserProfileUrl = (): string | void => {
const callback = () => this.clerkjs?.buildUserProfileUrl() || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildUserProfileUrl', callback);
}
};
buildCreateOrganizationUrl = (): string | void => {
const callback = () => this.clerkjs?.buildCreateOrganizationUrl() || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildCreateOrganizationUrl', callback);
}
};
buildOrganizationProfileUrl = (): string | void => {
const callback = () => this.clerkjs?.buildOrganizationProfileUrl() || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildOrganizationProfileUrl', callback);
}
};
buildWaitlistUrl = (): string | void => {
const callback = () => this.clerkjs?.buildWaitlistUrl() || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildWaitlistUrl', callback);
}
};
buildUrlWithAuth = (to: string): string | void => {
const callback = () => this.clerkjs?.buildUrlWithAuth(to) || '';
if (this.clerkjs && this.loaded) {
return callback();
} else {
this.premountMethodCalls.set('buildUrlWithAuth', callback);
}
};
handleUnauthenticated = async () => {
const callback = () => this.clerkjs?.handleUnauthenticated();
if (this.clerkjs && this.loaded) {
void callback();
} else {
this.premountMethodCalls.set('handleUnauthenticated', callback);
}
};
#waitForClerkJS(): Promise<HeadlessBrowserClerk | BrowserClerk> {
return new Promise<HeadlessBrowserClerk | BrowserClerk>(resolve => {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
this.addOnLoaded(() => resolve(this.clerkjs!));
});
}
async loadClerkJS(): Promise<HeadlessBrowserClerk | BrowserClerk | undefined> {
if (this.mode !== 'browser' || this.loaded) {
return;
}
// Store frontendAPI value on window as a fallback. This value can be used as a
// fallback during ClerkJS hot loading in case ClerkJS fails to find the
// "data-clerk-frontend-api" attribute on its script tag.
// This can happen when the DOM is altered completely during client rehydration.
// For example, in Remix with React 18 the document changes completely via `hydrateRoot(document)`.
// For more information refer to:
// - https://github.com/remix-run/remix/issues/2947
// - https://github.com/facebook/react/issues/24430
if (typeof window !== 'undefined') {
window.__clerk_publishable_key = this.#publishableKey;
window.__clerk_proxy_url = this.proxyUrl;
window.__clerk_domain = this.domain;
}
try {
if (this.Clerk) {
// Set a fixed Clerk version
let c: ClerkProp;
if (isConstructor<BrowserClerkConstructor | HeadlessBrowserClerkConstructor>(this.Clerk)) {
// Construct a new Clerk object if a constructor is passed
c = new this.Clerk(this.#publishableKey, {
proxyUrl: this.proxyUrl,
domain: this.domain,
} as any);
this.beforeLoad(c);
await c.load(this.options);
} else {
// Otherwise use the instantiated Clerk object
c = this.Clerk;
if (!c.loaded) {
this.beforeLoad(c);
await c.load(this.options);
}
}
global.Clerk = c;
} else if (!__BUILD_DISABLE_RHC__) {
// Hot-load latest ClerkJS from Clerk CDN
if (!global.Clerk) {
await loadClerkJsScript({
...this.options,
publishableKey: this.#publishableKey,
proxyUrl: this.proxyUrl,
domain: this.domain,
nonce: this.options.nonce,
});
}
if (!global.Clerk) {
throw new Error('Failed to download latest ClerkJS. Contact support@clerk.com.');
}
this.beforeLoad(global.Clerk);
await global.Clerk.load(this.options);
}
if (global.Clerk?.loaded) {
return this.hydrateClerkJS(global.Clerk);
}
return;
} catch (err) {
const error = err as Error;
this.#eventBus.emit(clerkEvents.Status, 'error');
console.error(error.stack || error.message || error);
return;
}
}
public on: Clerk['on'] = (...args) => {
// Support older clerk-js versions.
if (this.clerkjs?.on) {
return this.clerkjs.on(...args);
} else {
this.#eventBus.on(...args);
}
};
public off: Clerk['off'] = (...args) => {
// Support older clerk-js versions.
if (this.clerkjs?.off) {
return this.clerkjs.off(...args);
} else {
this.#eventBus.off(...args);
}
};
/**
* @deprecated Please use `addStatusListener`. This api will be removed in the next major.
*/
public addOnLoaded = (cb: () => void) => {
this.loadedListeners.push(cb);
/**
* When IsomorphicClerk is loaded execute the callback directly
*/
if (this.loaded) {
this.emitLoaded();
}
};
/**
* @deprecated Please use `__internal_setStatus`. This api will be removed in the next major.
*/
public emitLoaded = () => {
this.loadedListeners.forEach(cb => cb());
this.loadedListeners = [];
};
private beforeLoad = (clerkjs: BrowserClerk | HeadlessBrowserClerk | undefined) => {
if (!clerkjs) {
throw new Error('Failed to hydrate latest Clerk JS');
}
};
private hydrateClerkJS = (clerkjs: BrowserClerk | HeadlessBrowserClerk | undefined) => {
if (!clerkjs) {
throw new Error('Failed to hydrate latest Clerk JS');
}
this.clerkjs = clerkjs;
this.premountMethodCalls.forEach(cb => cb());
this.premountAddListenerCalls.forEach((listenerHandlers, listener) => {
listenerHandlers.nativeUnsubscribe = clerkjs.addListener(listener);
});
this.#eventBus.internal.retrieveListeners('status')?.forEach(listener => {
// Since clerkjs exists it will call `this.clerkjs.on('status', listener)`
this.on('status', listener, { notify: true });
});
if (this.preopenSignIn !== null) {
clerkjs.openSignIn(this.preopenSignIn);
}
if (this.preopenCheckout !== null) {
clerkjs.__internal_openCheckout(this.preopenCheckout);
}
if (this.preopenSubscriptionDetails !== null) {
clerkjs.__internal_openSubscriptionDetails(this.preopenSubscriptionDetails);
}
if (this.preopenSignUp !== null) {
clerkjs.openSignUp(this.preopenSignUp);
}
if (this.preopenUserProfile !== null) {
clerkjs.openUserProfile(this.preopenUserProfile);
}
if (this.preopenUserVerification !== null) {
clerkjs.__internal_openReverification(this.preopenUserVerification);
}
if (this.preopenOneTap !== null) {
clerkjs.openGoogleOneTap(this.preopenOneTap);
}
if (this.preopenOrganizationProfile !== null) {
clerkjs.openOrganizationProfile(this.preopenOrganizationProfile);
}
if (this.preopenCreateOrganization !== null) {
clerkjs.openCreateOrganization(this.preopenCreateOrganization);
}
if (this.preOpenWaitlist !== null) {
clerkjs.openWaitlist(this.preOpenWaitlist);
}
this.premountSignInNodes.forEach((props, node) => {
clerkjs.mountSignIn(node, props);
});
this.premountSignUpNodes.forEach((props, node) => {
clerkjs.mountSignUp(node, props);
});
this.premountUserProfileNodes.forEach((props, node) => {
clerkjs.mountUserProfile(node, props);
});
this.premountUserButtonNodes.forEach((props, node) => {
clerkjs.mountUserButton(node, props);
});
this.premountOrganizationListNodes.forEach((props, node) => {
clerkjs.mountOrganizationList(node, props);
});
this.premountWaitlistNodes.forEach((props, node) => {
clerkjs.mountWaitlist(node, props);
});
this.premountPricingTableNodes.forEach((props, node) => {
clerkjs.__experimental_mountPricingTable(node, props);
});
/**
* Only update status in case `clerk.status` is missing. In any other case, `clerk-js` should be the orchestrator.
*/
if (typeof this.clerkjs.status === 'undefined') {
this.#eventBus.emit(clerkEvents.Status, 'ready');
}
this.emitLoaded();
return this.clerkjs;
};
get version() {
return this.clerkjs?.version;
}
get client(): ClientResource | undefined {
if (this.clerkjs) {
return this.clerkjs.client;
// TODO: add ssr condition
} else {
return undefined;
}
}
get session() {
if (this.clerkjs) {
return this.clerkjs.session;
} else {
return undefined;
}
}
get user() {
if (this.clerkjs) {
return this.clerkjs.user;
} else {
return undefined;
}
}
get organization() {
if (this.clerkjs) {
return this.clerkjs.organization;
} else {
return undefined;
}
}
get telemetry() {
if (this.clerkjs) {
return this.clerkjs.telemetry;
} else {
return undefined;
}
}
get __unstable__environment(): any {
if (this.clerkjs) {
return (this.clerkjs as any).__unstable__environment;
// TODO: add ssr condition
} else {
return undefined;
}
}
get isSignedIn(): boolean {
if (this.clerkjs) {
return this.clerkjs.isSignedIn;
} else {
return false;
}
}
get __experimental_commerce(): __experimental_CommerceNamespace | undefined {
return this.clerkjs?.__experimental_commerce;
}
__unstable__setEnvironment(...args: any): void {
if (this.clerkjs && '__unstable__setEnvironment' in this.clerkjs) {
(this.clerkjs as any).__unstable__setEnvironment(args);
} else {
return undefined;
}
}
__unstable__updateProps = async (props: any): Promise<void> => {
const clerkjs = await this.#waitForClerkJS();
// Handle case where accounts has clerk-react@4 installed, but clerk-js@3 is manually loaded
if (clerkjs && '__unstable__updateProps' in clerkjs) {
return (clerkjs as any).__unstable__updateProps(props);
}
};
__experimental_nextTask = async (params?: NextTaskParams): Promise<void> => {
if (this.clerkjs) {
return this.clerkjs.__experimental_nextTask(params);
} else {
return Promise.reject();
}
};
/**
* `setActive` can be used to set the active session and/or organization.
*/
setActive = (params: SetActiveParams): Promise<void> => {
if (this.clerkjs) {
return this.clerkjs.setActive(params);
} else {
return Promise.reject();
}
};
openSignIn = (props?: SignInProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.openSignIn(props);
} else {
this.preopenSignIn = props;
}
};
closeSignIn = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.closeSignIn();
} else {
this.preopenSignIn = null;
}
};
__internal_openCheckout = (props?: __experimental_CheckoutProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.__internal_openCheckout(props);
} else {
this.preopenCheckout = props;
}
};
__internal_closeCheckout = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.__internal_closeCheckout();
} else {
this.preopenCheckout = null;
}
};
__internal_openSubscriptionDetails = (props?: __experimental_SubscriptionDetailsProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.__internal_openSubscriptionDetails(props);
} else {
this.preopenSubscriptionDetails = props;
}
};
__internal_closeSubscriptionDetails = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.__internal_closeSubscriptionDetails();
} else {
this.preopenSubscriptionDetails = null;
}
};
__internal_openReverification = (props?: __internal_UserVerificationModalProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.__internal_openReverification(props);
} else {
this.preopenUserVerification = props;
}
};
__internal_closeReverification = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.__internal_closeReverification();
} else {
this.preopenUserVerification = null;
}
};
openGoogleOneTap = (props?: GoogleOneTapProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.openGoogleOneTap(props);
} else {
this.preopenOneTap = props;
}
};
closeGoogleOneTap = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.closeGoogleOneTap();
} else {
this.preopenOneTap = null;
}
};
openUserProfile = (props?: UserProfileProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.openUserProfile(props);
} else {
this.preopenUserProfile = props;
}
};
closeUserProfile = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.closeUserProfile();
} else {
this.preopenUserProfile = null;
}
};
openOrganizationProfile = (props?: OrganizationProfileProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.openOrganizationProfile(props);
} else {
this.preopenOrganizationProfile = props;
}
};
closeOrganizationProfile = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.closeOrganizationProfile();
} else {
this.preopenOrganizationProfile = null;
}
};
openCreateOrganization = (props?: CreateOrganizationProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.openCreateOrganization(props);
} else {
this.preopenCreateOrganization = props;
}
};
closeCreateOrganization = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.closeCreateOrganization();
} else {
this.preopenCreateOrganization = null;
}
};
openWaitlist = (props?: WaitlistProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.openWaitlist(props);
} else {
this.preOpenWaitlist = props;
}
};
closeWaitlist = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.closeWaitlist();
} else {
this.preOpenWaitlist = null;
}
};
openSignUp = (props?: SignUpProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.openSignUp(props);
} else {
this.preopenSignUp = props;
}
};
closeSignUp = () => {
if (this.clerkjs && this.loaded) {
this.clerkjs.closeSignUp();
} else {
this.preopenSignUp = null;
}
};
mountSignIn = (node: HTMLDivElement, props?: SignInProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountSignIn(node, props);
} else {
this.premountSignInNodes.set(node, props);
}
};
unmountSignIn = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.unmountSignIn(node);
} else {
this.premountSignInNodes.delete(node);
}
};
mountSignUp = (node: HTMLDivElement, props?: SignUpProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountSignUp(node, props);
} else {
this.premountSignUpNodes.set(node, props);
}
};
unmountSignUp = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.unmountSignUp(node);
} else {
this.premountSignUpNodes.delete(node);
}
};
mountUserProfile = (node: HTMLDivElement, props?: UserProfileProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountUserProfile(node, props);
} else {
this.premountUserProfileNodes.set(node, props);
}
};
unmountUserProfile = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.unmountUserProfile(node);
} else {
this.premountUserProfileNodes.delete(node);
}
};
mountOrganizationProfile = (node: HTMLDivElement, props?: OrganizationProfileProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountOrganizationProfile(node, props);
} else {
this.premountOrganizationProfileNodes.set(node, props);
}
};
unmountOrganizationProfile = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.unmountOrganizationProfile(node);
} else {
this.premountOrganizationProfileNodes.delete(node);
}
};
mountCreateOrganization = (node: HTMLDivElement, props?: CreateOrganizationProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountCreateOrganization(node, props);
} else {
this.premountCreateOrganizationNodes.set(node, props);
}
};
unmountCreateOrganization = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.unmountCreateOrganization(node);
} else {
this.premountCreateOrganizationNodes.delete(node);
}
};
mountOrganizationSwitcher = (node: HTMLDivElement, props?: OrganizationSwitcherProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountOrganizationSwitcher(node, props);
} else {
this.premountOrganizationSwitcherNodes.set(node, props);
}
};
unmountOrganizationSwitcher = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.unmountOrganizationSwitcher(node);
} else {
this.premountOrganizationSwitcherNodes.delete(node);
}
};
__experimental_prefetchOrganizationSwitcher = () => {
const callback = () => this.clerkjs?.__experimental_prefetchOrganizationSwitcher();
if (this.clerkjs && this.loaded) {
void callback();
} else {
this.premountMethodCalls.set('__experimental_prefetchOrganizationSwitcher', callback);
}
};
mountOrganizationList = (node: HTMLDivElement, props?: OrganizationListProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountOrganizationList(node, props);
} else {
this.premountOrganizationListNodes.set(node, props);
}
};
unmountOrganizationList = (node: HTMLDivElement) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.unmountOrganizationList(node);
} else {
this.premountOrganizationListNodes.delete(node);
}
};
mountUserButton = (node: HTMLDivElement, userButtonProps?: UserButtonProps) => {
if (this.clerkjs && this.loaded) {
this.clerkjs.mountUserButton(node, userButtonProps);
} else {
this.premountUserButtonNodes.set(node, userButtonProps);