@@ -70,7 +70,7 @@ export function createAuthHandler({
70
70
basePath = "/api/auth" ,
71
71
...options
72
72
} : CreateAuthHandlerOptions ) {
73
- // re-map the server wallet to to the admin account option
73
+ // re-map the server wallet to the admin account option
74
74
const twAuth = createAuth ( { ...options , adminAccount : serverWallet } ) ;
75
75
76
76
// payload generation endpoint
@@ -80,14 +80,13 @@ export function createAuthHandler({
80
80
method : "GET" ,
81
81
query : z . object ( {
82
82
address : z . string ( ) . refine ( isAddress , "Invalid address" ) ,
83
- chainId : z . number ( ) . optional ( ) ,
83
+ chainId : z . coerce . number ( ) . optional ( ) ,
84
84
} ) ,
85
85
} ,
86
86
( ctx ) => {
87
- const { address, chainId } = ctx . query ;
88
87
return twAuth . generatePayload ( {
89
- address,
90
- chainId : chainId ? Number ( chainId ) : undefined ,
88
+ address : ctx . query . address ,
89
+ chainId : ctx . query . chainId ,
91
90
} ) ;
92
91
} ,
93
92
) ;
@@ -127,23 +126,38 @@ export function createAuthHandler({
127
126
// construct the JWT
128
127
const jwt = await twAuth . generateJWT ( { payload : result . payload } ) ;
129
128
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
+ ) ;
131
147
132
148
// try to set the JWT on the client's cookies
133
149
ctx . setCookie ( "tw:jwt" , jwt , {
134
150
httpOnly : true ,
135
151
secure : true ,
136
152
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 ,
139
154
expires : expiresAt ,
140
155
} ) ;
141
156
142
157
// return the constructed JWT
143
158
return {
144
159
jwt,
145
- // have to decode it again to get the expiration time (lul)
146
- expiresAt,
160
+ expiresAt : expiresAt . toISOString ( ) ,
147
161
} ;
148
162
} ,
149
163
) ;
@@ -185,7 +199,7 @@ export function createAuthHandler({
185
199
return {
186
200
address : result . parsedJWT . aud ,
187
201
jwt : token ,
188
- expiresAt : new Date ( result . parsedJWT . exp * 1000 ) ,
202
+ expiresAt : new Date ( result . parsedJWT . exp * 1000 ) . toISOString ( ) ,
189
203
} ;
190
204
} ,
191
205
) ;
0 commit comments