Skip to content

Commit a4e3c12

Browse files
committed
refactor: update custom auth to not route through iframe
1 parent 1557db3 commit a4e3c12

File tree

4 files changed

+42
-38
lines changed

4 files changed

+42
-38
lines changed

packages/thirdweb/src/wallets/in-app/core/authentication/authEndpoint.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
import type { ThirdwebClient } from "../../../../client/client.js";
2-
import { getSessionHeaders } from "../../native/helpers/api/fetchers.js";
2+
import { getClientFetch } from "../../../../utils/fetch.js";
33
import { ROUTE_AUTH_ENDPOINT_CALLBACK } from "../../native/helpers/constants.js";
44
import { createErrorMessage } from "../../native/helpers/errors.js";
5-
import type { ClientScopedStorage } from "./client-scoped-storage.js";
5+
import type { Ecosystem } from "../wallet/types.js";
66
import type { AuthStoredTokenWithCookieReturnType } from "./types.js";
77

88
export async function authEndpoint(args: {
99
payload: string;
1010
client: ThirdwebClient;
11-
storage: ClientScopedStorage;
11+
ecosystem?: Ecosystem;
1212
}): Promise<AuthStoredTokenWithCookieReturnType> {
13-
const resp = await fetch(ROUTE_AUTH_ENDPOINT_CALLBACK, {
13+
const clientFetch = getClientFetch(args.client, args.ecosystem);
14+
15+
const res = await clientFetch(ROUTE_AUTH_ENDPOINT_CALLBACK, {
1416
method: "POST",
1517
headers: {
16-
...getSessionHeaders(),
18+
"Content-Type": "application/json",
1719
},
1820
body: JSON.stringify({
1921
payload: args.payload,
2022
developerClientId: args.client.clientId,
2123
}),
2224
});
23-
if (!resp.ok) {
24-
const error = await resp.json();
25+
26+
if (!res.ok) {
27+
const error = await res.json();
2528
throw new Error(
2629
`Custom auth endpoint authentication error: ${error.message}`,
2730
);
2831
}
2932

3033
try {
31-
const { verifiedToken } = await resp.json();
34+
const { verifiedToken } = await res.json();
3235

3336
return { storedToken: verifiedToken };
3437
} catch (e) {

packages/thirdweb/src/wallets/in-app/core/authentication/jwt.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,36 @@
1+
import { getClientFetch } from "src/utils/fetch.js";
12
import type { ThirdwebClient } from "../../../../client/client.js";
2-
import { getSessionHeaders } from "../../native/helpers/api/fetchers.js";
33
import { ROUTE_AUTH_JWT_CALLBACK } from "../../native/helpers/constants.js";
44
import { createErrorMessage } from "../../native/helpers/errors.js";
5-
import type { ClientScopedStorage } from "./client-scoped-storage.js";
5+
import type { Ecosystem } from "../wallet/types.js";
66
import type { AuthStoredTokenWithCookieReturnType } from "./types.js";
77

88
export async function customJwt(args: {
99
jwt: string;
1010
client: ThirdwebClient;
11-
storage: ClientScopedStorage;
11+
ecosystem?: Ecosystem;
1212
}): Promise<AuthStoredTokenWithCookieReturnType> {
13-
const resp = await fetch(ROUTE_AUTH_JWT_CALLBACK, {
13+
const clientFetch = getClientFetch(args.client, args.ecosystem);
14+
15+
const res = await clientFetch(ROUTE_AUTH_JWT_CALLBACK, {
1416
method: "POST",
1517
headers: {
16-
...getSessionHeaders(),
18+
"Content-Type": "application/json",
1719
},
1820
body: JSON.stringify({
1921
jwt: args.jwt,
2022
developerClientId: args.client.clientId,
2123
}),
2224
});
2325

24-
if (!resp.ok) {
25-
const error = await resp.json();
26+
if (!res.ok) {
27+
const error = await res.json();
2628
throw new Error(`JWT authentication error: ${error.message}`);
2729
}
2830

2931
try {
30-
const { verifiedToken } = await resp.json();
32+
const { verifiedToken } = await res.json();
33+
3134
return { storedToken: verifiedToken };
3235
} catch (e) {
3336
throw new Error(

packages/thirdweb/src/wallets/in-app/native/native-connector.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,11 @@ export class InAppNativeConnector implements InAppConnector {
199199
return customJwt({
200200
jwt: params.jwt,
201201
client: this.client,
202-
storage: this.storage,
203202
});
204203
case "auth_endpoint":
205204
return authEndpoint({
206205
payload: params.payload,
207206
client: this.client,
208-
storage: this.storage,
209207
});
210208
default:
211209
throw new Error(`Unsupported authentication type: ${strategy}`);

packages/thirdweb/src/wallets/in-app/web/lib/web-connector.ts

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@ import { webLocalStorage } from "../../../../utils/storage/webStorage.js";
44
import type { SocialAuthOption } from "../../../../wallets/types.js";
55
import type { Account } from "../../../interfaces/wallet.js";
66
import { getUserStatus } from "../../core/actions/get-enclave-user-status.js";
7+
import { authEndpoint } from "../../core/authentication/authEndpoint.js";
78
import { ClientScopedStorage } from "../../core/authentication/client-scoped-storage.js";
89
import { guestAuthenticate } from "../../core/authentication/guest.js";
10+
import { customJwt } from "../../core/authentication/jwt.js";
911
import {
1012
getLinkedProfilesInternal,
1113
linkAccount,
@@ -268,8 +270,11 @@ export class InAppWebConnector implements InAppConnector {
268270
});
269271
}
270272

271-
async loginWithAuthToken(authResult: AuthStoredTokenWithCookieReturnType) {
272-
return this.auth.loginWithAuthToken(authResult);
273+
async loginWithAuthToken(
274+
authResult: AuthStoredTokenWithCookieReturnType,
275+
recoveryCode?: string,
276+
) {
277+
return this.auth.loginWithAuthToken(authResult, recoveryCode);
273278
}
274279

275280
/**
@@ -292,20 +297,22 @@ export class InAppWebConnector implements InAppConnector {
292297
client: this.client,
293298
ecosystem: this.ecosystem,
294299
});
300+
case "auth_endpoint": {
301+
return authEndpoint({
302+
payload: args.payload,
303+
client: this.client,
304+
ecosystem: this.ecosystem,
305+
});
306+
}
295307
case "jwt":
296-
return this.auth.authenticateWithCustomJwt({
308+
return customJwt({
297309
jwt: args.jwt,
298-
encryptionKey: args.encryptionKey,
310+
client: this.client,
311+
ecosystem: this.ecosystem,
299312
});
300313
case "passkey": {
301314
return this.passkeyAuth(args);
302315
}
303-
case "auth_endpoint": {
304-
return this.auth.authenticateWithCustomAuthEndpoint({
305-
payload: args.payload,
306-
encryptionKey: args.encryptionKey,
307-
});
308-
}
309316
case "iframe_email_verification": {
310317
return this.auth.authenticateWithIframe({
311318
email: args.email,
@@ -359,17 +366,10 @@ export class InAppWebConnector implements InAppConnector {
359366
): Promise<AuthLoginReturnType> {
360367
const strategy = args.strategy;
361368
switch (strategy) {
369+
case "auth_endpoint":
362370
case "jwt": {
363-
return this.auth.loginWithCustomJwt({
364-
jwt: args.jwt,
365-
encryptionKey: args.encryptionKey,
366-
});
367-
}
368-
case "auth_endpoint": {
369-
return this.auth.loginWithCustomAuthEndpoint({
370-
payload: args.payload,
371-
encryptionKey: args.encryptionKey,
372-
});
371+
const authToken = await this.authenticate(args);
372+
return await this.loginWithAuthToken(authToken, args.encryptionKey);
373373
}
374374
case "iframe_email_verification": {
375375
return this.auth.loginWithIframe({

0 commit comments

Comments
 (0)