Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(providers): Adding HubSpot Provider #4633

Merged
merged 16 commits into from Sep 11, 2022
Merged
2 changes: 1 addition & 1 deletion SECURITY.md
Expand Up @@ -15,7 +15,7 @@ If you contact us regarding a serious issue:

The best way to report an issue is by contacting us via email at info@balazsorban.com, yo@ndo.dev, thvu@hey.com and me@iaincollins.com, or raise a public issue requesting someone get in touch with you via whatever means you prefer for more details. (Please do not disclose sensitive details publicly at this stage.)

> For less serious issues (e.g. RFC compliance for unsupported flows or potential issues that may cause a problem in the future) it is appropriate to submit these these publically as bug reports or feature requests or to raise a question to open a discussion around them.
> For less serious issues (e.g. RFC compliance for unsupported flows or potential issues that may cause a problem in the future) it is appropriate to submit these publicly as bug reports or feature requests or to raise a question to open a discussion around them.
Miciurash marked this conversation as resolved.
Show resolved Hide resolved

## Supported Versions

Expand Down
43 changes: 43 additions & 0 deletions docs/docs/providers/hubspot.md
@@ -0,0 +1,43 @@
---
id: hubspot
title: HubSpot
---

:::note
HubSpot returns a limited amount of information on the token holder (see [docs](https://legacydocs.hubspot.com/docs/methods/oauth2/get-access-token-information)). One other issue is that the name and profile photo cannot be fetched through API as discussed [here](https://community.hubspot.com/t5/APIs-Integrations/Profile-photo-is-not-retrieved-with-User-API/m-p/325521).
:::

## Documentation

https://developers.hubspot.com/docs/api/oauth-quickstart-guide

## Configuration

You need to have an APP in your Developer Account as described at https://developers.hubspot.com/docs/api/developer-tools-overview

## Options

The **HubSpot Provider** comes with a set of default options:

- [HubSpot Provider options](https://github.com/nextauthjs/next-auth/blob/main/packages/next-auth/src/providers/hubspot.ts)

You can override any of the options to suit your own use case.

## Example

```js
import HubspotProvider from "next-auth/providers/hubspot";
...
providers: [
HubspotProvider({
clientId: process.env.HUBSPOT_CLIENT_ID,
clientSecret: process.env.HUBSPOT_CLIENT_SECRET
})
]
...
```

:::warning
The **Redirect URL** under the **Auth** tab on the HubSpot App Settings page must match the callback url which would be http://localhost:3000/api/auth/callback/hubspot for local development. Only one callback URL per Client ID and Client Secret pair is allowed, so it might be easier to create a new app for local development then fiddle with the url changes.
:::

79 changes: 79 additions & 0 deletions packages/next-auth/src/providers/hubspot.ts
@@ -0,0 +1,79 @@
import type { OAuthConfig, OAuthUserConfig } from "."

interface HubSpotProfile extends Record<string, any> {

// TODO: figure out additional fields, for now using
// https://legacydocs.hubspot.com/docs/methods/oauth2/get-access-token-information

user: string,
user_id: string,

hub_domain: string,
hub_id: string,
}


const HubSpotConfig = {
authorizationUrl: "https://app.hubspot.com/oauth/authorize",
tokenUrl: "https://api.hubapi.com/oauth/v1/token",
profileUrl: "https://api.hubapi.com/oauth/v1/access-tokens"
}

export default function HubSpot<P extends HubSpotProfile>(
options: OAuthUserConfig<P>
): OAuthConfig<P> {

return {
id: "hubspot",
name: "HubSpot",
type: "oauth",

...HubSpotConfig,

authorization: {
url: HubSpotConfig.authorizationUrl,
params: {
scope: "oauth",
client_id: options.clientId,
},

},
client: {
token_endpoint_auth_method: "client_secret_post",
},
token: HubSpotConfig.tokenUrl,
userinfo: {
url: HubSpotConfig.profileUrl,
async request(context) {

const url = `${HubSpotConfig.profileUrl}/${context.tokens.access_token}`;

const response = await fetch(url, {
headers: {
"Content-Type": "application/json",
},
method: "GET",
});

const userInfo = await response.json();

return { userInfo }
}
},
profile(profile) {

const { userInfo } = profile

return {
id: userInfo.user_id,
name: userInfo.user,
email: userInfo.user,

// TODO: get image from profile once it's available
// Details available https://community.hubspot.com/t5/APIs-Integrations/Profile-photo-is-not-retrieved-with-User-API/m-p/325521
image: null
}
},
options,
}
}