Skip to content

Commit e8eed30

Browse files
committed
update new endpoints and use example
1 parent afa56e9 commit e8eed30

File tree

2 files changed

+81
-53
lines changed

2 files changed

+81
-53
lines changed

examples/react-components/src/app/dashboard/page.tsx

+13-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
OtpVerification,
88
OtpType,
99
} from "@turnkey/sdk-react";
10+
1011
import { server } from "@turnkey/sdk-server";
1112
import { useEffect, useState } from "react";
1213
import "./dashboard.css";
@@ -131,12 +132,12 @@ export default function Dashboard() {
131132
toast.error("Email is already connected to another account");
132133
return;
133134
}
134-
await authIframeClient?.updateUser({
135-
organizationId: suborgId,
135+
136+
await authIframeClient?.addUserAuth({
136137
userId: user.userId,
137-
userEmail: emailInput,
138-
userTagIds: [],
138+
email: emailInput,
139139
});
140+
140141
const sendOtpResponse = await server.sendOtp({
141142
suborgID: suborgId,
142143
otpType: OtpType.Email,
@@ -165,12 +166,12 @@ export default function Dashboard() {
165166
toast.error("Phone Number is already connected to another account");
166167
return;
167168
}
168-
await authIframeClient?.updateUser({
169-
organizationId: suborgId,
169+
170+
await authIframeClient?.addUserAuth({
170171
userId: user.userId,
171-
userPhoneNumber: phoneInput,
172-
userTagIds: [],
172+
phoneNumber: phoneInput,
173173
});
174+
174175
const sendOtpResponse = await server.sendOtp({
175176
suborgID: suborgId,
176177
otpType: OtpType.Sms,
@@ -229,8 +230,8 @@ export default function Dashboard() {
229230
toast.error("Social login is already connected to another account");
230231
return;
231232
}
232-
await authIframeClient?.createOauthProviders({
233-
organizationId: suborgId,
233+
234+
await authIframeClient?.addUserAuth({
234235
userId: user.userId,
235236
oauthProviders: [
236237
{
@@ -260,8 +261,7 @@ export default function Dashboard() {
260261
})) || {};
261262

262263
if (encodedChallenge && attestation) {
263-
await authIframeClient?.createAuthenticators({
264-
organizationId: suborgId,
264+
await authIframeClient?.addUserAuth({
265265
userId: user.userId,
266266
authenticators: [
267267
{
@@ -271,6 +271,7 @@ export default function Dashboard() {
271271
},
272272
],
273273
});
274+
274275
window.location.reload();
275276
}
276277
};

packages/sdk-browser/src/sdk-client.ts

+68-41
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,25 @@ interface UpdateUserAuthParams {
8888
deleteIds?: string[]; // API key IDs to delete
8989
};
9090
}
91+
92+
interface DeleteUserAuthParams {
93+
userId: string; // Unique identifier of the user
94+
phoneNumber?: boolean; // true to remove the phone number
95+
email?: boolean; // true to remove the email
96+
authenticatorIds?: string[]; // Array of authenticator IDs to remove
97+
oauthProviderIds?: string[]; // Array of OAuth provider IDs to remove
98+
apiKeyIds?: string[]; // Array of API key IDs to remove
99+
}
100+
101+
interface AddUserAuthParams {
102+
userId: string; // Unique identifier of the user
103+
phoneNumber?: string; // New phone number to set
104+
email?: string; // New email address to set
105+
authenticators?: Authenticator[]; // Array of authenticator objects to add
106+
oauthProviders?: OauthProvider[]; // Array of OAuth provider objects to add
107+
apiKeys?: ApiKey[]; // Array of API key objects to add
108+
}
109+
91110
export class TurnkeyBrowserSDK {
92111
config: TurnkeySDKBrowserConfig;
93112

@@ -397,43 +416,45 @@ export class TurnkeyBrowserClient extends TurnkeySDKClientBase {
397416
* All removal operations are executed in parallel if multiple
398417
* parameters are provided.
399418
*
400-
* @param userId - Unique identifier of the user
401-
* @param phoneNumber - If true, removes the user's phone number
402-
* @param email - If true, removes the user's email
403-
* @param authenticatorIds - Array of authenticator IDs to remove
404-
* @param oauthProviderIds - Array of OAuth provider IDs to remove
405-
* @param apiKeyIds - Array of API key IDs to remove
419+
* @param params - A structured object containing all the removal parameters
420+
* @param params.userId - Unique identifier of the user
421+
* @param params.phoneNumber - true to remove the phone number
422+
* @param params.email - true to remove the email
423+
* @param params.authenticatorIds - Array of authenticator IDs to remove
424+
* @param params.oauthProviderIds - Array of OAuth provider IDs to remove
425+
* @param params.apiKeyIds - Array of API key IDs to remove
406426
* @returns A promise that resolves to an array of results from each removal operation
407427
*/
408-
deleteUserAuth = async (
409-
userId: string,
410-
phoneNumber?: boolean,
411-
email?: boolean,
412-
authenticatorIds?: string[],
413-
oauthProviderIds?: string[],
414-
apiKeyIds?: string[],
415-
): Promise<any[]> => {
428+
deleteUserAuth = async (params: DeleteUserAuthParams): Promise<any[]> => {
416429
try {
430+
const {
431+
userId,
432+
phoneNumber,
433+
email,
434+
authenticatorIds,
435+
oauthProviderIds,
436+
apiKeyIds,
437+
} = params;
417438
const promises: Promise<any>[] = [];
418439

419440
if (phoneNumber) {
420-
promises.push(this.updateUser({ userId, userPhoneNumber: "" }));
441+
promises.push(
442+
this.updateUser({ userId, userPhoneNumber: "", userTagIds: [] }),
443+
);
421444
}
422-
423445
if (email) {
424-
promises.push(this.updateUser({ userId, userEmail: "" }));
446+
promises.push(
447+
this.updateUser({ userId, userEmail: "", userTagIds: [] }),
448+
);
425449
}
426-
427450
if (authenticatorIds && authenticatorIds.length > 0) {
428451
promises.push(this.deleteAuthenticators({ userId, authenticatorIds }));
429452
}
430-
431453
if (oauthProviderIds && oauthProviderIds.length > 0) {
432454
promises.push(
433455
this.deleteOauthProviders({ userId, providerIds: oauthProviderIds }),
434456
);
435457
}
436-
437458
if (apiKeyIds && apiKeyIds.length > 0) {
438459
promises.push(this.deleteApiKeys({ userId, apiKeyIds }));
439460
}
@@ -459,43 +480,47 @@ export class TurnkeyBrowserClient extends TurnkeySDKClientBase {
459480
* All additions/updates are executed in parallel if multiple
460481
* parameters are provided.
461482
*
462-
* @param userId - Unique identifier of the user
463-
* @param phoneNumber - New phone number for the user
464-
* @param email - New email address for the user
465-
* @param authenticators - Array of authenticator objects to create
466-
* @param oauthProviders - Array of OAuth provider objects to create
467-
* @param apiKeys - Array of API key objects to create
483+
* @param params - A structured object containing all the addition/update parameters
484+
* @param params.userId - Unique identifier of the user
485+
* @param params.phoneNumber - New phone number for the user
486+
* @param params.email - New email address for the user
487+
* @param params.authenticators - Array of authenticator objects to create
488+
* @param params.oauthProviders - Array of OAuth provider objects to create
489+
* @param params.apiKeys - Array of API key objects to create
468490
* @returns A promise that resolves to an array of results from each addition or update
469491
*/
470-
addUserAuth = async (
471-
userId: string,
472-
phoneNumber?: string,
473-
email?: string,
474-
authenticators?: Authenticator[],
475-
oauthProviders?: OauthProvider[],
476-
apiKeys?: ApiKey[],
477-
): Promise<any[]> => {
492+
addUserAuth = async (params: AddUserAuthParams): Promise<any[]> => {
478493
try {
494+
const {
495+
userId,
496+
phoneNumber,
497+
email,
498+
authenticators,
499+
oauthProviders,
500+
apiKeys,
501+
} = params;
479502
const promises: Promise<any>[] = [];
480503

481504
if (phoneNumber) {
482505
promises.push(
483-
this.updateUser({ userId, userPhoneNumber: phoneNumber }),
506+
this.updateUser({
507+
userId,
508+
userPhoneNumber: phoneNumber,
509+
userTagIds: [],
510+
}),
484511
);
485512
}
486-
487513
if (email) {
488-
promises.push(this.updateUser({ userId, userEmail: email }));
514+
promises.push(
515+
this.updateUser({ userId, userEmail: email, userTagIds: [] }),
516+
);
489517
}
490-
491518
if (authenticators && authenticators.length > 0) {
492519
promises.push(this.createAuthenticators({ userId, authenticators }));
493520
}
494-
495521
if (oauthProviders && oauthProviders.length > 0) {
496522
promises.push(this.createOauthProviders({ userId, oauthProviders }));
497523
}
498-
499524
if (apiKeys && apiKeys.length > 0) {
500525
promises.push(this.createApiKeys({ userId, apiKeys }));
501526
}
@@ -551,7 +576,9 @@ export class TurnkeyBrowserClient extends TurnkeySDKClientBase {
551576
userUpdates.userEmail = email === null ? "" : email;
552577
}
553578
if (Object.keys(userUpdates).length > 0) {
554-
promises.push(this.updateUser({ userId, ...userUpdates }));
579+
promises.push(
580+
this.updateUser({ userId, ...userUpdates, userTagIds: [] }),
581+
);
555582
}
556583

557584
// Handle authenticators

0 commit comments

Comments
 (0)