-
Notifications
You must be signed in to change notification settings - Fork 31
refactor(medusa): refactor cart ownership checks and update order list query #837
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
Merged
lukasz-hycom
merged 3 commits into
main
from
fix/medusa_cart-validation-and-orders-fixes
Mar 26, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@o2s/integrations.medusajs': minor | ||
| --- | ||
|
|
||
| Simplify cart/order ownership checks — resolve Medusa customer ID via /store/customers/me instead of relying on auth token claims. Fix missing +item_subtotal field in order list query. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 29 additions & 21 deletions
50
packages/integrations/medusajs/src/utils/customer-access.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,49 +1,57 @@ | ||
| import Medusa from '@medusajs/js-sdk'; | ||
| import { HttpTypes } from '@medusajs/types'; | ||
| import { UnauthorizedException } from '@nestjs/common'; | ||
| import { Observable, from, map } from 'rxjs'; | ||
|
|
||
| import { Auth } from '@o2s/framework/modules'; | ||
| import { Observable, from, map, of, switchMap } from 'rxjs'; | ||
|
|
||
| /** | ||
| * Checks if a resource (cart/order) with a given customerId can be accessed. | ||
| * | ||
| * Medusa assigns a customer_id to both registered and guest customers. | ||
| * Guest customers (has_account=false) are created automatically when an email is provided. | ||
| * This helper uses Admin API to check has_account and enforce ownership: | ||
| * Verifies that the caller has access to a resource owned by a given customer. | ||
| * | ||
| * - No customerId on resource → accessible to anyone | ||
| * - No authorization token → check if customer is guest (has_account=false), if so allow access | ||
| * - Authorization provided → customerId must match the authenticated user | ||
| * - No customerId on resource → allow (public/unassigned resource) | ||
| * - Authenticated + resource is mine → allow | ||
| * - Authenticated + resource belongs to a guest → allow (e.g. cart created before login) | ||
| * - Authenticated + resource belongs to another registered user → deny | ||
| * - Unauthenticated + resource belongs to a guest → allow (guest checkout flow) | ||
| * - Unauthenticated + resource belongs to a registered user → deny | ||
| */ | ||
| export const verifyResourceAccess = ( | ||
| sdk: Medusa, | ||
| authService: Auth.Service, | ||
| adminHeaders: Record<string, string>, | ||
| storeHeaders: Record<string, string>, | ||
| customerId: string | undefined, | ||
| authorization: string | undefined, | ||
| ): Observable<void> => { | ||
| // No customer on resource — public access | ||
| if (!customerId) { | ||
| return from(Promise.resolve()); | ||
| } | ||
|
|
||
| // Authorized user — check ownership directly (no Admin API call needed) | ||
| if (authorization) { | ||
| const tokenCustomerId = authService.getCustomerId(authorization); | ||
| if (customerId !== tokenCustomerId) { | ||
| throw new UnauthorizedException('Unauthorized'); | ||
| } | ||
| return from(Promise.resolve()); | ||
| // Resolve the caller's Medusa customer ID via /store/customers/me | ||
| return from(sdk.store.customer.retrieve({}, storeHeaders)).pipe( | ||
| switchMap((response: HttpTypes.StoreCustomerResponse) => { | ||
| if (response.customer?.id === customerId) { | ||
| return of(undefined); | ||
| } | ||
|
|
||
| // Not the owner — allow if it's a guest cart, deny if registered | ||
| return from(sdk.admin.customer.retrieve(customerId, {}, adminHeaders)).pipe( | ||
| map((adminResponse) => { | ||
| const customer = adminResponse.customer as { has_account?: boolean }; | ||
| if (customer.has_account !== false) { | ||
| throw new UnauthorizedException('Unauthorized to access this resource'); | ||
| } | ||
| }), | ||
| ); | ||
| }), | ||
| ); | ||
| } | ||
|
|
||
| // No authorization — check if this is a guest customer via Admin API | ||
| // No token — only allow if this is a guest customer (has_account=false) | ||
| return from(sdk.admin.customer.retrieve(customerId, {}, adminHeaders)).pipe( | ||
| map((response) => { | ||
| const customer = response.customer as { has_account?: boolean }; | ||
| if (customer.has_account !== false) { | ||
| throw new UnauthorizedException('Authentication required to access this resource'); | ||
| } | ||
| // Guest customer (has_account=false) — allow access | ||
| }), | ||
| ); | ||
| }; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.