From 25414e691af8bf3d5c578aed3b458708427cff12 Mon Sep 17 00:00:00 2001 From: Jeremy Gabriel <63984091+jeremyagabriel@users.noreply.github.com> Date: Thu, 26 Sep 2024 11:55:31 -0700 Subject: [PATCH] chore: update session commit per hydrogen migration requirement --- app/lib/customer/utils.ts | 1 - app/lib/session.server.ts | 4 ++++ app/routes/($locale).account.login.tsx | 2 +- app/routes/($locale).account.logout.ts | 9 ++------- app/routes/($locale).account.profile.tsx | 5 +---- app/routes/($locale).account.register.tsx | 2 +- app/routes/($locale).account.tsx | 5 ----- server.ts | 4 ++++ 8 files changed, 13 insertions(+), 19 deletions(-) diff --git a/app/lib/customer/utils.ts b/app/lib/customer/utils.ts index 0f8b0eb..2c27776 100644 --- a/app/lib/customer/utils.ts +++ b/app/lib/customer/utils.ts @@ -22,7 +22,6 @@ export const validateCustomerAccessToken = async ( if (customerAccessTokenExpired) { session.unset('customerAccessToken'); - headers.append('Set-Cookie', await session.commit()); } else { isLoggedIn = true; } diff --git a/app/lib/session.server.ts b/app/lib/session.server.ts index b9fb4da..a44e1ff 100644 --- a/app/lib/session.server.ts +++ b/app/lib/session.server.ts @@ -8,6 +8,7 @@ import type {SessionStorage, Session} from '@shopify/remix-oxygen'; * swap out the cookie-based implementation with something else! */ export class AppSession implements HydrogenSession { + public isPending = false; #sessionStorage; #session; @@ -45,10 +46,12 @@ export class AppSession implements HydrogenSession { } get unset() { + this.isPending = true; return this.#session.unset; } get set() { + this.isPending = true; return this.#session.set; } @@ -57,6 +60,7 @@ export class AppSession implements HydrogenSession { } commit() { + this.isPending = true; return this.#sessionStorage.commitSession(this.#session); } } diff --git a/app/routes/($locale).account.login.tsx b/app/routes/($locale).account.login.tsx index e92c87a..a691aeb 100644 --- a/app/routes/($locale).account.login.tsx +++ b/app/routes/($locale).account.login.tsx @@ -19,7 +19,7 @@ export async function action({request, context}: ActionFunctionArgs) { const customerAccessToken = data.customerAccessToken; if (customerAccessToken) { session.set('customerAccessToken', customerAccessToken); - return json(data, {headers: {'Set-Cookie': await session.commit()}}); + return json(data); } return json(data, {status}); } diff --git a/app/routes/($locale).account.logout.ts b/app/routes/($locale).account.logout.ts index 8df78d1..d04c15a 100644 --- a/app/routes/($locale).account.logout.ts +++ b/app/routes/($locale).account.logout.ts @@ -9,7 +9,7 @@ import type { import {LOGGED_OUT_REDIRECT_TO} from '~/lib/constants'; import type {I18nLocale} from '~/lib/types'; -export async function cusomterLogout(context: AppLoadContext) { +export async function customerLogout(context: AppLoadContext) { const {session} = context; session.unset('customerAccessToken'); @@ -18,11 +18,6 @@ export async function cusomterLogout(context: AppLoadContext) { `${ (context.storefront.i18n as I18nLocale).pathPrefix }${LOGGED_OUT_REDIRECT_TO}`, - { - headers: { - 'Set-Cookie': await session.commit(), - }, - }, ); } @@ -35,5 +30,5 @@ export async function loader({context}: LoaderFunctionArgs) { } export const action: ActionFunction = async ({context}: ActionFunctionArgs) => { - return cusomterLogout(context); + return customerLogout(context); }; diff --git a/app/routes/($locale).account.profile.tsx b/app/routes/($locale).account.profile.tsx index 11eb531..3d94e8d 100644 --- a/app/routes/($locale).account.profile.tsx +++ b/app/routes/($locale).account.profile.tsx @@ -17,10 +17,7 @@ export async function action({request, context}: ActionFunctionArgs) { const {data, status} = await customerUpdateProfileAction({request, context}); if (data.customerAccessToken) { context.session.set('customerAccessToken', data.customerAccessToken); - return json(data, { - status, - headers: {'Set-Cookie': await context.session.commit()}, - }); + return json(data, {status}); } return json(data, {status}); } diff --git a/app/routes/($locale).account.register.tsx b/app/routes/($locale).account.register.tsx index 0fc75a1..d09b8fd 100644 --- a/app/routes/($locale).account.register.tsx +++ b/app/routes/($locale).account.register.tsx @@ -19,7 +19,7 @@ export async function action({request, context}: ActionFunctionArgs) { const customerAccessToken = data.customerAccessToken; if (customerAccessToken) { session.set('customerAccessToken', customerAccessToken); - return json(data, {headers: {'Set-Cookie': await session.commit()}}); + return json(data); } return json({...data}, {status}); } diff --git a/app/routes/($locale).account.tsx b/app/routes/($locale).account.tsx index 53d3e49..1d24ba0 100644 --- a/app/routes/($locale).account.tsx +++ b/app/routes/($locale).account.tsx @@ -58,11 +58,6 @@ export async function loader({request, context, params}: LoaderFunctionArgs) { session.unset('customerAccessToken'); return redirect( locale ? `/${locale}${LOGGED_OUT_REDIRECT_TO}` : LOGGED_OUT_REDIRECT_TO, - { - headers: { - 'Set-Cookie': await session.commit(), - }, - }, ); } } diff --git a/server.ts b/server.ts index aa67020..719d4fd 100644 --- a/server.ts +++ b/server.ts @@ -106,6 +106,10 @@ export default { }), ); + if (session.isPending) { + response.headers.set('Set-Cookie', await session.commit()); + } + if (response.status === 404) { /** * Check for redirects only when there's a 404 from the app.