Instagram Provider
Configure Instagram OAuth for authentication in your Next.js application using NextAuth.js.
Documentation
https://developers.facebook.com/docs/instagram-basic-display-api/getting-started
Configuration
https://developers.facebook.com/apps/
Warning
Email address is not returned by the Instagram API. You will need to handle user identification without email.
Tip: HTTPS Required for Localhost
Instagram display app requires callback URL to be configured in your Facebook app and Facebook requires you to use HTTPS even for localhost! You need to either add an SSL to your localhost or use a proxy such as ngrok.
Instagram Basic Display API Setup
Follow these steps to configure Instagram OAuth:
Step 1: Create a Facebook App
- Go to Facebook Developers
- Click Create App
- Select Consumer or None (Build something else)
- Enter your app display name and contact email
- Click Create App
Step 2: Add Instagram Basic Display Product
- In your app dashboard, scroll down to find Instagram Basic Display
- Click Set Up
- Scroll down and click Create New App
- Enter a display name for your Instagram app
Step 3: Configure Basic Display Settings
- Navigate to Instagram Basic Display → Basic Display in the left sidebar
- Fill in the required URLs:
| Field | Value |
|---|---|
| Valid OAuth Redirect URIs | https://YOUR_DOMAIN/api/auth/callback/instagram |
| Deauthorize Callback URL | https://YOUR_DOMAIN/api/auth/callback/instagram/deauthorize |
| Data Deletion Request URL | https://YOUR_DOMAIN/api/auth/callback/instagram/delete |
Step 4: Setup for Local Development
Since Instagram requires HTTPS, use ngrok for local development:
# Install ngrok
npm install -g ngrok
# Start your Next.js app
npm run dev
# In another terminal, create an HTTPS tunnel
ngrok http 3000Use the HTTPS URL provided by ngrok (e.g., https://abc123.ngrok.io) as your redirect URI:
https://abc123.ngrok.io/api/auth/callback/instagramStep 5: Add Test Users
- Go to Roles → Instagram Testers
- Click Add Instagram Testers
- Enter the Instagram username of your test account
- The test user must accept the invitation in their Instagram app:
Settings → Apps and Websites → Tester Invites → Accept
Step 6: Get App ID and Secret
- Go to Instagram Basic Display → Basic Display
- Copy the Instagram App ID and Instagram App Secret
- Add them to your
.env.localfile:
# .env.local
INSTAGRAM_CLIENT_ID=your-instagram-app-id
INSTAGRAM_CLIENT_SECRET=your-instagram-app-secretOptions
The Instagram Provider comes with a set of default options. You can override any of the options to suit your own use case.
Example
// src/core/lib/auth.ts
import InstagramProvider from "next-auth/providers/instagram";
export const authOptions: NextAuthOptions = {
providers: [
InstagramProvider({
clientId: process.env.INSTAGRAM_CLIENT_ID!,
clientSecret: process.env.INSTAGRAM_CLIENT_SECRET!,
}),
],
};Usage in Component
// src/features/auth/components/social-login-buttons.tsx
"use client";
import { signIn } from "next-auth/react";
import { Button } from "@/components/ui/button";
export function InstagramLoginButton() {
return (
<Button
variant="outline"
onClick={() => signIn("instagram")}
>
Sign in with Instagram
</Button>
);
}Handling No Email
Since Instagram doesn't return email, you need to handle user identification differently:
// src/core/lib/auth.ts
export const authOptions: NextAuthOptions = {
providers: [
InstagramProvider({
clientId: process.env.INSTAGRAM_CLIENT_ID!,
clientSecret: process.env.INSTAGRAM_CLIENT_SECRET!,
}),
],
callbacks: {
async signIn({ account, profile }) {
if (account?.provider === "instagram") {
// Instagram doesn't provide email
// Use the Instagram user ID as unique identifier
return true;
}
return true;
},
async jwt({ token, account, profile }) {
if (account?.provider === "instagram") {
// Store Instagram-specific data
token.instagramId = account.providerAccountId;
token.username = (profile as { username?: string })?.username;
}
return token;
},
},
};Troubleshooting
Common Errors
| Error | Solution |
|---|---|
| Invalid redirect_uri | Ensure you're using HTTPS URL in Valid OAuth Redirect URIs |
| URL must use HTTPS | Use ngrok or similar tool to create HTTPS tunnel for localhost |
| User is not a tester | Add the Instagram account as a tester and accept the invitation |
| App not approved | Submit your app for App Review to allow non-testers to login |
| Missing product | Make sure Instagram Basic Display product is added to your Facebook app |
App Review for Production
To allow any Instagram user to log in (not just testers), you need to submit your app for review:
- Go to App Review → Permissions and Features
- Request the
instagram_graph_user_profilepermission - Prepare screenshots and a detailed description of your app
- Submit for review