Skip to content

Commit

Permalink
add session utility for dirty state. Ensure this dirty state is set t…
Browse files Browse the repository at this point in the history
…o true whenever session is changed. And set to false whenever session is commited. Then in server code, check if session is dirty, if its dirty, set cookie with sessin.commit.
  • Loading branch information
michenly committed May 22, 2024
1 parent adc840f commit 8f6a236
Show file tree
Hide file tree
Showing 42 changed files with 111 additions and 359 deletions.
35 changes: 14 additions & 21 deletions examples/analytics/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,28 +94,21 @@ export async function loader({context}: LoaderFunctionArgs) {
},
});

return defer(
{
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
shop: getShopAnalytics({
storefront,
publicStorefrontId: env.PUBLIC_STOREFRONT_ID
}),
consent: {
checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN,
storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN,
},
},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
return defer({
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
shop: getShopAnalytics({
storefront,
publicStorefrontId: env.PUBLIC_STOREFRONT_ID,
}),
consent: {
checkoutDomain: env.PUBLIC_CHECKOUT_DOMAIN,
storefrontAccessToken: env.PUBLIC_STOREFRONT_API_TOKEN,
},
);
});
}

export default function App() {
Expand Down
2 changes: 0 additions & 2 deletions examples/analytics/app/routes/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ export async function action({request, context}: ActionFunctionArgs) {
headers.set('Location', redirectTo);
}

headers.append('Set-Cookie', await context.session.commit());

return json(
{
cart: cartResult,
Expand Down
21 changes: 7 additions & 14 deletions examples/b2b/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,20 +110,13 @@ export async function loader({context}: LoaderFunctionArgs) {
},
});

return defer(
{
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
},
);
return defer({
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
});
}

export default function App() {
Expand Down
9 changes: 1 addition & 8 deletions examples/b2b/app/routes/b2blocations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,7 @@ export async function loader({context}: LoaderFunctionArgs) {

const modalOpen = Boolean(company) && !companyLocationId;

return defer(
{company, companyLocationId, modalOpen},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
},
);
return defer({company, companyLocationId, modalOpen});
}

export default function CartRoute() {
Expand Down
21 changes: 7 additions & 14 deletions examples/classic-remix/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,20 +90,13 @@ export async function loader({context}: LoaderFunctionArgs) {
},
});

return defer(
{
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
},
);
return defer({
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
});
}

export default function App() {
Expand Down
2 changes: 0 additions & 2 deletions examples/custom-cart-method/app/routes/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,6 @@ export async function action({request, context}: ActionFunctionArgs) {
headers.set('Location', redirectTo);
}

headers.append('Set-Cookie', await context.session.commit());

return json(
{
cart: cartResult,
Expand Down
1 change: 0 additions & 1 deletion examples/legacy-customer-account-flow/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ async function validateCustomerAccessToken(

if (customerAccessTokenExpired) {
session.unset('customerAccessToken');
headers.append('Set-Cookie', await session.commit());
} else {
isLoggedIn = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,7 @@ export async function action({request, context}: ActionFunctionArgs) {
);
}

return json(
{error: null, customer: updated.customerUpdate?.customer},
{
headers: {
'Set-Cookie': await session.commit(),
},
},
);
return json({error: null, customer: updated.customerUpdate?.customer});
} catch (error: any) {
return json({error: error.message, customer: null}, {status: 400});
}
Expand Down
12 changes: 2 additions & 10 deletions examples/legacy-customer-account-flow/app/routes/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ export async function loader({request, context}: LoaderFunctionArgs) {
if (!isLoggedIn) {
if (isPrivateRoute || isAccountHome) {
session.unset('customerAccessToken');
return redirect('/account/login', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account/login');
} else {
// public subroute such as /account/login...
return json({
Expand Down Expand Up @@ -67,11 +63,7 @@ export async function loader({request, context}: LoaderFunctionArgs) {
// eslint-disable-next-line no-console
console.error('There was a problem loading account', error);
session.unset('customerAccessToken');
return redirect('/account/login', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account/login');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ export async function action({request, context, params}: ActionFunctionArgs) {
}
session.set('customerAccessToken', customerAccessToken);

return redirect('/account', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account');
} catch (error: unknown) {
if (error instanceof Error) {
return json({error: error.message}, {status: 400});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export async function action({request, context}: ActionFunctionArgs) {
const {customerAccessToken} = customerAccessTokenCreate;
session.set('customerAccessToken', customerAccessToken);

return redirect('/account', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account');
} catch (error: unknown) {
if (error instanceof Error) {
return json({error: error.message}, {status: 400});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ export async function action({request, context}: ActionFunctionArgs) {
return json({error: 'Method not allowed'}, {status: 405});
}

return redirect('/', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/');
}

export default function Logout() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,6 @@ export async function action({request, context}: ActionFunctionArgs) {
{error: null, newCustomer},
{
status: 302,
headers: {
'Set-Cookie': await session.commit(),
Location: '/account',
},
},
);
} catch (error: unknown) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,7 @@ export async function action({request, context, params}: ActionFunctionArgs) {
}
session.set('customerAccessToken', customerReset.customerAccessToken);

return redirect('/account', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account');
} catch (error: unknown) {
if (error instanceof Error) {
return json({error: error.message}, {status: 400});
Expand Down
2 changes: 0 additions & 2 deletions examples/legacy-customer-account-flow/app/routes/cart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,6 @@ export async function action({request, context}: ActionFunctionArgs) {
headers.set('Location', redirectTo);
}

headers.append('Set-Cookie', await context.session.commit());

return json(
{
cart: cartResult,
Expand Down
31 changes: 12 additions & 19 deletions examples/metaobjects/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,25 +89,18 @@ export async function loader({context}: LoaderFunctionArgs) {
},
});

return defer(
{
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
/***********************************************/
/********** EXAMPLE UPDATE STARTS ************/
publictoreSubdomain: context.env.PUBLIC_SHOPIFY_STORE_DOMAIN,
/********** EXAMPLE UPDATE END ************/
/***********************************************/
},
{
headers: {
'Set-Cookie': await context.session.commit(),
},
},
);
return defer({
cart: cartPromise,
footer: footerPromise,
header: await headerPromise,
isLoggedIn: isLoggedInPromise,
publicStoreDomain,
/***********************************************/
/********** EXAMPLE UPDATE STARTS ************/
publictoreSubdomain: context.env.PUBLIC_SHOPIFY_STORE_DOMAIN,
/********** EXAMPLE UPDATE END ************/
/***********************************************/
});
}

export default function App() {
Expand Down
1 change: 0 additions & 1 deletion examples/multipass/app/root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,6 @@ async function validateCustomerAccessToken(

if (customerAccessTokenExpired) {
session.unset('customerAccessToken');
headers.append('Set-Cookie', await session.commit());
} else {
isLoggedIn = true;
}
Expand Down
9 changes: 1 addition & 8 deletions examples/multipass/app/routes/account.profile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,7 @@ export async function action({request, context}: ActionFunctionArgs) {
);
}

return json(
{error: null, customer: updated.customerUpdate?.customer},
{
headers: {
'Set-Cookie': await session.commit(),
},
},
);
return json({error: null, customer: updated.customerUpdate?.customer});
} catch (error: any) {
return json({error: error.message, customer: null}, {status: 400});
}
Expand Down
12 changes: 2 additions & 10 deletions examples/multipass/app/routes/account.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,7 @@ export async function loader({request, context}: LoaderFunctionArgs) {
if (!isLoggedIn) {
if (isPrivateRoute || isAccountHome) {
session.unset('customerAccessToken');
return redirect('/account/login', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account/login');
} else {
// public subroute such as /account/login...
return json({
Expand Down Expand Up @@ -67,11 +63,7 @@ export async function loader({request, context}: LoaderFunctionArgs) {
// eslint-disable-next-line no-console
console.error('There was a problem loading account', error);
session.unset('customerAccessToken');
return redirect('/account/login', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account/login');
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,7 @@ export async function action({request, context, params}: ActionFunctionArgs) {
}
session.set('customerAccessToken', customerAccessToken);

return redirect('/account', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account');
} catch (error: unknown) {
if (error instanceof Error) {
return json({error: error.message}, {status: 400});
Expand Down
6 changes: 1 addition & 5 deletions examples/multipass/app/routes/account_.login.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,7 @@ export async function action({request, context}: ActionFunctionArgs) {
const {customerAccessToken} = customerAccessTokenCreate;
session.set('customerAccessToken', customerAccessToken);

return redirect('/account', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/account');
} catch (error: unknown) {
if (error instanceof Error) {
return json({error: error.message}, {status: 400});
Expand Down
6 changes: 1 addition & 5 deletions examples/multipass/app/routes/account_.logout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ export async function action({request, context}: ActionFunctionArgs) {
return json({error: 'Method not allowed'}, {status: 405});
}

return redirect('/', {
headers: {
'Set-Cookie': await session.commit(),
},
});
return redirect('/');
}

export default function Logout() {
Expand Down
1 change: 0 additions & 1 deletion examples/multipass/app/routes/account_.register.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export async function action({request, context}: ActionFunctionArgs) {
{
status: 302,
headers: {
'Set-Cookie': await session.commit(),
Location: '/account',
},
},
Expand Down
Loading

0 comments on commit 8f6a236

Please sign in to comment.