Skip to content

Commit fbc8a74

Browse files
committed
Add API domain override and update OTP endpoint
Introduces an 'api' domain override in domains.ts and updates the OTP authentication flow to use the new API endpoint for initiation. This change improves flexibility for API server configuration and aligns OTP requests with the updated backend route. Closes BLD-220
1 parent f714e5d commit fbc8a74

File tree

2 files changed

+105
-97
lines changed

2 files changed

+105
-97
lines changed

packages/thirdweb/src/utils/domains.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@ type DomainOverrides = {
1414
* @default "embedded-wallet.thirdweb.com"
1515
*/
1616
inAppWallet?: string;
17+
/**
18+
* The base URL for the API server.
19+
* @default "api.thirdweb.com"
20+
*/
21+
api?: string;
1722
/**
1823
* The base URL for the payment server.
1924
* @default "pay.thirdweb.com"
@@ -54,6 +59,7 @@ type DomainOverrides = {
5459
export const DEFAULT_RPC_URL = "rpc.thirdweb.com";
5560
const DEFAULT_SOCIAL_URL = "social.thirdweb.com";
5661
const DEFAULT_IN_APP_WALLET_URL = "embedded-wallet.thirdweb.com";
62+
const DEFAULT_API_URL = "api.thirdweb.com";
5763
const DEFAULT_PAY_URL = "pay.thirdweb.com";
5864
const DEFAULT_STORAGE_URL = "storage.thirdweb.com";
5965
const DEFAULT_BUNDLER_URL = "bundler.thirdweb.com";
@@ -64,6 +70,7 @@ const DEFAULT_BRIDGE_URL = "bridge.thirdweb.com";
6470

6571
let domains: { [k in keyof DomainOverrides]-?: string } = {
6672
analytics: DEFAULT_ANALYTICS_URL,
73+
api: DEFAULT_API_URL,
6774
bridge: DEFAULT_BRIDGE_URL,
6875
bundler: DEFAULT_BUNDLER_URL,
6976
engineCloud: DEFAULT_ENGINE_CLOUD_URL,
@@ -78,6 +85,7 @@ let domains: { [k in keyof DomainOverrides]-?: string } = {
7885
export const setThirdwebDomains = (DomainOverrides: DomainOverrides) => {
7986
domains = {
8087
analytics: DomainOverrides.analytics ?? DEFAULT_ANALYTICS_URL,
88+
api: DomainOverrides.api ?? DEFAULT_API_URL,
8189
bridge: DomainOverrides.bridge ?? DEFAULT_BRIDGE_URL,
8290
bundler: DomainOverrides.bundler ?? DEFAULT_BUNDLER_URL,
8391
engineCloud: DomainOverrides.engineCloud ?? DEFAULT_ENGINE_CLOUD_URL,
Lines changed: 97 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,115 +1,115 @@
11
import type { ThirdwebClient } from "../../../../../client/client.js";
2+
import { getThirdwebBaseUrl } from "../../../../../utils/domains.js";
23
import { stringify } from "../../../../../utils/json.js";
3-
import {
4-
getLoginCallbackUrl,
5-
getLoginUrl,
6-
} from "../../../core/authentication/getLoginPath.js";
4+
import { getLoginCallbackUrl } from "../../../core/authentication/getLoginPath.js";
75
import type {
8-
AuthStoredTokenWithCookieReturnType,
9-
MultiStepAuthArgsType,
10-
PreAuthArgsType,
6+
AuthStoredTokenWithCookieReturnType,
7+
MultiStepAuthArgsType,
8+
PreAuthArgsType,
119
} from "../../../core/authentication/types.js";
1210
import type { Ecosystem } from "../../../core/wallet/types.js";
1311

1412
/**
1513
* @internal
1614
*/
1715
export const sendOtp = async (args: PreAuthArgsType): Promise<void> => {
18-
const { client, ecosystem } = args;
19-
const url = getLoginUrl({ authOption: args.strategy, client, ecosystem });
20-
21-
const headers: Record<string, string> = {
22-
"Content-Type": "application/json",
23-
"x-client-id": client.clientId,
24-
};
25-
26-
if (ecosystem?.id) {
27-
headers["x-ecosystem-id"] = ecosystem.id;
28-
}
29-
30-
if (ecosystem?.partnerId) {
31-
headers["x-ecosystem-partner-id"] = ecosystem.partnerId;
32-
}
33-
34-
const body = (() => {
35-
switch (args.strategy) {
36-
case "email":
37-
return {
38-
email: args.email,
39-
};
40-
case "phone":
41-
return {
42-
phone: args.phoneNumber,
43-
};
44-
}
45-
})();
46-
47-
const response = await fetch(url, {
48-
body: stringify(body),
49-
headers,
50-
method: "POST",
51-
});
52-
53-
if (!response.ok) {
54-
throw new Error("Failed to send verification code");
55-
}
56-
57-
return await response.json();
16+
const { client, ecosystem } = args;
17+
const url = `${getThirdwebBaseUrl("api")}/v1/auth/initiate`;
18+
19+
const headers: Record<string, string> = {
20+
"Content-Type": "application/json",
21+
"x-client-id": client.clientId,
22+
};
23+
24+
if (ecosystem?.id) {
25+
headers["x-ecosystem-id"] = ecosystem.id;
26+
}
27+
28+
if (ecosystem?.partnerId) {
29+
headers["x-ecosystem-partner-id"] = ecosystem.partnerId;
30+
}
31+
32+
const body = (() => {
33+
switch (args.strategy) {
34+
case "email":
35+
return {
36+
type: "email",
37+
email: args.email,
38+
};
39+
case "phone":
40+
return {
41+
type: "phone",
42+
phone: args.phoneNumber,
43+
};
44+
}
45+
})();
46+
47+
const response = await fetch(url, {
48+
body: stringify(body),
49+
headers,
50+
method: "POST",
51+
});
52+
53+
if (!response.ok) {
54+
throw new Error("Failed to send verification code");
55+
}
56+
57+
return await response.json();
5858
};
5959

6060
/**
6161
* @internal
6262
*/
6363
export const verifyOtp = async (
64-
args: MultiStepAuthArgsType & {
65-
client: ThirdwebClient;
66-
ecosystem?: Ecosystem;
67-
},
64+
args: MultiStepAuthArgsType & {
65+
client: ThirdwebClient;
66+
ecosystem?: Ecosystem;
67+
},
6868
): Promise<AuthStoredTokenWithCookieReturnType> => {
69-
const { client, ecosystem } = args;
70-
const url = getLoginCallbackUrl({
71-
authOption: args.strategy,
72-
client: args.client,
73-
ecosystem: args.ecosystem,
74-
});
75-
76-
const headers: Record<string, string> = {
77-
"Content-Type": "application/json",
78-
"x-client-id": client.clientId,
79-
};
80-
81-
if (ecosystem?.id) {
82-
headers["x-ecosystem-id"] = ecosystem.id;
83-
}
84-
85-
if (ecosystem?.partnerId) {
86-
headers["x-ecosystem-partner-id"] = ecosystem.partnerId;
87-
}
88-
89-
const body = (() => {
90-
switch (args.strategy) {
91-
case "email":
92-
return {
93-
code: args.verificationCode,
94-
email: args.email,
95-
};
96-
case "phone":
97-
return {
98-
code: args.verificationCode,
99-
phone: args.phoneNumber,
100-
};
101-
}
102-
})();
103-
104-
const response = await fetch(url, {
105-
body: stringify(body),
106-
headers,
107-
method: "POST",
108-
});
109-
110-
if (!response.ok) {
111-
throw new Error("Failed to verify verification code");
112-
}
113-
114-
return await response.json();
69+
const { client, ecosystem } = args;
70+
const url = getLoginCallbackUrl({
71+
authOption: args.strategy,
72+
client: args.client,
73+
ecosystem: args.ecosystem,
74+
});
75+
76+
const headers: Record<string, string> = {
77+
"Content-Type": "application/json",
78+
"x-client-id": client.clientId,
79+
};
80+
81+
if (ecosystem?.id) {
82+
headers["x-ecosystem-id"] = ecosystem.id;
83+
}
84+
85+
if (ecosystem?.partnerId) {
86+
headers["x-ecosystem-partner-id"] = ecosystem.partnerId;
87+
}
88+
89+
const body = (() => {
90+
switch (args.strategy) {
91+
case "email":
92+
return {
93+
code: args.verificationCode,
94+
email: args.email,
95+
};
96+
case "phone":
97+
return {
98+
code: args.verificationCode,
99+
phone: args.phoneNumber,
100+
};
101+
}
102+
})();
103+
104+
const response = await fetch(url, {
105+
body: stringify(body),
106+
headers,
107+
method: "POST",
108+
});
109+
110+
if (!response.ok) {
111+
throw new Error("Failed to verify verification code");
112+
}
113+
114+
return await response.json();
115115
};

0 commit comments

Comments
 (0)