@@ -13,12 +13,15 @@ import {
13
13
} from "@remix-run/node" ;
14
14
import {
15
15
type CoreDetails ,
16
+ CoreDetailsDocument ,
16
17
CoreEnabledFeaturesDocument ,
17
18
GetPresignedS3UrlDocument ,
18
19
PresignedPutS3UrlDocument ,
20
+ UserCollectionsListDocument ,
19
21
type UserCollectionsListQuery ,
20
22
type UserLot ,
21
23
type UserPreferences ,
24
+ UserPreferencesDocument ,
22
25
} from "@ryot/generated/graphql/backend/graphql" ;
23
26
import { UserDetailsDocument } from "@ryot/generated/graphql/backend/graphql" ;
24
27
import { GraphQLClient } from "graphql-request" ;
@@ -40,8 +43,14 @@ const getAuthorizationCookie = async (request: Request) => {
40
43
return cookie ;
41
44
} ;
42
45
43
- export const getAuthorizationHeader = async ( request : Request ) => {
44
- const cookie = await getAuthorizationCookie ( request ) ;
46
+ export const getAuthorizationHeader = async (
47
+ request ?: Request ,
48
+ token ?: string ,
49
+ ) => {
50
+ let cookie : string ;
51
+ if ( request ) cookie = await getAuthorizationCookie ( request ) ;
52
+ else if ( token ) cookie = token ;
53
+ else cookie = "" ;
45
54
return { Authorization : `Bearer ${ cookie } ` } ;
46
55
} ;
47
56
@@ -147,6 +156,15 @@ export const processSubmission = <Schema extends ZodTypeAny>(
147
156
return submission . value ;
148
157
} ;
149
158
159
+ export const getUserCollectionsList = async ( request : Request ) => {
160
+ const { userCollectionsList } = await gqlClient . request (
161
+ UserCollectionsListDocument ,
162
+ { } ,
163
+ await getAuthorizationHeader ( request ) ,
164
+ ) ;
165
+ return userCollectionsList ;
166
+ } ;
167
+
150
168
export const getLogoutCookies = async ( ) => {
151
169
return combineHeaders (
152
170
{
@@ -169,11 +187,6 @@ export const getLogoutCookies = async () => {
169
187
expires : new Date ( 0 ) ,
170
188
} ) ,
171
189
} ,
172
- {
173
- "Set-Cookie" : await userCollectionsListCookie . serialize ( "" , {
174
- expires : new Date ( 0 ) ,
175
- } ) ,
176
- } ,
177
190
) ;
178
191
} ;
179
192
@@ -256,7 +269,6 @@ export const getCoreDetails = async (request: Request) => {
256
269
const details = await coreDetailsCookie . parse (
257
270
request . headers . get ( "cookie" ) || "" ,
258
271
) ;
259
- redirectIfDetailNotPresent ( request , details ) ;
260
272
return details as CoreDetails ;
261
273
} ;
262
274
@@ -265,7 +277,6 @@ export const getUserPreferences = async (request: Request) => {
265
277
const prefs = await userPreferencesCookie . parse (
266
278
request . headers . get ( "cookie" ) || "" ,
267
279
) ;
268
- redirectIfDetailNotPresent ( request , prefs ) ;
269
280
return prefs as UserPreferences ;
270
281
} ;
271
282
@@ -274,28 +285,9 @@ export const getUserDetails = async (request: Request) => {
274
285
const details = await userDetailsCookie . parse (
275
286
request . headers . get ( "cookie" ) || "" ,
276
287
) ;
277
- redirectIfDetailNotPresent ( request , details ) ;
278
288
return details as ApplicationUser ;
279
289
} ;
280
290
281
- export const getUserCollectionsList = async ( request : Request ) => {
282
- await redirectIfNotAuthenticated ( request ) ;
283
- const list = await userCollectionsListCookie . parse (
284
- request . headers . get ( "cookie" ) || "" ,
285
- ) ;
286
- redirectIfDetailNotPresent ( request , list ) ;
287
- return list as UserCollectionsListQuery [ "userCollectionsList" ] ;
288
- } ;
289
-
290
- const redirectIfDetailNotPresent = ( request : Request , detail : unknown ) => {
291
- if ( ! detail )
292
- throw redirect (
293
- withQuery ( $path ( "/actions" ) , {
294
- [ redirectToQueryParam ] : withoutHost ( request . url ) ,
295
- } ) ,
296
- ) ;
297
- } ;
298
-
299
291
const envVariables = expectedEnvironmentVariables . parse ( process . env ) ;
300
292
301
293
const commonCookieOptions = {
@@ -329,11 +321,6 @@ export const userDetailsCookie = createCookie(
329
321
commonCookieOptions ,
330
322
) ;
331
323
332
- export const userCollectionsListCookie = createCookie (
333
- ApplicationKey . UserCollectionsList ,
334
- commonCookieOptions ,
335
- ) ;
336
-
337
324
export const toastSessionStorage = createCookieSessionStorage ( {
338
325
cookie : { ...commonCookieOptions , name : ApplicationKey . Toast } ,
339
326
} ) ;
@@ -392,3 +379,38 @@ export async function getToast(request: Request) {
392
379
: null ,
393
380
} ;
394
381
}
382
+
383
+ export const getCookiesForApplication = async ( token : string ) => {
384
+ const [ { coreDetails } , { userPreferences } , { userDetails } ] =
385
+ await Promise . all ( [
386
+ gqlClient . request ( CoreDetailsDocument ) ,
387
+ gqlClient . request (
388
+ UserPreferencesDocument ,
389
+ undefined ,
390
+ await getAuthorizationHeader ( undefined , token ) ,
391
+ ) ,
392
+ gqlClient . request (
393
+ UserDetailsDocument ,
394
+ undefined ,
395
+ await getAuthorizationHeader ( undefined , token ) ,
396
+ ) ,
397
+ ] ) ;
398
+ const cookieMaxAge = coreDetails . tokenValidForDays * 24 * 60 * 60 ;
399
+ return combineHeaders (
400
+ {
401
+ "Set-Cookie" : await coreDetailsCookie . serialize ( coreDetails , {
402
+ maxAge : cookieMaxAge ,
403
+ } ) ,
404
+ } ,
405
+ {
406
+ "Set-Cookie" : await userPreferencesCookie . serialize ( userPreferences , {
407
+ maxAge : cookieMaxAge ,
408
+ } ) ,
409
+ } ,
410
+ {
411
+ "Set-Cookie" : await userDetailsCookie . serialize ( userDetails , {
412
+ maxAge : cookieMaxAge ,
413
+ } ) ,
414
+ } ,
415
+ ) ;
416
+ } ;
0 commit comments