Skip to content

Commit 9ff39cb

Browse files
committed
feat: add SFCC_REDIRECT_URI env var for implicit auth behind a proxy
When running behind a proxy where localhost:8080 cannot be reached directly by the browser, SFCC_REDIRECT_URI overrides the redirect_uri sent to Account Manager. The local server still listens on localPort.
1 parent 646d78d commit 9ff39cb

1 file changed

Lines changed: 17 additions & 4 deletions

File tree

packages/b2c-tooling-sdk/src/auth/oauth-implicit.ts

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ export interface ImplicitOAuthConfig {
3434
* Defaults to 8080 or SFCC_OAUTH_LOCAL_PORT environment variable.
3535
*/
3636
localPort?: number;
37+
/**
38+
* Full redirect URI for OAuth. Use when running behind a proxy where
39+
* localhost cannot be reached directly by the browser.
40+
* Defaults to `http://localhost:${localPort}` or SFCC_REDIRECT_URI environment variable.
41+
* The local server still listens on localPort regardless of this setting.
42+
*/
43+
redirectUri?: string;
3744
}
3845

3946
/**
@@ -100,15 +107,22 @@ async function openBrowser(url: string): Promise<void> {
100107
export class ImplicitOAuthStrategy implements AuthStrategy {
101108
private accountManagerHost: string;
102109
private localPort: number;
110+
private redirectUri: string;
103111
private _hasHadSuccess = false;
104112

105113
constructor(private config: ImplicitOAuthConfig) {
106114
this.accountManagerHost = config.accountManagerHost || DEFAULT_ACCOUNT_MANAGER_HOST;
107115
this.localPort = config.localPort || parseInt(process.env.SFCC_OAUTH_LOCAL_PORT || '', 10) || DEFAULT_LOCAL_PORT;
116+
this.redirectUri = config.redirectUri || process.env.SFCC_REDIRECT_URI || `http://localhost:${this.localPort}`;
108117

109118
const logger = getLogger();
110119
logger.debug(
111-
{clientId: this.config.clientId, accountManagerHost: this.accountManagerHost, port: this.localPort},
120+
{
121+
clientId: this.config.clientId,
122+
accountManagerHost: this.accountManagerHost,
123+
port: this.localPort,
124+
redirectUri: this.redirectUri,
125+
},
112126
'[Auth] ImplicitOAuthStrategy initialized',
113127
);
114128
logger.trace({scopes: this.config.scopes}, '[Auth] Configured scopes');
@@ -283,11 +297,10 @@ export class ImplicitOAuthStrategy implements AuthStrategy {
283297
*/
284298
private async implicitFlowLogin(): Promise<AccessTokenResponse> {
285299
const logger = getLogger();
286-
const redirectUrl = `http://localhost:${this.localPort}`;
287300

288301
const params = new URLSearchParams({
289302
client_id: this.config.clientId,
290-
redirect_uri: redirectUrl,
303+
redirect_uri: this.redirectUri,
291304
response_type: 'token',
292305
});
293306

@@ -300,7 +313,7 @@ export class ImplicitOAuthStrategy implements AuthStrategy {
300313
logger.debug(
301314
{
302315
clientId: this.config.clientId,
303-
redirectUrl,
316+
redirectUri: this.redirectUri,
304317
scopes: this.config.scopes,
305318
accountManagerHost: this.accountManagerHost,
306319
},

0 commit comments

Comments
 (0)