-
Notifications
You must be signed in to change notification settings - Fork 246
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(elements): Create sign-up actors
- Loading branch information
Showing
7 changed files
with
126 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
packages/elements/src/internals/machines/sign-up/actors/start-attempt-web3.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import type { LoadedClerk, SignUpResource, Web3Strategy } from '@clerk/types'; | ||
import { fromPromise } from 'xstate'; | ||
|
||
import { ClerkElementsRuntimeError } from '~/internals/errors'; | ||
|
||
export type StartAttemptWeb3Input = { clerk: LoadedClerk; strategy: Web3Strategy }; | ||
export type StartAttemptWeb3Output = SignUpResource; | ||
|
||
export const startAttempt = fromPromise<StartAttemptWeb3Output, StartAttemptWeb3Input>(({ input }) => { | ||
const { clerk, strategy } = input; | ||
|
||
if (strategy === 'web3_metamask_signature') { | ||
return clerk.client.signUp.authenticateWithMetamask(); | ||
} | ||
|
||
if (strategy === 'web3_coinbase_wallet_signature') { | ||
return clerk.client.signUp.authenticateWithCoinbaseWallet(); | ||
} | ||
|
||
throw new ClerkElementsRuntimeError(`Unsupported Web3 strategy: ${strategy}`); | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/elements/src/internals/machines/sign-up/actors/start-attempt.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { LoadedClerk, SignUpResource } from '@clerk/types'; | ||
import { fromPromise } from 'xstate'; | ||
|
||
import { fieldsToSignUpParams } from '~/internals/machines/sign-up/utils/fields-to-params'; | ||
|
||
import type { FormFields } from '../../form'; | ||
|
||
export type StartAttemptParams = { strategy: 'ticket'; ticket: string } | { strategy?: never; ticket?: never }; | ||
export type StartAttemptInput = { clerk: LoadedClerk; fields: FormFields; params?: StartAttemptParams }; | ||
export type StartAttemptOutput = SignUpResource; | ||
|
||
// TODO: Also used for continueAttempt | ||
export const startAttempt = fromPromise<StartAttemptOutput, StartAttemptInput>(({ input }) => { | ||
const { clerk, fields, params } = input; | ||
return clerk.client.signUp.create({ ...fieldsToSignUpParams(fields), ...params }); | ||
}); |
63 changes: 63 additions & 0 deletions
63
packages/elements/src/internals/machines/sign-up/actors/verification-attempt-email-link.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { Poller } from '@clerk/shared/poller'; | ||
import type { LoadedClerk } from '@clerk/types'; | ||
import { fromCallback } from 'xstate'; | ||
|
||
import { ClerkElementsError, ClerkElementsRuntimeError } from '~/internals/errors'; | ||
|
||
export type VerificationAttemptEmailLinkInput = { clerk: LoadedClerk }; | ||
export type VerificationAttemptEmailLinkEvents = { type: 'STOP' }; | ||
|
||
export const verificationAttemptEmailLink = fromCallback< | ||
VerificationAttemptEmailLinkEvents, | ||
VerificationAttemptEmailLinkInput | ||
>(({ receive, sendBack, input: { clerk } }) => { | ||
const { run, stop } = Poller(); | ||
|
||
void run(async () => | ||
clerk.client.signUp | ||
.reload() | ||
.then(resource => { | ||
const signInStatus = resource.status; | ||
const verificationStatus = resource.verifications.emailAddress.status; | ||
|
||
// Short-circuit if the sign-up resource is already complete | ||
if (signInStatus === 'complete') { | ||
return sendBack({ type: 'EMAIL_LINK.VERIFIED', resource }); | ||
} | ||
|
||
switch (verificationStatus) { | ||
case 'verified': | ||
case 'transferable': | ||
case 'expired': { | ||
sendBack({ type: `EMAIL_LINK.${verificationStatus.toUpperCase()}`, resource }); | ||
break; | ||
} | ||
case 'failed': { | ||
sendBack({ | ||
type: 'EMAIL_LINK.FAILED', | ||
error: new ClerkElementsError('email-link-verification-failed', 'Email verification failed'), | ||
resource, | ||
}); | ||
break; | ||
} | ||
// case 'unverified': | ||
default: | ||
return; | ||
} | ||
|
||
stop(); | ||
}) | ||
.catch(error => { | ||
stop(); | ||
new ClerkElementsRuntimeError(error); | ||
}), | ||
); | ||
|
||
receive(event => { | ||
if (event.type === 'STOP') { | ||
stop(); | ||
} | ||
}); | ||
|
||
return () => stop(); | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/elements/src/internals/machines/sign-up/actors/verification-attempt.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { AttemptVerificationParams, LoadedClerk, SignUpResource } from '@clerk/types'; | ||
import { fromPromise } from 'xstate'; | ||
|
||
export type VerificationAttemptInput = { clerk: LoadedClerk; params: AttemptVerificationParams }; | ||
export type VerificationAttemptOutput = SignUpResource; | ||
|
||
export const verificationAttempt = fromPromise<VerificationAttemptOutput, VerificationAttemptInput>(({ input }) => { | ||
const { clerk, params } = input; | ||
return clerk.client.signUp.attemptVerification(params); | ||
}); |
15 changes: 15 additions & 0 deletions
15
packages/elements/src/internals/machines/sign-up/actors/verification-prepare.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { LoadedClerk, PrepareVerificationParams, SignUpResource } from '@clerk/types'; | ||
import { fromPromise } from 'xstate'; | ||
|
||
export type VerificationPrepareInput = { clerk: LoadedClerk; params: PrepareVerificationParams }; | ||
export type VerificationPrepareOutput = SignUpResource; | ||
|
||
export const verificationPrepare = fromPromise<VerificationPrepareOutput, VerificationPrepareInput>(({ input }) => { | ||
const { clerk, params } = input; | ||
|
||
if (params.strategy === 'email_link' && params.redirectUrl) { | ||
params.redirectUrl = clerk.buildUrlWithAuth(params.redirectUrl); | ||
} | ||
|
||
return clerk.client.signUp.prepareVerification(params); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters