Skip to content

Commit

Permalink
fix(instagram): oauth provider
Browse files Browse the repository at this point in the history
  • Loading branch information
sandros94 authored Dec 9, 2024
1 parent 96363b2 commit 192e0e7
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/runtime/server/lib/oauth/instagram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,23 @@ export interface OAuthInstagramConfig {
clientSecret?: string
/**
* Instagram OAuth Scope
* @default [ 'user_profile' ]
* @required [ 'business_basic' ]
* @see https://developers.facebook.com/docs/instagram-basic-display-api/overview#permissions
* @example [ 'user_profile', 'user_media' ],
* @example [ 'business_basic', 'business_manage_messages' ],
*/
scope?: string[]
scope?: ('business_basic' | 'business_content_publish' | 'business_manage_comments' | 'business_manage_messages')[]

/**
* Instagram OAuth User Fields
* @default [ 'id', 'username'],
* @see https://developers.facebook.com/docs/instagram-basic-display-api/reference/user#fields
* @example [ 'id', 'username', 'account_type', 'media_count' ],
* @example [ 'id', 'username', 'user_id', 'account_type', 'profile_picture_url' ],
*/
fields?: string[]
fields?: ('id' | 'user_id' | 'username' | 'name' | 'account_type' | 'profile_picture_url' | 'followers_count' | 'follows_count' | 'media_count')[]

/**
* Instagram OAuth Authorization URL
* @default 'https://api.instagram.com/oauth/authorize'
* @default 'https://www.instagram.com/oauth/authorize'
*/
authorizationURL?: string

Expand Down Expand Up @@ -64,19 +64,28 @@ export function defineOAuthInstagramEventHandler({
}: OAuthConfig<OAuthInstagramConfig>) {
return eventHandler(async (event: H3Event) => {
config = defu(config, useRuntimeConfig(event).oauth?.instagram, {
scope: ['user_profile'],
authorizationURL: 'https://api.instagram.com/oauth/authorize',
scope: ['business_basic'],
authorizationURL: 'https://www.instagram.com/oauth/authorize',
tokenURL: 'https://api.instagram.com/oauth/access_token',
authorizationParams: {},
}) as OAuthInstagramConfig

const query = getQuery<{ code?: string, error?: string }>(event)
const query = getQuery<{
code?: string
error?: string
error_reason?: string
error_description?: string
}>(event)

if (query.error) {
const error = createError({
statusCode: 401,
message: `Instagram login failed: ${query.error || 'Unknown error'}`,
data: query,
data: {
error: query.error,
error_reason: query.error_reason,
error_description: query.error_description,
},
})
if (!onError) throw error
return onError(event, error)
Expand All @@ -96,7 +105,7 @@ export function defineOAuthInstagramEventHandler({
withQuery(config.authorizationURL as string, {
client_id: config.clientId,
redirect_uri: redirectURL,
scope: config.scope.join(' '),
scope: config.scope.join(),
response_type: 'code',
}),
)
Expand All @@ -120,10 +129,10 @@ export function defineOAuthInstagramEventHandler({
// TODO: improve typing

config.fields = config.fields || ['id', 'username']
const fields = config.fields.join(',')
const fields = config.fields.join()

const user = await $fetch(
`https://graph.instagram.com/v20.0/me?fields=${fields}&access_token=${accessToken}`,
`https://graph.instagram.com/v21.0/me?fields=${fields}&access_token=${accessToken}`,
)

if (!user) {
Expand Down

0 comments on commit 192e0e7

Please sign in to comment.