Skip to content

Commit

Permalink
chore: update session commit per hydrogen migration requirement
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyagabriel committed Sep 26, 2024
1 parent a2771ca commit 25414e6
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 19 deletions.
1 change: 0 additions & 1 deletion app/lib/customer/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ export const validateCustomerAccessToken = async (

if (customerAccessTokenExpired) {
session.unset('customerAccessToken');
headers.append('Set-Cookie', await session.commit());
} else {
isLoggedIn = true;
}
Expand Down
4 changes: 4 additions & 0 deletions app/lib/session.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand All @@ -57,6 +60,7 @@ export class AppSession implements HydrogenSession {
}

commit() {
this.isPending = true;
return this.#sessionStorage.commitSession(this.#session);
}
}
2 changes: 1 addition & 1 deletion app/routes/($locale).account.login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
Expand Down
9 changes: 2 additions & 7 deletions app/routes/($locale).account.logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand All @@ -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(),
},
},
);
}

Expand All @@ -35,5 +30,5 @@ export async function loader({context}: LoaderFunctionArgs) {
}

export const action: ActionFunction = async ({context}: ActionFunctionArgs) => {
return cusomterLogout(context);
return customerLogout(context);
};
5 changes: 1 addition & 4 deletions app/routes/($locale).account.profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
Expand Down
2 changes: 1 addition & 1 deletion app/routes/($locale).account.register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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});
}
Expand Down
5 changes: 0 additions & 5 deletions app/routes/($locale).account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
},
);
}
}
Expand Down
4 changes: 4 additions & 0 deletions server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 25414e6

Please sign in to comment.