Remix Auth Facebook Strategy

1.0.1 · active · verified Wed Apr 22

remix-auth-facebook provides a robust authentication strategy for integrating Facebook Login into Remix applications, built upon the `remix-auth` library. It enables developers to easily authenticate users using Facebook's OAuth 2.0 flow, handling token exchange and user profile retrieval. The current stable version is 1.0.1, indicating recent maintenance. The package follows a stable release cadence, with updates primarily focusing on minor fixes and improvements, as seen in the recent 1.0.1 release. Its key differentiator is its seamless integration with the Remix Auth ecosystem, offering a standardized approach for various authentication providers within Remix projects, supporting both Node.js and Cloudflare runtimes. It simplifies the setup required for external OAuth providers by abstracting the complex interactions with the Facebook API.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `remix-auth` with `FacebookStrategy`, define a user type, and configure the session storage using environment variables.

import { Authenticator } from 'remix-auth';
import { FacebookStrategy } from 'remix-auth-facebook';
import { createCookieSessionStorage } from '@remix-run/node';

// 1. Create a session storage (e.g., cookie based)
//    Ensure SESSION_SECRET is set in your .env
export const sessionStorage = createCookieSessionStorage({
  cookie: {
    name: '__session',
    httpOnly: true,
    path: '/',
    sameSite: 'lax',
    secrets: [process.env.SESSION_SECRET ?? 's3cr3t'],
    secure: process.env.NODE_ENV === 'production',
  },
});

// 2. Create an Authenticator instance
//    Replace `User` with your actual user type
export type User = { id: string; name: string; email: string; };
export const authenticator = new Authenticator<User>(sessionStorage);

// 3. Configure the Facebook Strategy
//    Ensure FACEBOOK_CLIENT_ID, FACEBOOK_CLIENT_SECRET, and FACEBOOK_CALLBACK_URL are set in your .env
authenticator.use(
  new FacebookStrategy(
    {
      clientID: process.env.FACEBOOK_CLIENT_ID ?? '',
      clientSecret: process.env.FACEBOOK_CLIENT_SECRET ?? '',
      callbackURL: process.env.FACEBOOK_CALLBACK_URL ?? 'http://localhost:3000/auth/facebook/callback',
    },
    async ({ profile }) => {
      // This is where you find or create the user in your database
      // using the profile data provided by Facebook.
      // For demonstration, we'll just return a mock user.
      return {
        id: profile.id,
        name: profile.displayName,
        email: profile.emails?.[0]?.value || `${profile.id}@example.com`,
      };
    }
  ),
  'facebook' // Name of the strategy
);

// Example routes (in app/routes/auth/facebook.tsx and app/routes/auth/facebook/callback.tsx)
// app/routes/auth/facebook.tsx (for initiating login)
// export async function loader({ request }: LoaderFunction) {
//   return authenticator.authenticate('facebook', request);
// }

// app/routes/auth/facebook/callback.tsx (for handling the callback)
// export async function loader({ request }: LoaderFunction) {
//   return authenticator.authenticate('facebook', request, {
//     successRedirect: '/dashboard',
//     failureRedirect: '/login',
//   });
// }

view raw JSON →