Skip to content

Commit

Permalink
feat: add types
Browse files Browse the repository at this point in the history
  • Loading branch information
guilherme-codes committed Dec 12, 2024
1 parent 2cab38f commit bc505c7
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 11 deletions.
1 change: 1 addition & 0 deletions playground/auth.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ declare module '#auth-utils' {
authentik?: string
seznam?: string
strava?: string
hubspot?: string
}

interface UserSession {
Expand Down
4 changes: 1 addition & 3 deletions playground/server/routes/auth/hubspot.get.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ export default defineOAuthHubspotEventHandler({
async onSuccess(event, { user }) {
await setUserSession(event, {
user: {
id: `${user.id}`,
email: `${user.email}`,
domain: `${user.domain}`,
hubspot: user.email,
},
loggedInAt: Date.now(),
})
Expand Down
66 changes: 58 additions & 8 deletions src/runtime/server/lib/oauth/hubspot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,60 @@ import {
import { useRuntimeConfig } from '#imports'
import type { OAuthConfig } from '#auth-utils'

export interface OAuthHubspotConfig {}
export interface OAuthHubspotUser {}
export interface OAuthHubspotConfig {
/**
* Hubspot OAuth Client ID
* @default process.env.NUXT_OAUTH_HUBSPOT_CLIENT_ID
*/
clientId?: string

/**
* Hubspot OAuth Client Secret
* @default process.env.NUXT_OAUTH_HUBSPOT_CLIENT_SECRET
*/
clientSecret?: string

/**
* Hubspot OAuth Redirect URL
* @default process.env.NUXT_OAUTH_HUBSPOT_REDIRECT_URL
*/
redirectURL?: string

/**
* Hubspot OAuth Scope
* @default ['oauth']
* @see https://developers.hubspot.com/beta-docs/guides/apps/authentication/scopes
* @example ['accounting', 'automation', 'actions']
*/
scope?: string[]
}
interface SignedAccessToken {
expiresAt: number
scopes: string
hubId: number
userId: number
appId: number
signature: string
scopeToScopeGroupPks?: string
newSignature?: string
hublet?: string
trialScopes?: string
trialScopeToScopeGroupPks?: string
isUserLevel: boolean
}

interface OAuthHubspotAccessInfo {
token: string
user: string
hub_domain: string
scopes: string[]
signed_access_token: SignedAccessToken
hub_id: number
app_id: number
expires_in: number
user_id: number
token_type: string
}

export function defineOAuthHubspotEventHandler({ config, onSuccess, onError }: OAuthConfig<OAuthHubspotConfig>) {
return eventHandler(async (event: H3Event) => {
Expand Down Expand Up @@ -51,19 +103,17 @@ export function defineOAuthHubspotEventHandler({ config, onSuccess, onError }: O
},
})

console.log('tokens', tokens)

if (tokens.error) {
return handleAccessTokenErrorResponse(event, 'hubspot', tokens, onError)
}

const profile: OAuthHubspotUser = await $fetch('https://api.hubapi.com/oauth/v1/access-tokens/' + tokens.access_token)
const info: OAuthHubspotAccessInfo = await $fetch('https://api.hubapi.com/oauth/v1/access-tokens/' + tokens.access_token)

return onSuccess(event, {
user: {
id: profile.user_id,
email: profile.user,
domain: profile.hub_domain,
id: info.user_id,
email: info.user,
domain: info.hub_domain,
},
tokens,
})
Expand Down

0 comments on commit bc505c7

Please sign in to comment.