Skip to content

Commit 49c465c

Browse files
committed
Update auth-handler.ts
1 parent 00edc15 commit 49c465c

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

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

Lines changed: 7 additions & 3 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";
@@ -205,11 +206,14 @@ function mapAccount(
205206
: undefined,
206207
});
207208
// if the JWT is valid, we can simply return it
208-
if (data?.address === account.address) {
209+
if (
210+
data?.address &&
211+
getAddress(data.address) === getAddress(account.address)
212+
) {
209213
// set the JWT in the local state
210214
jwt_cache = {
211215
jwt: data.jwt,
212-
expiresAt: data.expiresAt,
216+
expiresAt: new Date(data.expiresAt),
213217
};
214218
// return the JWT
215219
return data.jwt;
@@ -265,7 +269,7 @@ function mapAccount(
265269
// set the jwt cache
266270
jwt_cache = {
267271
jwt: loginResponse.data.jwt,
268-
expiresAt: loginResponse.data.expiresAt,
272+
expiresAt: new Date(loginResponse.data.expiresAt),
269273
};
270274
return loginResponse.data.jwt;
271275
},

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

Lines changed: 25 additions & 11 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
);
@@ -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)