From dc9058c191cdbf386a2c3e1c252bfd497c53e31d Mon Sep 17 00:00:00 2001 From: Jainam Tushar Sheth Date: Fri, 28 Apr 2023 16:49:15 -0400 Subject: [PATCH 1/2] Added session bridge call to login for phased launch --- .../app/commerce-api/auth.js | 49 +++++++++++++++---- 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/packages/template-retail-react-app/app/commerce-api/auth.js b/packages/template-retail-react-app/app/commerce-api/auth.js index b85644ea06..057671fa15 100644 --- a/packages/template-retail-react-app/app/commerce-api/auth.js +++ b/packages/template-retail-react-app/app/commerce-api/auth.js @@ -189,6 +189,31 @@ class Auth { return response } + /** + * Make a post request to the OCAPI /session endpoint to bridge the session. + * + * The HTTP response contains a set-cookie header which sets the dwsid session cookie. + * This cookie is used on SFRA, and it allows shoppers to navigate between SFRA and + * this PWA site seamlessly; this is often used to enable hybrid deployment. + * + * (Note: this method is client side only, b/c MRT doesn't support set-cookie header right now) + * + * @returns {Promise} + */ + createOCAPISession() { + return fetch( + `${getAppOrigin()}/mobify/proxy/ocapi/s/${ + this._config.parameters.siteId + }/dw/shop/v22_8/sessions`, + { + method: 'POST', + headers: { + Authorization: this.authToken + } + } + ) + } + /** * Authorizes the customer as a registered or guest user. * @param {CustomerCredentials} [credentials] @@ -210,15 +235,21 @@ class Auth { } else if (this.refreshToken) { authorizationMethod = '_refreshAccessToken' } - return this[authorizationMethod](credentials).catch((error) => { - const retryErrors = [INVALID_TOKEN, EXPIRED_TOKEN] - if (retries === 0 && retryErrors.includes(error.message)) { - retries = 1 // we only retry once - this._clearAuth() - return startLoginFlow() - } - throw error - }) + return this[authorizationMethod](credentials) + .catch((error) => { + const retryErrors = [INVALID_TOKEN, EXPIRED_TOKEN] + if (retries === 0 && retryErrors.includes(error.message)) { + retries = 1 // we only retry once + this._clearAuth() + return startLoginFlow() + } + throw error + }) + .then((result) => { + // Uncomment the following line for phased launch + // this._onClient && this.createOCAPISession() + return result + }) } this._pendingLogin = startLoginFlow().finally(() => { From da08f53df5af3966124ec3be8560e71f6cf2b845 Mon Sep 17 00:00:00 2001 From: Jainam Tushar Sheth Date: Fri, 28 Apr 2023 17:41:19 -0400 Subject: [PATCH 2/2] Fix code smell for session-bridge in hybrid --- .../template-retail-react-app/app/commerce-api/auth.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/template-retail-react-app/app/commerce-api/auth.js b/packages/template-retail-react-app/app/commerce-api/auth.js index 057671fa15..f60be52492 100644 --- a/packages/template-retail-react-app/app/commerce-api/auth.js +++ b/packages/template-retail-react-app/app/commerce-api/auth.js @@ -236,6 +236,11 @@ class Auth { authorizationMethod = '_refreshAccessToken' } return this[authorizationMethod](credentials) + .then((result) => { + // Uncomment the following line for phased launch + // this._onClient && this.createOCAPISession() + return result + }) .catch((error) => { const retryErrors = [INVALID_TOKEN, EXPIRED_TOKEN] if (retries === 0 && retryErrors.includes(error.message)) { @@ -245,11 +250,6 @@ class Auth { } throw error }) - .then((result) => { - // Uncomment the following line for phased launch - // this._onClient && this.createOCAPISession() - return result - }) } this._pendingLogin = startLoginFlow().finally(() => {