Skip to content

Commit 74cffc0

Browse files
Fixes OAuth authentication issues when using custom hosts without specifying region
1 parent a9a085f commit 74cffc0

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

lib/core/contentstackHTTPClient.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,30 @@ export default function contentstackHttpClient (options) {
6868
config.basePath = `/${config.basePath.split('/').filter(Boolean).join('/')}`
6969
}
7070
const baseURL = config.endpoint || `${protocol}://${hostname}:${port}${config.basePath}/{api-version}`
71-
const region = config.region || 'na'
71+
72+
let region = config.region || 'na'
73+
if (!config.region && config.host) {
74+
const hostRegionMatch = config.host.match(/^([a-z]+-?[a-z]*)-api\./)
75+
if (hostRegionMatch) {
76+
region = hostRegionMatch[1]
77+
}
78+
}
79+
7280
const uiHostName = getRegionEndpoint(region, 'application')
73-
const developerHubBaseUrl = getRegionEndpoint(region, 'developerHub').replace(/^/, 'https://')
81+
82+
let developerHubBaseUrl
83+
if (config.host && (config.host.startsWith('dev') || config.host.startsWith('stag'))) {
84+
developerHubBaseUrl = config.host
85+
.replace('api', 'developerhub-api')
86+
.replace(/^dev\d+/, 'dev')
87+
.replace(/^stag\d+/, 'stag')
88+
.replace(/^http:\/\//, '')
89+
.replace(/^https:\/\//, '')
90+
.replace(/^/, 'https://')
91+
} else {
92+
developerHubBaseUrl = getRegionEndpoint(region, 'developerHub').replace(/^/, 'https://')
93+
}
94+
7495
const uiBaseUrl = config.endpoint || `${protocol}://${uiHostName}`
7596

7697
// set ui host name

test/sanity-check/api/oauth-test.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,6 @@ const oauthClient = client.oauth({
2121
redirectUri: process.env.REDIRECT_URI
2222
})
2323

24-
// Override developerHubBaseUrl if DEV_HUB_HOST is provided
25-
if (process.env.DEV_HUB_HOST) {
26-
oauthClient.axiosInstance.defaults.developerHubBaseUrl = `https://${process.env.DEV_HUB_HOST}`
27-
oauthClient.developerHubBaseUrl = `https://${process.env.DEV_HUB_HOST}`
28-
}
29-
3024
describe('OAuth Authentication API Test', () => {
3125
it('should login with credentials', done => {
3226
client.login({ email: process.env.EMAIL, password: process.env.PASSWORD }, { include_orgs: true, include_orgs_roles: true, include_stack_roles: true, include_user_settings: true }).then((response) => {

0 commit comments

Comments
 (0)