|
1 | 1 | import type { ThirdwebClient } from "../../../../../client/client.js"; |
| 2 | +import { getThirdwebBaseUrl } from "../../../../../utils/domains.js"; |
2 | 3 | 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"; |
7 | 5 | import type { |
8 | | - AuthStoredTokenWithCookieReturnType, |
9 | | - MultiStepAuthArgsType, |
10 | | - PreAuthArgsType, |
| 6 | + AuthStoredTokenWithCookieReturnType, |
| 7 | + MultiStepAuthArgsType, |
| 8 | + PreAuthArgsType, |
11 | 9 | } from "../../../core/authentication/types.js"; |
12 | 10 | import type { Ecosystem } from "../../../core/wallet/types.js"; |
13 | 11 |
|
14 | 12 | /** |
15 | 13 | * @internal |
16 | 14 | */ |
17 | 15 | 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(); |
58 | 58 | }; |
59 | 59 |
|
60 | 60 | /** |
61 | 61 | * @internal |
62 | 62 | */ |
63 | 63 | export const verifyOtp = async ( |
64 | | - args: MultiStepAuthArgsType & { |
65 | | - client: ThirdwebClient; |
66 | | - ecosystem?: Ecosystem; |
67 | | - }, |
| 64 | + args: MultiStepAuthArgsType & { |
| 65 | + client: ThirdwebClient; |
| 66 | + ecosystem?: Ecosystem; |
| 67 | + }, |
68 | 68 | ): 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(); |
115 | 115 | }; |
0 commit comments