Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rich-parrots-crash.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
---
"@clerk/elements": minor
---

Add Metamask (Web3) support for sign in and sign up
Original file line numberDiff line numberDiff line change
Expand Up@@ -334,6 +334,9 @@ export const SignInRouterMachine = setup({
'AUTHENTICATE.PASSKEY.AUTOFILL': {
actions: sendTo('start', ({ event }) => event),
},
'AUTHENTICATE.WEB3': {
actions: sendTo('start', ({ event }) => event),
},
NEXT: [
{
guard: 'isComplete',
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
import type { SignInResource } from '@clerk/types';
import { fromPromise, not, sendTo, setup } from 'xstate';
import type { SignInResource, Web3Strategy } from '@clerk/types';
import { assertEvent, fromPromise, not, sendTo, setup } from 'xstate';

import { SIGN_IN_DEFAULT_BASE_PATH } from '~/internals/constants';
import { ClerkElementsRuntimeError } from '~/internals/errors';
import type { FormFields } from '~/internals/machines/form';
import { sendToLoading } from '~/internals/machines/shared';
import { assertActorEventError } from '~/internals/machines/utils/assert';
Expand All@@ -23,6 +24,14 @@ export const SignInStartMachine = setup({
flow,
});
}),
attemptWeb3: fromPromise<SignInResource, { parent: SignInRouterMachineActorRef; strategy: Web3Strategy }>(
({ input: { parent, strategy } }) => {
if (strategy === 'web3_metamask_signature') {
return parent.getSnapshot().context.clerk.client.signIn.authenticateWithMetamask();
}
throw new ClerkElementsRuntimeError(`Unsupported Web3 strategy: ${strategy}`);
},
),
attempt: fromPromise<SignInResource, { parent: SignInRouterMachineActorRef; fields: FormFields }>(
({ input: { fields, parent } }) => {
const clerk = parent.getSnapshot().context.clerk;
Expand DownExpand Up@@ -94,6 +103,11 @@ export const SignInStartMachine = setup({
target: 'AttemptingPasskeyAutoFill',
reenter: false,
},
'AUTHENTICATE.WEB3': {
guard: not('isExampleMode'),
target: 'AttemptingWeb3',
reenter: true,
},
},
},
Attempting: {
Expand DownExpand Up@@ -163,5 +177,27 @@ export const SignInStartMachine = setup({
},
},
},
AttemptingWeb3: {
tags: ['state:attempting', 'state:loading'],
entry: 'sendToLoading',
invoke: {
id: 'attemptWeb3',
src: 'attemptWeb3',
input: ({ context, event }) => {
assertEvent(event, 'AUTHENTICATE.WEB3');
return {
parent: context.parent,
strategy: event.strategy,
};
},
onDone: {
actions: ['sendToNext', 'sendToLoading'],
},
onError: {
actions: ['setFormErrors', 'sendToLoading'],
target: 'Pending',
},
},
},
},
});
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
import type { ClerkAPIResponseError } from '@clerk/shared/error';
import type { Web3Strategy } from '@clerk/types';
import type { ActorRefFrom, DoneActorEvent, ErrorActorEvent } from 'xstate';

import type { FormMachine } from '~/internals/machines/form';
Expand All@@ -14,12 +15,14 @@ export type SignInStartTags = 'state:pending' | 'state:attempting' | 'state:load
export type SignInStartSubmitEvent = { type: 'SUBMIT' };
export type SignInStartPasskeyEvent = { type: 'AUTHENTICATE.PASSKEY' };
export type SignInStartPasskeyAutofillEvent = { type: 'AUTHENTICATE.PASSKEY.AUTOFILL' };
export type SignInStartWeb3Event = { type: 'AUTHENTICATE.WEB3'; strategy: Web3Strategy };

export type SignInStartEvents =
| ErrorActorEvent
| SignInStartSubmitEvent
| SignInStartPasskeyEvent
| SignInStartPasskeyAutofillEvent
| SignInStartWeb3Event
| DoneActorEvent;

// ---------------------------------- Input ---------------------------------- //
Expand Down
Original file line numberDiff line numberDiff line change
Expand Up@@ -195,6 +195,9 @@ export const SignUpRouterMachine = setup({
params: { strategy: 'saml' },
}),
},
'AUTHENTICATE.WEB3': {
actions: sendTo('start', ({ event }) => event),
},
'FORM.ATTACH': {
description: 'Attach/re-attach the form to the router.',
actions: enqueueActions(({ enqueue, event }) => {
Expand Down
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
import type { SignUpResource } from '@clerk/types';
import { fromPromise, not, sendTo, setup } from 'xstate';
import type { SignUpResource, Web3Strategy } from '@clerk/types';
import { assertEvent, fromPromise, not, sendTo, setup } from 'xstate';

import { SIGN_UP_DEFAULT_BASE_PATH } from '~/internals/constants';
import { ClerkElementsRuntimeError } from '~/internals/errors';
import type { FormFields } from '~/internals/machines/form';
import { sendToLoading } from '~/internals/machines/shared';
import { fieldsToSignUpParams } from '~/internals/machines/sign-up/utils';
Expand DownExpand Up@@ -29,6 +30,14 @@ export const SignUpStartMachine = setup({
return parent.getSnapshot().context.clerk.client.signUp.create(params);
},
),
attemptWeb3: fromPromise<SignUpResource, { parent: SignInRouterMachineActorRef; strategy: Web3Strategy }>(
({ input: { parent, strategy } }) => {
if (strategy === 'web3_metamask_signature') {
return parent.getSnapshot().context.clerk.client.signUp.authenticateWithMetamask();
}
throw new ClerkElementsRuntimeError(`Unsupported Web3 strategy: ${strategy}`);
},
),
thirdParty: ThirdPartyMachine,
},
actions: {
Expand DownExpand Up@@ -84,6 +93,11 @@ export const SignUpStartMachine = setup({
target: 'Attempting',
reenter: true,
},
'AUTHENTICATE.WEB3': {
guard: not('isExampleMode'),
target: 'AttemptingWeb3',
reenter: true,
},
},
},
Attempting: {
Expand All@@ -105,5 +119,27 @@ export const SignUpStartMachine = setup({
},
},
},
AttemptingWeb3: {
tags: ['state:attempting', 'state:loading'],
entry: 'sendToLoading',
invoke: {
id: 'attemptCreateWeb3',
src: 'attemptWeb3',
input: ({ context, event }) => {
assertEvent(event, 'AUTHENTICATE.WEB3');
return {
parent: context.parent,
strategy: event.strategy,
};
},
onDone: {
actions: ['sendToNext', 'sendToLoading'],
},
onError: {
actions: ['setFormErrors', 'sendToLoading'],
target: 'Pending',
},
},
},
},
});