Who should use this guide: Only existing PWA Kit v2.x projects that want to add Hybrid Auth by upgrading to v2.10.0. PWA Kit v2.10.0 supports SLAS Public Clients only.
Use this guide to upgrade your existing 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. For additional details, check out the complete pwa-kit GitHub diff to get a better understanding of the code changes.
Prerequisites
Before starting the upgrade process, make sure that 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)
1# Open app/commerce-api/utils.js and remove authentication-related functions
Remove these imports from the top of the file.
1// REMOVE these lines from app/commerce-api/utils.js2import jwtDecode from "jwt-decode";3import{refreshTokenGuestStorageKey, refreshTokenRegisteredStorageKey}from "./constants";
Remove these functions from app/commerce-api/utils.js.
1// REMOVE these functions from app/commerce-api/utils.js2export function isTokenExpired(token){ ... }3export function createGetTokenBody(urlString, slasCallbackEndpoint, codeVerifier){ ... }4export function hasSFRAAuthStateChanged(storage, storageCopy){ ... }
Keep all other utility functions, including keysToCamel and createOcapiFetch, as they’re still needed.
Update the useCustomer hook. Open your app/hooks/use-customer.js file and update the authentication methods.
1// Add these new imports at the top of the file:2import{AuthHelpers}from '@salesforce/commerce-sdk-react'3import{useAuthHelper}from '@salesforce/commerce-sdk-react'45// Find the login and logout methods in useCustomer hook and update them:67// Old implementation (v2.9.x)8const login = async(credentials)=>{9 const auth = new Auth();10 await auth.login(credentials);11 // ... rest of login logic12};1314const logout = async()=>{15 const auth = new Auth();16 await auth.logout();17 // ... rest of logout logic18};1920// New implementation (v2.10.0)21export default function useCustomer(){22 const api = useCommerceAPI()23 const{customer, setCustomer} = useContext(CustomerContext)2425 const login = useAuthHelper(AuthHelpers.LoginRegisteredUserB2C)26 const logout = useAuthHelper(AuthHelpers.Logout)2728 const getSkeletonCustomer = ()=>{29 return{30 customerId: api.auth.get('customer_id'),31 authType: api.auth.get('customer_type')32}33}3435 const self = useMemo(()=>{36 return{37 ... customer,3839 // Other useCustomer hook functions40 ...41 // The login method is now handled by the useAuthHelper hook42 async login(credentials){43 await api.auth.ready()44 let skeletonCustomer = getSkeletonCustomer()45 if(credentials){46 await login.mutateAsync({47 username: credentials.email,48 password: credentials.password49})50 skeletonCustomer = getSkeletonCustomer()51}52 // ... rest of login logic53},5455 async logout(){56 await logout.mutateAsync()57 await api.auth.ready()58 const skeletonCustomer = getSkeletonCustomer()59 setCustomer(skeletonCustomer)60 // ... rest of logout logic61}62}63})
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.
Note
Step 5: Update the Commerce API Implementation
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.
Note
Open your app/commerce-api/index.js file.
Update the imports at the top of the file.
1// Remove these old imports:2import Auth from "./auth";3import{isError}from "./utils";45// Add these new imports:6import Auth from "@salesforce/commerce-sdk-react/auth";7import{transformSDKClient}from "@salesforce/commerce-sdk-react/utils";8import{DWSID_HEADER_KEY}from "./constants";
Update the auth module instantiation.
1// Old implementation (v2.9.x)23this._config = {proxy, ...restConfig};45this.auth = new Auth(this); // Update this with new authConfig
Replace with this new code:
1// Old implementation (v2.9.x)23this._config = {proxy, ...restConfig};45// Add new authConfig object6this._authConfig = {7 redirectURI: `${getAppOrigin()}/callback`,8 proxy,9 locale: this._config.locale,10 currency: this._config.currency,11 ...this._config.parameters,12 ...this._config.headers,13};1415this.auth = new Auth(this._authConfig); // pass authConfig as param to Auth class constructor.
Find the section where SDK clients are instantiated (around line 100-150) and replace the entire SDK instantiation logic.
Remove this old code:
1// Old implementation (v2.9.x) - using Proxy2const SdkClass = apiConfigs[key].api;3self._sdkInstances = {4 ...self._sdkInstances,5[key]: new Proxy(new SdkClass(this._config), {6 get: function(obj, prop){7 // ... proxy implementation8},9}),10};
Critical Change: The CommerceAPIProvider now requires different props structure.
Important
Open your app/components/_app-config/index.jsx file.
Find the CommerceAPIProvider component and update it.
Replace this code:
1// Old implementation (v2.9.x)2<CommerceAPIProvider value={locals.api}>
With this code:
1// New implementation (v2.10.0)2<CommerceAPIProvider value={locals}>
Save the app/components/_app-config/index.jsx file.
Step 8: Cleanup Auth Initialization
Open your app/components/_app/index.jsx file.
Remove the login call.
1// Auth initialization is now handled by calling await self.auth.ready() in commerce-api/index.js2// Remove this line.3await api.auth.login();
Step 9: Test Your Upgrade
Start the development server.
1npm start
Run the build to check for errors.
1npm run build
(Optional) Use Commerce SDK React Query Hooks
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.
Example: Simple Order List Component
Create a minimal order list component using the new query hooks.