@@ -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