UNPKG

better-auth

Version:

The most comprehensive authentication framework for TypeScript.

1 lines 3.79 kB
{"version":3,"file":"hubspot.mjs","names":[],"sources":["../../../../src/plugins/generic-oauth/providers/hubspot.ts"],"sourcesContent":["import type { OAuth2Tokens, OAuth2UserInfo } from \"@better-auth/core/oauth2\";\nimport { betterFetch } from \"@better-fetch/fetch\";\nimport type { BaseOAuthProviderOptions, GenericOAuthConfig } from \"../index\";\n\nexport interface HubSpotOptions extends BaseOAuthProviderOptions {\n\t/**\n\t * OAuth scopes to request.\n\t * @default [\"oauth\"]\n\t */\n\tscopes?: string[];\n}\n\n/**\n * HubSpot access token information response.\n * Based on: https://legacydocs.hubspot.com/docs/methods/oauth2/get-access-token-information\n *\n * The API may return additional fields, but we only use the fields needed for user identification.\n */\ninterface HubSpotProfile extends Record<string, any> {\n\tuser: string;\n\tuser_id: string;\n\thub_domain: string;\n\thub_id: string;\n\tsigned_access_token?: {\n\t\tuserId?: string;\n\t\t[key: string]: any;\n\t};\n}\n\n/**\n * HubSpot OAuth provider helper\n *\n * @example\n * ```ts\n * import { genericOAuth, hubspot } from \"better-auth/plugins/generic-oauth\";\n *\n * export const auth = betterAuth({\n * plugins: [\n * genericOAuth({\n * config: [\n * hubspot({\n * clientId: process.env.HUBSPOT_CLIENT_ID,\n * clientSecret: process.env.HUBSPOT_CLIENT_SECRET,\n * scopes: [\"oauth\", \"contacts\"],\n * }),\n * ],\n * }),\n * ],\n * });\n * ```\n */\nexport function hubspot(options: HubSpotOptions): GenericOAuthConfig {\n\tconst defaultScopes = [\"oauth\"];\n\n\tconst getUserInfo = async (\n\t\ttokens: OAuth2Tokens,\n\t): Promise<OAuth2UserInfo | null> => {\n\t\tconst tokenInfoUrl = `https://api.hubapi.com/oauth/v1/access-tokens/${tokens.accessToken}`;\n\n\t\tconst { data: profile, error } = await betterFetch<HubSpotProfile>(\n\t\t\ttokenInfoUrl,\n\t\t\t{\n\t\t\t\theaders: {\n\t\t\t\t\t\"Content-Type\": \"application/json\",\n\t\t\t\t},\n\t\t\t},\n\t\t);\n\n\t\tif (error || !profile) {\n\t\t\treturn null;\n\t\t}\n\n\t\t// Note: HubSpot's OAuth API only returns the email address in the 'user' field.\n\t\t// It does NOT provide a display name or profile picture. This is a known limitation.\n\t\t// See: https://community.hubspot.com/t5/APIs-Integrations/Profile-photo-is-not-retrieved-with-User-API/m-p/325521\n\t\tconst id = profile.user_id ?? profile.signed_access_token?.userId;\n\n\t\tif (!id) {\n\t\t\treturn null;\n\t\t}\n\n\t\treturn {\n\t\t\tid,\n\t\t\tname: profile.user,\n\t\t\temail: profile.user,\n\t\t\timage: undefined,\n\t\t\temailVerified: false,\n\t\t};\n\t};\n\n\treturn {\n\t\tproviderId: \"hubspot\",\n\t\tauthorizationUrl: \"https://app.hubspot.com/oauth/authorize\",\n\t\ttokenUrl: \"https://api.hubapi.com/oauth/v1/token\",\n\t\tclientId: options.clientId,\n\t\tclientSecret: options.clientSecret,\n\t\tscopes: options.scopes ?? defaultScopes,\n\t\tredirectURI: options.redirectURI,\n\t\tauthentication: \"post\",\n\t\tpkce: options.pkce,\n\t\tdisableImplicitSignUp: options.disableImplicitSignUp,\n\t\tdisableSignUp: options.disableSignUp,\n\t\toverrideUserInfo: options.overrideUserInfo,\n\t\tgetUserInfo,\n\t};\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAmDA,SAAgB,QAAQ,SAA6C;CACpE,MAAM,gBAAgB,CAAC,QAAQ;CAE/B,MAAM,cAAc,OACnB,WACoC;EAGpC,MAAM,EAAE,MAAM,SAAS,UAAU,MAAM,YAFlB,iDAAiD,OAAO,eAI5E,EACC,SAAS,EACR,gBAAgB,oBAChB,EACD,CACD;AAED,MAAI,SAAS,CAAC,QACb,QAAO;EAMR,MAAM,KAAK,QAAQ,WAAW,QAAQ,qBAAqB;AAE3D,MAAI,CAAC,GACJ,QAAO;AAGR,SAAO;GACN;GACA,MAAM,QAAQ;GACd,OAAO,QAAQ;GACf,OAAO;GACP,eAAe;GACf;;AAGF,QAAO;EACN,YAAY;EACZ,kBAAkB;EAClB,UAAU;EACV,UAAU,QAAQ;EAClB,cAAc,QAAQ;EACtB,QAAQ,QAAQ,UAAU;EAC1B,aAAa,QAAQ;EACrB,gBAAgB;EAChB,MAAM,QAAQ;EACd,uBAAuB,QAAQ;EAC/B,eAAe,QAAQ;EACvB,kBAAkB,QAAQ;EAC1B;EACA"}