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'
611import Session from 'react-storefront-connector/Session'
712
813export default async function session ( req , res ) : Promise < Session > {
9- const cookiesToSet = [ ] ;
14+ const cookiesToSet = [ ]
1015
1116 // ### 1 - LOGGED IN SESSION
1217 const tokenCookieValue = getCookieValue ( req , COOKIES . M2_CUSTOMER_TOKEN )
1318 if ( tokenCookieValue ) {
1419 const customerCartData = await customerCart ( tokenCookieValue )
1520 if ( customerCartData . error ) {
16- return res . status ( 400 ) . json ( {
21+ return {
1722 error : customerCartData . error ,
18- } )
23+ }
1924 }
2025 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
26+ cookiesToSet . push (
27+ prepareSetCookie ( COOKIES . M2_CUSTOMER_TOKEN , tokenCookieValue , { maxAge : 3600 * 24 * 30 } )
28+ ) // renew customer token cookie for 30 more days
29+ cookiesToSet . push (
30+ prepareSetCookie ( COOKIES . M2_CUSTOMER_CART_ID , customerCartId , { maxAge : 3600 * 24 * 30 } )
31+ ) // set/renew customer cart ID cookie for 30 days
2332 cookiesToSet . push ( prepareKillCookie ( COOKIES . M2_GUEST_CART_ID ) ) // kill guest cart ID cookie just in case (prevents possible cart merges issues)
2433 setCookies ( res , cookiesToSet )
25- return res . status ( 200 ) . json ( {
34+ return {
2635 signedIn : true ,
2736 cart,
28- } )
37+ }
2938 }
3039
3140 // ### 2 - GUEST SESSION
@@ -36,34 +45,38 @@ export default async function session(req, res): Promise<Session> {
3645 const guestCartData = await guestCart ( guestCartIdCookieValue )
3746 if ( guestCartData . error ) {
3847 setCookies ( res , cookiesToSet )
39- return res . status ( 400 ) . json ( {
48+ return {
4049 error : guestCartData . error ,
41- } )
50+ }
4251 }
4352 const { cart } = guestCartData
44- cookiesToSet . push ( prepareSetCookie ( COOKIES . M2_GUEST_CART_ID , guestCartIdCookieValue , { maxAge : 3600 * 24 * 7 } ) ) // renew cookie for 7 more days
53+ cookiesToSet . push (
54+ prepareSetCookie ( COOKIES . M2_GUEST_CART_ID , guestCartIdCookieValue , { maxAge : 3600 * 24 * 7 } )
55+ ) // renew cookie for 7 more days
4556 setCookies ( res , cookiesToSet )
46- return res . status ( 200 ) . json ( {
57+ return {
4758 signedIn : false ,
4859 cart,
49- } )
60+ }
5061 }
5162
5263 // # 2.2 - Obtain new guest session
5364 const obtainSessionData = await obtainSession ( )
5465 if ( obtainSessionData . error ) {
5566 setCookies ( res , cookiesToSet )
56- return res . status ( 400 ) . json ( {
67+ return {
5768 error : obtainSessionData . error ,
58- } )
69+ }
5970 }
6071 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
72+ cookiesToSet . push (
73+ prepareSetCookie ( COOKIES . M2_GUEST_CART_ID , guestCartId , { maxAge : 3600 * 24 * 7 } )
74+ ) // set guest cart id cookie for 7 days
6275 setCookies ( res , cookiesToSet )
63- return res . status ( 200 ) . json ( {
76+ return {
6477 signedIn : false ,
6578 cart : {
6679 items : [ ] ,
6780 } ,
68- } )
81+ }
6982}
0 commit comments