-
Notifications
You must be signed in to change notification settings - Fork 3.7k
feat(instagram): add Instagram integration #5568
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
Merged
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
41e0e64
feat(instagram): add Instagram Login OAuth, tools, and block
5fa0c84
feat(instagram): add Gmail-style media uploads for publish ops
5897592
refactor(instagram): simplify messaging tools to direct requests, cle…
363a68e
fix(instagram): parallelize carousel child polling, enforce 2-10 item…
e310f97
fix(instagram): resolve user id from user_id only, accept numeric use…
05f51a3
fix(instagram): use form/query params for publish and comment endpoin…
293230a
fix(instagram): normalize Graph ID outputs to strings so downstream .…
c3c4f1d
style(instagram): use brand gradient tile for the block icon
9236d5b
fix(instagram): tighten publish defaults and cloud-storage upload UX
f8c9516
fix(instagram): fail closed when cloud storage status is unknown
6128a18
fix(instagram): proactively refresh long-lived tokens before expiry
6720e40
Merge branch 'staging' into feature/instagram-integration
BillLeoutsakosvl346 7a6f33a
fix(oauth): restore TikTok clientIdParamName JSDoc after merge
89caa68
fix(api): Zod-contract storage-status and ratchet validation baseline
24bdce9
fix(instagram): match Gmail advanced media placeholders
b247bb1
Merge branch 'staging' into feature/instagram-integration
icecrasher321 5952e1d
Merge remote-tracking branch 'origin/staging' into feature/instagram-…
icecrasher321 3ba73dc
Merge remote-tracking branch 'origin/staging' into feature/instagram-…
icecrasher321 0fa622c
code review + hide from toolbar
icecrasher321 b2238e7
address comments
icecrasher321 30de5b1
fix(instagram): drop hidden Instagram from OAuth catalog pin test
952399a
fix(instagram): validate client ID before creating connect draft
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| import { createLogger } from '@sim/logger' | ||
| import { generateShortId } from '@sim/utils/id' | ||
| import { type NextRequest, NextResponse } from 'next/server' | ||
| import { authorizeInstagramContract } from '@/lib/api/contracts/oauth-connections' | ||
| import { parseRequest } from '@/lib/api/server' | ||
| import { getSession } from '@/lib/auth' | ||
| import { env } from '@/lib/core/config/env' | ||
| import { getBaseUrl } from '@/lib/core/utils/urls' | ||
| import { isSameOrigin } from '@/lib/core/utils/validation' | ||
| import { withRouteHandler } from '@/lib/core/utils/with-route-handler' | ||
| import { createConnectDraft } from '@/lib/credentials/connect-draft' | ||
| import { getCanonicalScopesForProvider } from '@/lib/oauth/utils' | ||
| import { checkWorkspaceAccess } from '@/lib/workspaces/permissions/utils' | ||
|
|
||
| const logger = createLogger('InstagramAuthorize') | ||
|
|
||
| export const dynamic = 'force-dynamic' | ||
|
|
||
| const INSTAGRAM_STATE_COOKIE = 'instagram_oauth_state' | ||
| const INSTAGRAM_RETURN_URL_COOKIE = 'instagram_return_url' | ||
| const INSTAGRAM_STATE_COOKIE_PATH = '/api/auth' | ||
| const INSTAGRAM_STATE_COOKIE_MAX_AGE_SECONDS = 60 * 10 | ||
|
|
||
| export const GET = withRouteHandler(async (request: NextRequest) => { | ||
| try { | ||
| const session = await getSession() | ||
| if (!session?.user?.id) { | ||
| return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }) | ||
| } | ||
|
|
||
| const clientId = env.INSTAGRAM_CLIENT_ID | ||
| if (!clientId) { | ||
| logger.error('INSTAGRAM_CLIENT_ID not configured') | ||
| return NextResponse.json({ error: 'Instagram client ID not configured' }, { status: 500 }) | ||
| } | ||
|
|
||
| const parsed = await parseRequest(authorizeInstagramContract, request, {}) | ||
| if (!parsed.success) return parsed.response | ||
| const { returnUrl, workspaceId } = parsed.data.query | ||
|
|
||
| if (workspaceId) { | ||
| const access = await checkWorkspaceAccess(workspaceId, session.user.id) | ||
| if (!access.canWrite) { | ||
| return NextResponse.json({ error: 'Workspace write access denied' }, { status: 403 }) | ||
| } | ||
| await createConnectDraft({ | ||
| userId: session.user.id, | ||
| workspaceId, | ||
| providerId: 'instagram', | ||
| }) | ||
| } | ||
|
|
||
| const baseUrl = getBaseUrl() | ||
| const state = generateShortId(32) | ||
| const redirectUri = `${baseUrl}/api/auth/oauth2/callback/instagram` | ||
| const scope = getCanonicalScopesForProvider('instagram').join(',') | ||
|
|
||
| const authUrl = new URL('https://www.instagram.com/oauth/authorize') | ||
| authUrl.searchParams.set('client_id', clientId) | ||
| authUrl.searchParams.set('redirect_uri', redirectUri) | ||
| authUrl.searchParams.set('response_type', 'code') | ||
| authUrl.searchParams.set('scope', scope) | ||
| authUrl.searchParams.set('state', state) | ||
|
|
||
| const response = NextResponse.redirect(authUrl.toString()) | ||
| response.cookies.set(INSTAGRAM_STATE_COOKIE, state, { | ||
| httpOnly: true, | ||
| secure: process.env.NODE_ENV === 'production', | ||
| sameSite: 'lax', | ||
| maxAge: INSTAGRAM_STATE_COOKIE_MAX_AGE_SECONDS, | ||
| path: INSTAGRAM_STATE_COOKIE_PATH, | ||
| }) | ||
|
|
||
| if (returnUrl && isSameOrigin(returnUrl)) { | ||
| response.cookies.set(INSTAGRAM_RETURN_URL_COOKIE, returnUrl, { | ||
| httpOnly: true, | ||
| secure: process.env.NODE_ENV === 'production', | ||
| sameSite: 'lax', | ||
| maxAge: INSTAGRAM_STATE_COOKIE_MAX_AGE_SECONDS, | ||
| path: INSTAGRAM_STATE_COOKIE_PATH, | ||
| }) | ||
| } | ||
|
|
||
| return response | ||
| } catch (error) { | ||
| logger.error('Error starting Instagram OAuth', { error }) | ||
| return NextResponse.json({ error: 'Failed to start Instagram OAuth' }, { status: 500 }) | ||
| } | ||
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.