Skip to content

Commit c8a9d02

Browse files
committed
Update auth-handler.ts
1 parent 803b225 commit c8a9d02

File tree

3 files changed

+37
-21
lines changed

3 files changed

+37
-21
lines changed

packages/thirdweb/.size-limit.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
{
33
"name": "thirdweb (esm)",
44
"path": "./dist/esm/exports/thirdweb.js",
5-
"limit": "60 kB",
5+
"limit": "90 kB",
66
"import": "*"
77
},
88
{
99
"name": "thirdweb (cjs)",
1010
"path": "./dist/cjs/exports/thirdweb.js",
11-
"limit": "350 kB"
11+
"limit": "375 kB"
1212
},
1313
{
1414
"name": "thirdweb (minimal + tree-shaking)",

packages/thirdweb/src/login/client/login.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Chain } from "../../chains/types.js";
22
import type { ThirdwebClient } from "../../client/client.js";
33
import type { PreparedTransaction } from "../../transaction/prepare-transaction.js";
4+
import { getAddress } from "../../utils/address.js";
45
import type { AsyncStorage } from "../../utils/storage/AsyncStorage.js";
56
import { inAppWallet } from "../../wallets/in-app/web/in-app.js";
67
import type { Account, Wallet } from "../../wallets/interfaces/wallet.js";
@@ -121,8 +122,6 @@ export async function login(loginOptions: LoginOptions) {
121122
};
122123
}
123124

124-
// google login
125-
126125
// wallet login
127126
case "wallet": {
128127
const account = await IAW.connect({
@@ -146,10 +145,10 @@ export async function login(loginOptions: LoginOptions) {
146145

147146
return mapAccount(account, IAW, loginOptions.baseURL);
148147
}
148+
149+
throw new Error(`Invalid login type: ${loginOptions.type}`);
149150
}
150151
}
151-
152-
throw new Error("Invalid login type");
153152
}
154153

155154
function mapAccount(
@@ -205,11 +204,14 @@ function mapAccount(
205204
: undefined,
206205
});
207206
// if the JWT is valid, we can simply return it
208-
if (data?.address === account.address) {
207+
if (
208+
data?.address &&
209+
getAddress(data.address) === getAddress(account.address)
210+
) {
209211
// set the JWT in the local state
210212
jwt_cache = {
211213
jwt: data.jwt,
212-
expiresAt: data.expiresAt,
214+
expiresAt: new Date(data.expiresAt),
213215
};
214216
// return the JWT
215217
return data.jwt;
@@ -265,7 +267,7 @@ function mapAccount(
265267
// set the jwt cache
266268
jwt_cache = {
267269
jwt: loginResponse.data.jwt,
268-
expiresAt: loginResponse.data.expiresAt,
270+
expiresAt: new Date(loginResponse.data.expiresAt),
269271
};
270272
return loginResponse.data.jwt;
271273
},

packages/thirdweb/src/login/server/auth-handler.ts

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export function createAuthHandler({
7070
basePath = "/api/auth",
7171
...options
7272
}: CreateAuthHandlerOptions) {
73-
// re-map the server wallet to to the admin account option
73+
// re-map the server wallet to the admin account option
7474
const twAuth = createAuth({ ...options, adminAccount: serverWallet });
7575

7676
// payload generation endpoint
@@ -80,14 +80,13 @@ export function createAuthHandler({
8080
method: "GET",
8181
query: z.object({
8282
address: z.string().refine(isAddress, "Invalid address"),
83-
chainId: z.number().optional(),
83+
chainId: z.coerce.number().optional(),
8484
}),
8585
},
8686
(ctx) => {
87-
const { address, chainId } = ctx.query;
8887
return twAuth.generatePayload({
89-
address,
90-
chainId: chainId ? Number(chainId) : undefined,
88+
address: ctx.query.address,
89+
chainId: ctx.query.chainId,
9190
});
9291
},
9392
);
@@ -127,23 +126,38 @@ export function createAuthHandler({
127126
// construct the JWT
128127
const jwt = await twAuth.generateJWT({ payload: result.payload });
129128

130-
const expiresAt = new Date(decodeJWT(jwt).payload.exp * 1000);
129+
const decodedJWT = decodeJWT(jwt);
130+
const expTime =
131+
typeof decodedJWT.payload.exp === "string"
132+
? Number.parseInt(decodedJWT.payload.exp, 10)
133+
: decodedJWT.payload.exp;
134+
135+
if (!expTime || Number.isNaN(expTime)) {
136+
throw ctx.error(500, {
137+
message: "Invalid JWT expiration time",
138+
});
139+
}
140+
141+
const expiresAt = new Date(expTime * 1000);
142+
const thirtyDaysInSeconds = 60 * 60 * 24 * 30;
143+
const maxAgeInSeconds = Math.min(
144+
thirtyDaysInSeconds,
145+
Math.floor((expiresAt.getTime() - Date.now()) / 1000),
146+
);
131147

132148
// try to set the JWT on the client's cookies
133149
ctx.setCookie("tw:jwt", jwt, {
134150
httpOnly: true,
135151
secure: true,
136152
sameSite: "lax",
137-
maxAge: 60 * 60 * 24 * 30, // 30 days by default
138-
// set the expiration date to the expiration time of the JWT, no point in setting it for longer
153+
maxAge: maxAgeInSeconds,
139154
expires: expiresAt,
140155
});
141156

142157
// return the constructed JWT
143158
return {
144159
jwt,
145-
// have to decode it again to get the expiration time (lul)
146-
expiresAt,
160+
expiresAt: expiresAt.toISOString(),
147161
};
148162
},
149163
);
@@ -158,7 +172,7 @@ export function createAuthHandler({
158172
let [type, token] = ctx.headers.get("authorization")?.split(" ") ?? [];
159173

160174
// if the token is set but the type is not Bearer, return a 401 error
161-
if (token && type !== "Bearer") {
175+
if (token && (!type || type !== "Bearer")) {
162176
throw ctx.error(401, {
163177
message: "Invalid authorization header",
164178
});
@@ -185,7 +199,7 @@ export function createAuthHandler({
185199
return {
186200
address: result.parsedJWT.aud,
187201
jwt: token,
188-
expiresAt: new Date(result.parsedJWT.exp * 1000),
202+
expiresAt: new Date(result.parsedJWT.exp * 1000).toISOString(),
189203
};
190204
},
191205
);

0 commit comments

Comments
 (0)