forked from nextauthjs/next-auth
/
hubspot.ts
104 lines (82 loc) · 2.73 KB
/
hubspot.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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 = {
clientId: process.env.HUBSPOT_CLIENT_ID ?? "DEFAULT_CLIENT_ID",
clientSecret: process.env.HUBSPOT_CLIENT_SECRET ?? "DEFAULT_CLIENT_SECRET",
redirectUri: process.env.HUBSPOT_REDIRECT_URI ?? "DEFAULT_REDIRECT_URI",
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> & { "redirectURL": string }
): OAuthConfig<P> {
return {
id: "hubspot",
name: "HubSpot",
type: "oauth",
...HubSpotConfig,
authorization: {
url: HubSpotConfig.authorizationUrl,
params: {
scope: "oauth contacts",
redirect_uri: HubSpotConfig.redirectUri,
client_id: HubSpotConfig.clientId,
},
},
token: {
url: HubSpotConfig.tokenUrl,
async request(context) {
const url = HubSpotConfig.tokenUrl + "?" +
new URLSearchParams({
client_id: HubSpotConfig.clientId,
client_secret: HubSpotConfig.clientSecret,
grant_type: "authorization_code",
redirect_uri: HubSpotConfig.redirectUri,
code: context.params.code ?? "DEFAULT_CODE",
});
const response = await fetch(url, {
headers: {
"Content-Type": "application/x-www-form-urlencoded",
},
method: "POST",
});
const tokens = await response.json()
return { tokens };
},
},
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: "https://avatars.hubspot.net/default-100",
}
},
options,
}
}