@@ -23,6 +23,7 @@ import setCookie from "set-cookie-parser"
23
23
import cookieParser from "cookie-parser"
24
24
import { logoutCookie } from "@app/auth"
25
25
import { checkedToPhoneNumber } from "@domain/users"
26
+ import libCookie from "cookie"
26
27
27
28
const authRouter = express . Router ( { caseSensitive : true } )
28
29
@@ -170,21 +171,37 @@ authRouter.post(
170
171
. status ( 500 )
171
172
. send ( { error : "Missing csrf or ory_kratos_session cookie" } )
172
173
}
174
+ const kratosCookieStr = libCookie . serialize (
175
+ kratosSessionCookie . name ,
176
+ kratosSessionCookie . value ,
177
+ {
178
+ expires : kratosSessionCookie . expires ,
179
+ maxAge : kratosSessionCookie . maxAge ,
180
+ sameSite : "none" ,
181
+ secure : kratosSessionCookie . secure ,
182
+ httpOnly : kratosSessionCookie . httpOnly ,
183
+ path : kratosSessionCookie . path ,
184
+ } ,
185
+ )
186
+ const session = await kratosPublic . toSession ( { cookie : kratosCookieStr } )
187
+ const thirtyDaysFromNow = new Date ( new Date ( ) . setDate ( new Date ( ) . getDate ( ) + 30 ) )
188
+ const expiresAt = session . data . expires_at
189
+ ? new Date ( session . data . expires_at )
190
+ : thirtyDaysFromNow
191
+ const maxAge = expiresAt . getTime ( ) - new Date ( ) . getTime ( )
173
192
res . cookie ( kratosSessionCookie . name , kratosSessionCookie . value , {
174
- maxAge : kratosSessionCookie . maxAge ,
193
+ maxAge,
175
194
sameSite : "none" ,
176
195
secure : kratosSessionCookie . secure ,
177
196
httpOnly : kratosSessionCookie . httpOnly ,
178
197
path : kratosSessionCookie . path ,
179
- expires : kratosSessionCookie . expires ,
180
198
} )
181
199
res . cookie ( csrfCookie . name , csrfCookie . value , {
182
- maxAge : csrfCookie . maxAge ,
200
+ maxAge,
183
201
sameSite : "none" ,
184
202
secure : csrfCookie . secure ,
185
203
httpOnly : csrfCookie . httpOnly ,
186
204
path : csrfCookie . path ,
187
- expires : csrfCookie . expires ,
188
205
} )
189
206
} catch ( err ) {
190
207
recordExceptionInCurrentSpan ( { error : err } )
0 commit comments