Implement Hybrid Authentication for PWA Kit v2
Use this guide to upgrade your PWA Kit application to version 2.10.0 and implement Hybrid Auth. The PWA Kit 2.10.0 release introduces significant changes, such as compatibility with Hybrid Auth and integration with the @salesforce/commerce-sdk-react
package. PWA Kit v2.10.0 only supports SLAS Public Clients. For additional details, check out the complete pwa-kit GitHub diff to get a better understanding of the code changes.
If you're a new customer trying to onboard to Hybrid Auth, you must get started with PWA Kit v3.10.0 or later. For Hybrid Auth implementation with PWA Kit v3, see the Hybrid Implementation Guide.
Before starting the upgrade process, ensure you have:
- Node.js version 18.x or 20.x (upgraded from 14.x/16.x support)
- NPM version 9.x or 10.x (upgraded from 6.x/7.x/8.x support)
- Enable hybrid auth on your Business Manager instance and enable cookies on Managed Runtime. For details, see Hybrid Authentication.
The most significant update is the introduction of the new Commerce SDK React authentication system.
-
Check your current Node.js and npm versions.
If your Node.js version is below 18.x, upgrade it first. If your npm version is below 9.x, upgrade it first.
-
Open your
package.json
file and update the following sections.Update the
"engines"
section.Update the
"devDependencies"
section with these new dependencies and version bumps. -
Save the
package.json
file. -
Clean install the new dependencies.
The @testing-library/*
dependencies are optional and required only if you've implemented unit tests using react-testing-library
.
-
Open your
config/default.js
file. -
Find the line that contains
ssrFunctionNodeVersion
and update it. -
Save the
config/default.js
file.
Update your authentication implementation with the new Commerce SDK React system.
-
Remove old authentication files.
-
Update the
utils.js
file.Remove these imports from the top of the file.
Remove these functions from
app/commerce-api/utils.js
.Keep all other utility functions, including
keysToCamel
andcreateOcapiFetch
, as they're still needed. -
Update the
useCustomer
hook. Open yourapp/hooks/use-customer.js
file and update the authentication methods.
The useCustomer
hook now uses the new Commerce SDK React authentication methods. Your existing login and registration components don't need any changes since they call the same login()
and register()
functions.
The Commerce API implementation has been significantly updated to use the new transformSDKClient
utility from @salesforce/commerce-sdk-react
package. See the README.md for the package to learn more.
-
Open your
app/commerce-api/index.js
file. -
Update the imports at the top of the file.
-
Update the auth module instantiation.
Replace with this new code:
-
Find the section where SDK clients are instantiated (around line 100-150) and replace the entire SDK instantiation logic. Remove this old code:
Replace with this new code:
-
Find and remove these methods from the CommerceAPI class:
-
Update the constants file. Add a new constant in
app/commerce-api/constants.js
.
The Commerce API contexts have been updated to integrate with Commerce SDK React and React Query.
-
Open your
app/commerce-api/contexts.js
file. -
Add these new imports at the top.
-
Add the
QueryClient
configuration after the imports. -
Replace the existing
CommerceAPIProvider
with this new implementation. -
Save the
app/commerce-api/contexts.js
file.
Critical Change: The CommerceAPIProvider now requires different props
structure.
-
Open your
app/components/_app-config/index.jsx
file. -
Find the
CommerceAPIProvider
component and update it.Replace this code:
With this code:
-
Save the
app/components/_app-config/index.jsx
file.
-
Open your
app/components/_app/index.jsx
file. -
Remove the login call.
-
Start the development server.
-
Run the build to check for errors.
With PWA Kit v2.10.0, you now have access to powerful query hooks from @salesforce/commerce-sdk-react
that provide automatic caching, loading states, and error handling for both hybrid and non-hybrid PWA Kit v2.x storefronts. Here's how to use them in your pages.
Create a minimal order list component using the new query hooks.
Check out the key benefits of the new query hooks.
- Automatic caching—Data is cached and shared.
- Loading states—Built-in loading management.
- Error handling—Automatic error states.
- Optimized fetching—Only fetches when customer is available.
- Simplified code—Less manual state management.