Better Auth Instagram Provider

0.0.7 · active · verified Wed Apr 22

The `better-auth-instagram` package provides an Instagram OAuth provider specifically designed to integrate with the `better-auth` authentication framework. Currently at version 0.0.7, it functions as a plugin, allowing developers to easily add 'Login with Instagram' functionality to their TypeScript applications. It leverages `@better-fetch/fetch` for making robust API requests and `zod` for schema validation, aligning with the `better-auth` ecosystem's focus on type-safety and developer experience. While `better-auth` (currently at 1.4.0) offers a comprehensive, framework-agnostic authentication solution with a plugin-based architecture, `better-auth-instagram` simplifies the complexities of Instagram's OAuth flow, requiring proper setup of an Instagram Business Login API application. Its release cadence is tied to the `better-auth` ecosystem, focusing on incremental improvements and compatibility. Key differentiators include its tight integration with `better-auth`'s plugin system and built-in type safety with Zod.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the basic server-side setup of `better-auth` with the `better-auth-instagram` plugin and how to initiate the client-side OAuth flow.

import { betterAuth } from 'better-auth';
import { instagram } from 'better-auth-instagram';
import { createAuthClient } from 'better-auth/client';
import { genericOAuthClient } from 'better-auth/client/plugins';

// Server-side Better Auth instance configuration
export const auth = betterAuth({
  plugins: [
    instagram(), // Adds the Instagram OAuth provider
  ],
  // ... other better-auth configurations like database, secret, etc.
});

// Client-side Better Auth client instance configuration
// This should typically be in a separate client-side file
export const authClient = createAuthClient({
  plugins: [
    genericOAuthClient() // Required for OAuth client-side flows
  ]
});

// Example of initiating the sign-in flow on the client-side
async function signInWithInstagram() {
  if (typeof window !== 'undefined') {
    console.log('Initiating Instagram sign-in...');
    await authClient.signIn.oauth2({
      providerId: 'instagram',
      callbackURL: '/dashboard', // URL to redirect to after successful auth
      errorCallbackURL: '/login?error=instagram_failed'
    });
  }
}

// Call this function from a UI element, e.g., a button click
signInWithInstagram();

// In your .env file:
// INSTAGRAM_APP_ID="your_instagram_app_id"
// INSTAGRAM_APP_SECRET="your_instagram_app_secret"
// BETTER_AUTH_SECRET="super_secret_key_at_least_32_chars"
// BETTER_AUTH_URL="http://localhost:3000" // Base URL of your app for callbacks

view raw JSON →