Skip to content

Commit f52f325

Browse files
committed
cors example changes to headers
1 parent c9c116f commit f52f325

File tree

3 files changed

+95
-263
lines changed

3 files changed

+95
-263
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122
"@typescript-eslint/eslint-plugin": "^4.33.0",
123123
"@typescript-eslint/parser": "^4.33.0",
124124
"autoprefixer": "9.8.8",
125-
"bundlesize": "^0.18.1",
125+
"bundlesize2": "^0.0.31",
126126
"depcheck": "^1.4.3",
127127
"dotenv": "^16.0.3",
128128
"eslint": "^7.32.0",

src/static/helpers/slasHelper.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ export const generateCodeChallenge = async (
104104
* @param parameters.redirectURI - the location the client will be returned to after successful login with 3rd party IDP. Must be registered in SLAS.
105105
* @param parameters.hint? - optional string to hint at a particular IDP. Guest sessions are created by setting this to 'guest'
106106
* @param parameters.usid? - optional saved SLAS user id to link the new session to a previous session
107+
* @param headers? - optional headers to pass in the API calls.
107108
* @returns login url, user id and authorization code if available
108109
*/
109110
export async function authorize(
@@ -119,7 +120,8 @@ export async function authorize(
119120
hint?: string;
120121
usid?: string;
121122
},
122-
privateClient = false
123+
privateClient = false,
124+
headers: Record<string, string>
123125
): Promise<{code: string; url: string; usid: string}> {
124126
interface ClientOptions {
125127
codeChallenge?: string;
@@ -132,13 +134,23 @@ export async function authorize(
132134
// Create a copy to override specific fetchOptions
133135
const slasClientCopy = new ShopperLogin(slasClient.clientConfig);
134136

137+
const hdrs = {
138+
...headers,
139+
// Add CORS headers for the Salesforce request
140+
Origin: new URL(parameters.redirectURI).origin,
141+
};
142+
135143
// set manual redirect on server since node allows access to the location
136144
// header and it skips the extra call. In the browser, only the default
137145
// follow setting allows us to get the url.
138146
/* istanbul ignore next */
139147
slasClientCopy.clientConfig.fetchOptions = {
140148
...slasClient.clientConfig.fetchOptions,
149+
// In browser, we need to follow redirects
141150
redirect: isBrowser ? 'follow' : 'manual',
151+
// Ensure CORS mode is set
152+
mode: 'cors',
153+
headers: hdrs,
142154
};
143155

144156
const options = {
@@ -154,6 +166,7 @@ export async function authorize(
154166
response_type: 'code',
155167
...(parameters.usid && {usid: parameters.usid}),
156168
},
169+
headers: hdrs,
157170
};
158171

159172
const response = await slasClientCopy.authorizeCustomer(options, true);
@@ -376,11 +389,21 @@ export async function loginGuestUser(
376389
): Promise<TokenResponse> {
377390
const codeVerifier = createCodeVerifier();
378391

379-
const authResponse = await authorize(slasClient, codeVerifier, {
380-
redirectURI: parameters.redirectURI,
381-
hint: 'guest',
382-
...(parameters.usid && {usid: parameters.usid}),
383-
});
392+
const authResponse = await authorize(
393+
slasClient,
394+
codeVerifier,
395+
{
396+
redirectURI: parameters.redirectURI,
397+
hint: 'guest',
398+
...(parameters.usid ? {usid: parameters.usid} : {}),
399+
},
400+
// TODO: this is hard-coded and should not be
401+
false,
402+
{
403+
// Origin: 'https://scaffold-pwa-254-cors-demo.mobify-storefront.com',
404+
'X-Force-Preflight': 'true',
405+
}
406+
);
384407

385408
const tokenBody: TokenRequest = {
386409
client_id: slasClient.clientConfig.parameters.clientId,

0 commit comments

Comments
 (0)