Skip to content

feat(core): add session-sync endpoint #2108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: canary
Choose a base branch
from
Draft

Conversation

chanceaclark
Copy link
Contributor

What/Why?

Adds a session sync endpoint, used for when needing to sync the session between a "3rd party" session and your Catalyst session. In the case of this endpoint, we are primarily prepping for syncing across Catalyst <-> Stencil when navigating to the checkout. We still need some work to be done on the Stencil checkout piece, but this should enable the team to sync the session back and forth.

Testing

Logging in

Screen.Recording.2025-03-18.at.16.08.24.mov

Logging out

Screen.Recording.2025-03-18.at.16.10.25.mov

Copy link

changeset-bot bot commented Mar 18, 2025

🦋 Changeset detected

Latest commit: 8ede6ad

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@bigcommerce/catalyst-core Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Mar 18, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
catalyst-canary ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 4, 2025 6:59pm
4 Skipped Deployments
Name Status Preview Comments Updated (UTC)
catalyst ⬜️ Ignored (Inspect) Apr 4, 2025 6:59pm
catalyst-au ⬜️ Ignored (Inspect) Visit Preview Apr 4, 2025 6:59pm
catalyst-soul ⬜️ Ignored (Inspect) Visit Preview Apr 4, 2025 6:59pm
catalyst-uk ⬜️ Ignored (Inspect) Visit Preview Apr 4, 2025 6:59pm

Comment on lines +157 to +179
function loginWithSessionSync(credentials: unknown): User {
return SessionSyncCredentials.parse(credentials);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little awkward... Usually you should handle the validation of the sign in here but because we are going to move in a direction where there will alway be a session, this CredentialsProvider will actually be removed and we will just update the session accordingly. We also need to handle things like the redirect and any API validation errors so it made more sense to do the validation in the route handler, rather than in the signIn function.

}

// Note: redirectTo is going to include the full url, not a partial path
return NextResponse.redirect(content.redirectTo);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do I have this right:

this redirect url here will basically be the same/ do the same thing as this?

image

So that this API endpoint acts as a middleware between the redirect to checkout -> managed checkout redirect?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah that redirectedCheckoutUrl uses a session-sync endpoint on the Stencil side of things, which is similar to what this endpoint does.

The big thing about this approach is it allows sessions to be sync'd when we are scoping our session cookies with the _Host prefix as they are scoped to subdomains. For example a cookie wouldn't be able to be reused from example.com (www. subdomain) to checkout.example.com.

We need to follow this general approach:

  1. Redirect to endpoint on another/same domain, e.g. example.com -> checkout.example.com.
  2. Response contains the updated session via the Set-Cookie header + Location header + 3xx status.
  3. Browser consumes the Location header and redirect to the correct end destination (via redirectTo).

}, []);

useEffect(() => {
window.addEventListener('pageshow', handleBrowserBackButtonEvent);
Copy link

@davidchin davidchin Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the force reload happen on all pages when the back button is hit and bfcache is present, even if no session syncing is needed? Or does it happen only when the user clicks on the back button coming from BC checkout?

Copy link
Contributor

@willPrattUPL willPrattUPL Mar 31, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we set a flag like "redirected_to_checkout" when users click the checkout button? We only reload conditionally

Then session sync provider deletes this cookie when it successfully reloads once.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll need to do some digging into here – I bet I can check the referrer before doing this.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤞 We can use the referrer. IIRC, BC doesn't have explicit referrer policy so it'd be the default (e.g.: strict-origin-when-cross-origin). We should at least be able to retrieve the origin value and disable bfcache if the request comes from a different origin. If we can't get this working reliably, I’d much prefer to do nothing and let users refresh the page themselves.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dug into this a bit and yeah strict-origin-when-cross-origin is mucking the waters here. And we probably can't add a cookie to this as we'll need some sort of validation that it was the other origin who set it - I could see a security issue where a bad actor could set the cookie and trigger infinite reloads, exhausting our resources in the process.

@bookernath from engineering perspective this might more effort than it's worth - just letting users reload the page and the session will update it. I think this is the best bet :(

@chanceaclark chanceaclark changed the title feat: add session-sync endpoint feat(core): add session-sync endpoint Apr 4, 2025
Copy link
Contributor

github-actions bot commented Apr 4, 2025

⚡️🏠 Lighthouse report

Lighthouse ran against https://catalyst-canary-4p51rdznb-bigcommerce-platform.vercel.app

🖥️ Desktop

We ran Lighthouse against the changes on a desktop and produced this report. Here's the summary:

Category Score
🟢 Performance 99
🟢 Accessibility 92
🟠 Best practices 78
🟠 SEO 82

📱 Mobile

We ran Lighthouse against the changes on a mobile and produced this report. Here's the summary:

Category Score
🟠 Performance 82
🟢 Accessibility 92
🟠 Best practices 78
🟠 SEO 85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants