Skip to content

Commit accb51a

Browse files
author
waleed
committed
fixed hubspot scopes parsing
1 parent b8f97dc commit accb51a

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

apps/sim/app/api/auth/oauth/credentials/route.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,8 @@ export async function GET(request: NextRequest) {
210210
displayName = `${acc.accountId} (${baseProvider})`
211211
}
212212

213-
const grantedScopes = acc.scope ? acc.scope.split(/[\s,]+/).filter(Boolean) : []
213+
const storedScope = acc.scope?.trim()
214+
const grantedScopes = storedScope ? storedScope.split(/[\s,]+/).filter(Boolean) : []
214215
const scopeEvaluation = evaluateScopeCoverage(acc.providerId, grantedScopes)
215216

216217
return {

apps/sim/lib/auth.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -736,8 +736,6 @@ export const auth = betterAuth({
736736
tokenUrl: 'https://api.hubapi.com/oauth/v1/token',
737737
userInfoUrl: 'https://api.hubapi.com/oauth/v1/access-tokens',
738738
prompt: 'consent',
739-
responseType: 'code',
740-
overrideUserInfo: true,
741739
scopes: [
742740
'crm.objects.contacts.read',
743741
'crm.objects.contacts.write',
@@ -773,13 +771,29 @@ export const auth = betterAuth({
773771
)
774772

775773
if (!response.ok) {
774+
let errorBody: string | undefined
775+
try {
776+
errorBody = await response.text()
777+
} catch {
778+
// ignore
779+
}
776780
logger.error('Failed to fetch HubSpot user info', {
777781
status: response.status,
782+
statusText: response.statusText,
783+
body: errorBody?.slice(0, 500),
778784
})
779785
throw new Error('Failed to fetch user info')
780786
}
781787

782-
const data = await response.json()
788+
const rawText = await response.text()
789+
const data = JSON.parse(rawText)
790+
791+
const scopesArray = Array.isArray((data as any)?.scopes) ? (data as any).scopes : []
792+
if (Array.isArray(scopesArray) && scopesArray.length > 0) {
793+
tokens.scopes = scopesArray
794+
} else if (typeof (data as any)?.scope === 'string') {
795+
tokens.scopes = (data as any).scope.split(/\s+/).filter(Boolean)
796+
}
783797

784798
logger.info('HubSpot token metadata response:', {
785799
hasScopes: !!data.scopes,

0 commit comments

Comments
 (0)