1- import { getCookieValue , setCookies , prepareSetCookie , prepareKillCookie } from '../helpers/nodeCookieHelpers'
1+ import {
2+ getCookieValue ,
3+ setCookies ,
4+ prepareSetCookie ,
5+ prepareKillCookie ,
6+ } from '../helpers/nodeCookieHelpers'
27import obtainSession from './guest/obtainSession'
38import guestCart from './guest/cart'
49import customerCart from './customer/cart'
510import { COOKIES } from '../constants'
6- import Session from 'react-storefront-connector/Session'
711
8- export default async function session ( req , res ) : Promise < Session > {
9- const cookiesToSet = [ ] ;
12+ export default async function session ( req , res ) : Promise < any > {
13+ const cookiesToSet = [ ]
1014
1115 // ### 1 - LOGGED IN SESSION
1216 const tokenCookieValue = getCookieValue ( req , COOKIES . M2_CUSTOMER_TOKEN )
1317 if ( tokenCookieValue ) {
1418 const customerCartData = await customerCart ( tokenCookieValue )
1519 if ( customerCartData . error ) {
16- return res . status ( 400 ) . json ( {
20+ return {
1721 error : customerCartData . error ,
18- } )
22+ }
1923 }
2024 const { cart, customerCartId } = customerCartData
21- cookiesToSet . push ( prepareSetCookie ( COOKIES . M2_CUSTOMER_TOKEN , tokenCookieValue , { maxAge : 3600 * 24 * 30 } ) ) // renew customer token cookie for 30 more days
22- cookiesToSet . push ( prepareSetCookie ( COOKIES . M2_CUSTOMER_CART_ID , customerCartId , { maxAge : 3600 * 24 * 30 } ) ) // set/renew customer cart ID cookie for 30 days
25+ cookiesToSet . push (
26+ prepareSetCookie ( COOKIES . M2_CUSTOMER_TOKEN , tokenCookieValue , { maxAge : 3600 * 24 * 30 } )
27+ ) // renew customer token cookie for 30 more days
28+ cookiesToSet . push (
29+ prepareSetCookie ( COOKIES . M2_CUSTOMER_CART_ID , customerCartId , { maxAge : 3600 * 24 * 30 } )
30+ ) // set/renew customer cart ID cookie for 30 days
2331 cookiesToSet . push ( prepareKillCookie ( COOKIES . M2_GUEST_CART_ID ) ) // kill guest cart ID cookie just in case (prevents possible cart merges issues)
2432 setCookies ( res , cookiesToSet )
25- return res . status ( 200 ) . json ( {
33+ return {
2634 signedIn : true ,
2735 cart,
28- } )
36+ }
2937 }
3038
3139 // ### 2 - GUEST SESSION
@@ -36,34 +44,38 @@ export default async function session(req, res): Promise<Session> {
3644 const guestCartData = await guestCart ( guestCartIdCookieValue )
3745 if ( guestCartData . error ) {
3846 setCookies ( res , cookiesToSet )
39- return res . status ( 400 ) . json ( {
47+ return {
4048 error : guestCartData . error ,
41- } )
49+ }
4250 }
4351 const { cart } = guestCartData
44- cookiesToSet . push ( prepareSetCookie ( COOKIES . M2_GUEST_CART_ID , guestCartIdCookieValue , { maxAge : 3600 * 24 * 7 } ) ) // renew cookie for 7 more days
52+ cookiesToSet . push (
53+ prepareSetCookie ( COOKIES . M2_GUEST_CART_ID , guestCartIdCookieValue , { maxAge : 3600 * 24 * 7 } )
54+ ) // renew cookie for 7 more days
4555 setCookies ( res , cookiesToSet )
46- return res . status ( 200 ) . json ( {
56+ return {
4757 signedIn : false ,
4858 cart,
49- } )
59+ }
5060 }
5161
5262 // # 2.2 - Obtain new guest session
5363 const obtainSessionData = await obtainSession ( )
5464 if ( obtainSessionData . error ) {
5565 setCookies ( res , cookiesToSet )
56- return res . status ( 400 ) . json ( {
66+ return {
5767 error : obtainSessionData . error ,
58- } )
68+ }
5969 }
6070 const { guestCartId } = obtainSessionData
61- cookiesToSet . push ( prepareSetCookie ( COOKIES . M2_GUEST_CART_ID , guestCartId , { maxAge : 3600 * 24 * 7 } ) ) // set guest cart id cookie for 7 days
71+ cookiesToSet . push (
72+ prepareSetCookie ( COOKIES . M2_GUEST_CART_ID , guestCartId , { maxAge : 3600 * 24 * 7 } )
73+ ) // set guest cart id cookie for 7 days
6274 setCookies ( res , cookiesToSet )
63- return res . status ( 200 ) . json ( {
75+ return {
6476 signedIn : false ,
6577 cart : {
6678 items : [ ] ,
6779 } ,
68- } )
80+ }
6981}
0 commit comments