Skip to content

Commit ec55df7

Browse files
tmilewskiwobsoriano
authored andcommitted
feat(backend): Add sign ups to Backend API client (#5625)
1 parent c9de982 commit ec55df7

File tree

10 files changed

+181
-7
lines changed

10 files changed

+181
-7
lines changed

.changeset/smooth-ways-arrive.md

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
'@clerk/backend': minor
3+
---
4+
5+
Adds the ability to retrieve and update Sign Up Attempts to the Backend API client.
6+
7+
8+
```ts
9+
import { createClerkClient } from '@clerk/backend';
10+
11+
const clerkClient = createClerkClient(...);
12+
13+
await clerkClient.signUps.get('signUpAttemptId');
14+
await clerkClient.signUps.update({...});
15+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import { joinPaths } from '../../util/path';
2+
import type { SignUpAttempt } from '../resources/SignUpAttempt';
3+
import { AbstractAPI } from './AbstractApi';
4+
5+
type UpdateSignUpParams = {
6+
signUpAttemptId: string;
7+
externalId?: string | null;
8+
customAction?: boolean | null;
9+
};
10+
11+
const basePath = '/sign_ups';
12+
13+
export class SignUpAPI extends AbstractAPI {
14+
public async get(signUpAttemptId: string) {
15+
this.requireId(signUpAttemptId);
16+
17+
return this.request<SignUpAttempt>({
18+
method: 'GET',
19+
path: joinPaths(basePath, signUpAttemptId),
20+
});
21+
}
22+
23+
public async update(params: UpdateSignUpParams) {
24+
const { signUpAttemptId, ...bodyParams } = params;
25+
26+
return this.request<SignUpAttempt>({
27+
method: 'PATCH',
28+
path: joinPaths(basePath, signUpAttemptId),
29+
bodyParams,
30+
});
31+
}
32+
}

packages/backend/src/api/endpoints/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ export * from './RedirectUrlApi';
1919
export * from './SamlConnectionApi';
2020
export * from './SessionApi';
2121
export * from './SignInTokenApi';
22+
export * from './SignUpApi';
2223
export * from './TestingTokenApi';
2324
export * from './UserApi';
2425
export * from './WaitlistEntryApi';

packages/backend/src/api/factory.ts

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
SamlConnectionAPI,
2020
SessionAPI,
2121
SignInTokenAPI,
22+
SignUpAPI,
2223
TestingTokenAPI,
2324
UserAPI,
2425
WaitlistEntryAPI,
@@ -56,6 +57,7 @@ export function createBackendApiClient(options: CreateBackendApiOptions) {
5657
samlConnections: new SamlConnectionAPI(request),
5758
sessions: new SessionAPI(request),
5859
signInTokens: new SignInTokenAPI(request),
60+
signUps: new SignUpAPI(request),
5961
testingTokens: new TestingTokenAPI(request),
6062
users: new UserAPI(request),
6163
waitlistEntries: new WaitlistEntryAPI(request),

packages/backend/src/api/resources/Deserializer.ts

+3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
RedirectUrl,
2525
Session,
2626
SignInToken,
27+
SignUpAttempt,
2728
SMSMessage,
2829
Token,
2930
User,
@@ -126,6 +127,8 @@ function jsonToObject(item: any): any {
126127
return RedirectUrl.fromJSON(item);
127128
case ObjectType.SignInToken:
128129
return SignInToken.fromJSON(item);
130+
case ObjectType.SignUpAttempt:
131+
return SignUpAttempt.fromJSON(item);
129132
case ObjectType.Session:
130133
return Session.fromJSON(item);
131134
case ObjectType.SmsMessage:

packages/backend/src/api/resources/Enums.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export type OrganizationMembershipRole = OrganizationCustomRoleKey;
3333

3434
export type SignInStatus = 'needs_identifier' | 'needs_factor_one' | 'needs_factor_two' | 'complete';
3535

36-
export type SignUpStatus = 'missing_requirements' | 'complete' | 'abandoned';
36+
export type SignUpVerificationNextAction = 'needs_prepare' | 'needs_attempt' | '';
3737

3838
export type InvitationStatus = 'pending' | 'accepted' | 'revoked' | 'expired';
3939

packages/backend/src/api/resources/JSON.ts

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import type { SignUpStatus } from '@clerk/types';
2+
13
import type {
24
ActorTokenStatus,
35
AllowlistIdentifierType,
@@ -10,7 +12,7 @@ import type {
1012
OrganizationInvitationStatus,
1113
OrganizationMembershipRole,
1214
SignInStatus,
13-
SignUpStatus,
15+
SignUpVerificationNextAction,
1416
WaitlistEntryStatus,
1517
} from './Enums';
1618

@@ -467,18 +469,45 @@ export interface SignInTokenJSON extends ClerkResourceJSON {
467469

468470
export interface SignUpJSON extends ClerkResourceJSON {
469471
object: typeof ObjectType.SignUpAttempt;
472+
id: string;
470473
status: SignUpStatus;
474+
required_fields: string[];
475+
optional_fields: string[];
476+
missing_fields: string[];
477+
unverified_fields: string[];
478+
verifications: SignUpVerificationsJSON;
471479
username: string | null;
472480
email_address: string | null;
473481
phone_number: string | null;
474482
web3_wallet: string | null;
475-
web3_wallet_verification: VerificationJSON | null;
476-
external_account: any;
477-
has_password: boolean;
478-
name_full: string | null;
483+
password_enabled: boolean;
484+
first_name: string | null;
485+
last_name: string | null;
486+
public_metadata?: Record<string, unknown> | null;
487+
unsafe_metadata?: Record<string, unknown> | null;
488+
custom_action: boolean;
489+
external_id: string | null;
479490
created_session_id: string | null;
480491
created_user_id: string | null;
481492
abandon_at: number | null;
493+
legal_accepted_at: number | null;
494+
495+
/**
496+
* @deprecated Please use `verifications.external_account` instead
497+
*/
498+
external_account: object | null;
499+
}
500+
501+
export interface SignUpVerificationsJSON {
502+
email_address: SignUpVerificationJSON;
503+
phone_number: SignUpVerificationJSON;
504+
web3_wallet: SignUpVerificationJSON;
505+
external_account: VerificationJSON;
506+
}
507+
508+
export interface SignUpVerificationJSON {
509+
next_action: SignUpVerificationNextAction;
510+
supported_strategies: string[];
482511
}
483512

484513
export interface SMSMessageJSON extends ClerkResourceJSON {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import type { SignUpStatus } from '@clerk/types';
2+
3+
import type { SignUpVerificationNextAction } from './Enums';
4+
import type { SignUpJSON, SignUpVerificationJSON, SignUpVerificationsJSON } from './JSON';
5+
6+
export class SignUpAttemptVerification {
7+
constructor(
8+
readonly nextAction: SignUpVerificationNextAction,
9+
readonly supportedStrategies: string[],
10+
) {}
11+
12+
static fromJSON(data: SignUpVerificationJSON): SignUpAttemptVerification {
13+
return new SignUpAttemptVerification(data.next_action, data.supported_strategies);
14+
}
15+
}
16+
17+
export class SignUpAttemptVerifications {
18+
constructor(
19+
readonly emailAddress: SignUpAttemptVerification | null,
20+
readonly phoneNumber: SignUpAttemptVerification | null,
21+
readonly web3Wallet: SignUpAttemptVerification | null,
22+
readonly externalAccount: object | null,
23+
) {}
24+
25+
static fromJSON(data: SignUpVerificationsJSON): SignUpAttemptVerifications {
26+
return new SignUpAttemptVerifications(
27+
data.email_address && SignUpAttemptVerification.fromJSON(data.email_address),
28+
data.phone_number && SignUpAttemptVerification.fromJSON(data.phone_number),
29+
data.web3_wallet && SignUpAttemptVerification.fromJSON(data.web3_wallet),
30+
data.external_account,
31+
);
32+
}
33+
}
34+
35+
export class SignUpAttempt {
36+
constructor(
37+
readonly id: string,
38+
readonly status: SignUpStatus,
39+
readonly requiredFields: string[],
40+
readonly optionalFields: string[],
41+
readonly missingFields: string[],
42+
readonly unverifiedFields: string[],
43+
readonly verifications: SignUpAttemptVerifications | null,
44+
readonly username: string | null,
45+
readonly emailAddress: string | null,
46+
readonly phoneNumber: string | null,
47+
readonly web3Wallet: string | null,
48+
readonly passwordEnabled: boolean,
49+
readonly firstName: string | null,
50+
readonly lastName: string | null,
51+
readonly customAction: boolean,
52+
readonly externalId: string | null,
53+
readonly createdSessionId: string | null,
54+
readonly createdUserId: string | null,
55+
readonly abandonAt: number | null,
56+
readonly legalAcceptedAt: number | null,
57+
readonly publicMetadata?: Record<string, unknown> | null,
58+
readonly unsafeMetadata?: Record<string, unknown> | null,
59+
) {}
60+
61+
static fromJSON(data: SignUpJSON): SignUpAttempt {
62+
return new SignUpAttempt(
63+
data.id,
64+
data.status,
65+
data.required_fields,
66+
data.optional_fields,
67+
data.missing_fields,
68+
data.unverified_fields,
69+
data.verifications ? SignUpAttemptVerifications.fromJSON(data.verifications) : null,
70+
data.username,
71+
data.email_address,
72+
data.phone_number,
73+
data.web3_wallet,
74+
data.password_enabled,
75+
data.first_name,
76+
data.last_name,
77+
data.custom_action,
78+
data.external_id,
79+
data.created_session_id,
80+
data.created_user_id,
81+
data.abandon_at,
82+
data.legal_accepted_at,
83+
data.public_metadata,
84+
data.unsafe_metadata,
85+
);
86+
}
87+
}

packages/backend/src/api/resources/index.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ export type {
1717
OrganizationInvitationStatus,
1818
OrganizationMembershipRole,
1919
SignInStatus,
20-
SignUpStatus,
2120
} from './Enums';
2221

22+
export type { SignUpStatus } from '@clerk/types';
23+
2324
export * from './ExternalAccount';
2425
export * from './IdentificationLink';
2526
export * from './Instance';
@@ -39,6 +40,7 @@ export * from './ProxyCheck';
3940
export * from './RedirectUrl';
4041
export * from './Session';
4142
export * from './SignInTokens';
43+
export * from './SignUpAttempt';
4244
export * from './SMSMessage';
4345
export * from './Token';
4446
export * from './User';

packages/backend/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ export type {
8989
SignInJSON,
9090
SignInTokenJSON,
9191
SignUpJSON,
92+
SignUpVerificationJSON,
93+
SignUpVerificationsJSON,
9294
SMSMessageJSON,
9395
UserJSON,
9496
VerificationJSON,
@@ -130,6 +132,7 @@ export type {
130132
PhoneNumber,
131133
Session,
132134
SignInToken,
135+
SignUpAttempt,
133136
SMSMessage,
134137
Token,
135138
User,

0 commit comments

Comments
 (0)