Skip to content

Commit efb6fac

Browse files
authored
feat(clerk-js): SignIn and SignUp debug logs (#6665)
1 parent 65b12ee commit efb6fac

File tree

3 files changed

+104
-33
lines changed

3 files changed

+104
-33
lines changed

.changeset/thirty-knives-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@clerk/clerk-js': patch
3+
---
4+
5+
Adding baseline debug logging to SignIn and SignUp components

packages/clerk-js/src/core/resources/SignIn.ts

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ import type {
5353
Web3SignatureFactor,
5454
} from '@clerk/types';
5555

56+
import { debugLogger } from '@/utils/debug';
57+
5658
import {
5759
generateSignatureWithBase,
5860
generateSignatureWithCoinbaseWallet,
@@ -88,7 +90,7 @@ export class SignIn extends BaseResource implements SignInResource {
8890
pathRoot = '/client/sign_ins';
8991

9092
id?: string;
91-
status: SignInStatus | null = null;
93+
private _status: SignInStatus | null = null;
9294
supportedIdentifiers: SignInIdentifier[] = [];
9395
supportedFirstFactors: SignInFirstFactor[] | null = [];
9496
supportedSecondFactors: SignInSecondFactor[] | null = null;
@@ -98,6 +100,31 @@ export class SignIn extends BaseResource implements SignInResource {
98100
createdSessionId: string | null = null;
99101
userData: UserData = new UserData(null);
100102

103+
/**
104+
* The current status of the sign-in process.
105+
*
106+
* @returns The current sign-in status, or null if no status has been set
107+
*/
108+
get status(): SignInStatus | null {
109+
return this._status;
110+
}
111+
112+
/**
113+
* Sets the sign-in status and logs the transition at debug level.
114+
*
115+
* @param value - The new status to set. Can be null to clear the status.
116+
* @remarks When setting a new status that differs from the previous one,
117+
* a debug log entry is created showing the transition from the old to new status.
118+
*/
119+
set status(value: SignInStatus | null) {
120+
const previousStatus = this._status;
121+
this._status = value;
122+
123+
if (value && previousStatus !== value) {
124+
debugLogger.debug('SignIn.status', { id: this.id, from: previousStatus, to: value });
125+
}
126+
}
127+
101128
/**
102129
* @experimental This experimental API is subject to change.
103130
*
@@ -118,7 +145,8 @@ export class SignIn extends BaseResource implements SignInResource {
118145
this.fromJSON(data);
119146
}
120147

121-
create = (params: SignInCreateParams): Promise<this> => {
148+
create = (params: SignInCreateParams): Promise<SignInResource> => {
149+
debugLogger.debug('SignIn.create', { id: this.id, strategy: 'strategy' in params ? params.strategy : undefined });
122150
return this._basePost({
123151
path: this.pathRoot,
124152
body: params,
@@ -132,76 +160,78 @@ export class SignIn extends BaseResource implements SignInResource {
132160
});
133161
};
134162

135-
prepareFirstFactor = (factor: PrepareFirstFactorParams): Promise<SignInResource> => {
163+
prepareFirstFactor = (params: PrepareFirstFactorParams): Promise<SignInResource> => {
164+
debugLogger.debug('SignIn.prepareFirstFactor', { id: this.id, strategy: params.strategy });
136165
let config;
137-
switch (factor.strategy) {
166+
switch (params.strategy) {
138167
case 'passkey':
139168
config = {} as PassKeyConfig;
140169
break;
141170
case 'email_link':
142171
config = {
143-
emailAddressId: factor.emailAddressId,
144-
redirectUrl: factor.redirectUrl,
172+
emailAddressId: params.emailAddressId,
173+
redirectUrl: params.redirectUrl,
145174
} as EmailLinkConfig;
146175
break;
147176
case 'email_code':
148-
config = { emailAddressId: factor.emailAddressId } as EmailCodeConfig;
177+
config = { emailAddressId: params.emailAddressId } as EmailCodeConfig;
149178
break;
150179
case 'phone_code':
151180
config = {
152-
phoneNumberId: factor.phoneNumberId,
153-
default: factor.default,
154-
channel: factor.channel,
181+
phoneNumberId: params.phoneNumberId,
182+
default: params.default,
183+
channel: params.channel,
155184
} as PhoneCodeConfig;
156185
break;
157186
case 'web3_metamask_signature':
158187
case 'web3_base_signature':
159188
case 'web3_coinbase_wallet_signature':
160189
case 'web3_okx_wallet_signature':
161-
config = { web3WalletId: factor.web3WalletId } as Web3SignatureConfig;
190+
config = { web3WalletId: params.web3WalletId } as Web3SignatureConfig;
162191
break;
163192
case 'reset_password_phone_code':
164-
config = { phoneNumberId: factor.phoneNumberId } as ResetPasswordPhoneCodeFactorConfig;
193+
config = { phoneNumberId: params.phoneNumberId } as ResetPasswordPhoneCodeFactorConfig;
165194
break;
166195
case 'reset_password_email_code':
167-
config = { emailAddressId: factor.emailAddressId } as ResetPasswordEmailCodeFactorConfig;
196+
config = { emailAddressId: params.emailAddressId } as ResetPasswordEmailCodeFactorConfig;
168197
break;
169198
case 'saml':
170199
config = {
171-
redirectUrl: factor.redirectUrl,
172-
actionCompleteRedirectUrl: factor.actionCompleteRedirectUrl,
200+
redirectUrl: params.redirectUrl,
201+
actionCompleteRedirectUrl: params.actionCompleteRedirectUrl,
173202
} as SamlConfig;
174203
break;
175204
case 'enterprise_sso':
176205
config = {
177-
redirectUrl: factor.redirectUrl,
178-
actionCompleteRedirectUrl: factor.actionCompleteRedirectUrl,
179-
oidcPrompt: factor.oidcPrompt,
206+
redirectUrl: params.redirectUrl,
207+
actionCompleteRedirectUrl: params.actionCompleteRedirectUrl,
208+
oidcPrompt: params.oidcPrompt,
180209
} as EnterpriseSSOConfig;
181210
break;
182211
default:
183-
clerkInvalidStrategy('SignIn.prepareFirstFactor', factor.strategy);
212+
clerkInvalidStrategy('SignIn.prepareFirstFactor', params.strategy);
184213
}
185214
return this._basePost({
186-
body: { ...config, strategy: factor.strategy },
215+
body: { ...config, strategy: params.strategy },
187216
action: 'prepare_first_factor',
188217
});
189218
};
190219

191-
attemptFirstFactor = (attemptFactor: AttemptFirstFactorParams): Promise<SignInResource> => {
220+
attemptFirstFactor = (params: AttemptFirstFactorParams): Promise<SignInResource> => {
221+
debugLogger.debug('SignIn.attemptFirstFactor', { id: this.id, strategy: params.strategy });
192222
let config;
193-
switch (attemptFactor.strategy) {
223+
switch (params.strategy) {
194224
case 'passkey':
195225
config = {
196-
publicKeyCredential: JSON.stringify(serializePublicKeyCredentialAssertion(attemptFactor.publicKeyCredential)),
226+
publicKeyCredential: JSON.stringify(serializePublicKeyCredentialAssertion(params.publicKeyCredential)),
197227
};
198228
break;
199229
default:
200-
config = { ...attemptFactor };
230+
config = { ...params };
201231
}
202232

203233
return this._basePost({
204-
body: { ...config, strategy: attemptFactor.strategy },
234+
body: { ...config, strategy: params.strategy },
205235
action: 'attempt_first_factor',
206236
});
207237
};
@@ -243,13 +273,15 @@ export class SignIn extends BaseResource implements SignInResource {
243273
};
244274

245275
prepareSecondFactor = (params: PrepareSecondFactorParams): Promise<SignInResource> => {
276+
debugLogger.debug('SignIn.prepareSecondFactor', { id: this.id, strategy: params.strategy });
246277
return this._basePost({
247278
body: params,
248279
action: 'prepare_second_factor',
249280
});
250281
};
251282

252283
attemptSecondFactor = (params: AttemptSecondFactorParams): Promise<SignInResource> => {
284+
debugLogger.debug('SignIn.attemptSecondFactor', { id: this.id, strategy: params.strategy });
253285
return this._basePost({
254286
body: params,
255287
action: 'attempt_second_factor',

packages/clerk-js/src/core/resources/SignUp.ts

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import type {
3535
Web3Provider,
3636
} from '@clerk/types';
3737

38+
import { debugLogger } from '@/utils/debug';
39+
3840
import {
3941
generateSignatureWithBase,
4042
generateSignatureWithCoinbaseWallet,
@@ -70,10 +72,10 @@ export class SignUp extends BaseResource implements SignUpResource {
7072
pathRoot = '/client/sign_ups';
7173

7274
id: string | undefined;
73-
status: SignUpStatus | null = null;
75+
private _status: SignUpStatus | null = null;
7476
requiredFields: SignUpField[] = [];
75-
optionalFields: SignUpField[] = [];
7677
missingFields: SignUpField[] = [];
78+
optionalFields: SignUpField[] = [];
7779
unverifiedFields: SignUpIdentificationField[] = [];
7880
verifications: SignUpVerifications = new SignUpVerifications(null);
7981
username: string | null = null;
@@ -90,6 +92,31 @@ export class SignUp extends BaseResource implements SignUpResource {
9092
abandonAt: number | null = null;
9193
legalAcceptedAt: number | null = null;
9294

95+
/**
96+
* The current status of the sign-up process.
97+
*
98+
* @returns The current sign-up status, or null if no status has been set
99+
*/
100+
get status(): SignUpStatus | null {
101+
return this._status;
102+
}
103+
104+
/**
105+
* Sets the sign-up status and logs the transition at debug level.
106+
*
107+
* @param value - The new status to set. Can be null to clear the status.
108+
* @remarks When setting a new status that differs from the previous one,
109+
* a debug log entry is created showing the transition from the old to new status.
110+
*/
111+
set status(value: SignUpStatus | null) {
112+
const previousStatus = this._status;
113+
this._status = value;
114+
115+
if (value && previousStatus !== value) {
116+
debugLogger.debug('SignUp.status', { id: this.id, from: previousStatus, to: value });
117+
}
118+
}
119+
93120
/**
94121
* @experimental This experimental API is subject to change.
95122
*
@@ -110,36 +137,43 @@ export class SignUp extends BaseResource implements SignUpResource {
110137
this.fromJSON(data);
111138
}
112139

113-
create = async (_params: SignUpCreateParams): Promise<SignUpResource> => {
114-
let params: Record<string, unknown> = _params;
140+
create = async (params: SignUpCreateParams): Promise<SignUpResource> => {
141+
debugLogger.debug('SignUp.create', { id: this.id, strategy: params.strategy });
142+
143+
let finalParams = { ...params };
115144

116145
if (!__BUILD_DISABLE_RHC__ && !this.clientBypass() && !this.shouldBypassCaptchaForAttempt(params)) {
117146
const captchaChallenge = new CaptchaChallenge(SignUp.clerk);
118147
const captchaParams = await captchaChallenge.managedOrInvisible({ action: 'signup' });
119148
if (!captchaParams) {
120149
throw new ClerkRuntimeError('', { code: 'captcha_unavailable' });
121150
}
122-
params = { ...params, ...captchaParams };
151+
finalParams = { ...finalParams, ...captchaParams };
123152
}
124153

125-
if (params.transfer && this.shouldBypassCaptchaForAttempt(params)) {
126-
params.strategy = SignUp.clerk.client?.signIn.firstFactorVerification.strategy;
154+
if (finalParams.transfer && this.shouldBypassCaptchaForAttempt(finalParams)) {
155+
const strategy = SignUp.clerk.client?.signIn.firstFactorVerification.strategy;
156+
if (strategy) {
157+
finalParams = { ...finalParams, strategy: strategy as SignUpCreateParams['strategy'] };
158+
}
127159
}
128160

129161
return this._basePost({
130162
path: this.pathRoot,
131-
body: normalizeUnsafeMetadata(params),
163+
body: normalizeUnsafeMetadata(finalParams),
132164
});
133165
};
134166

135167
prepareVerification = (params: PrepareVerificationParams): Promise<this> => {
168+
debugLogger.debug('SignUp.prepareVerification', { id: this.id, strategy: params.strategy });
136169
return this._basePost({
137170
body: params,
138171
action: 'prepare_verification',
139172
});
140173
};
141174

142175
attemptVerification = (params: AttemptVerificationParams): Promise<SignUpResource> => {
176+
debugLogger.debug('SignUp.attemptVerification', { id: this.id, strategy: params.strategy });
143177
return this._basePost({
144178
body: params,
145179
action: 'attempt_verification',

0 commit comments

Comments
 (0)