facebook-auth-nestjs

raw JSON →
0.0.4 verified Sat Apr 25 auth: no javascript

A NestJS module for Facebook login authentication. Current version 0.0.4. This package provides a simple way to integrate Facebook OAuth2 login into NestJS applications, wrapping the Facebook Graph API to retrieve user information. It supports synchronous (forRoot) and asynchronous (forRootAsync) configuration, and the service exposes a getUser method. Key differentiators: it is specifically designed for NestJS (leveraging its module system), uses the official NestJS patterns (forRoot/forRootAsync), and is minimal with no extra dependencies beyond NestJS core and common HTTP modules. Alternatives like passport-facebook require more configuration and are framework-agnostic.

error Cannot find module 'facebook-auth-nestjs' or its corresponding type declarations.
cause The package may not be installed or TypeScript cannot resolve the types.
fix
Install the package: npm install facebook-auth-nestjs. Ensure no version mismatch.
error The 'getUser' method expects at least 2 string arguments for fields.
cause Calling getUser with an array or a single string like 'id,name' instead of separate arguments.
fix
Use multiple arguments: this.service.getUser(token, 'id', 'name')
error Invalid client credentials when calling getUser.
cause The clientId or clientSecret configured in forRoot is incorrect or not set.
fix
Verify your Facebook app credentials and ensure environment variables are loaded correctly.
gotcha The 'getUser' method requires the fields to be passed as separate string arguments (e.g., 'id', 'name'), not as an array or a single comma-separated string.
fix Use multiple string arguments: service.getUser(accessToken, 'id', 'name', 'email')
gotcha The package is for NestJS only; it cannot be used with plain Node.js or other frameworks.
fix Use a general-purpose Facebook authentication library like passport-facebook for non-NestJS projects.
gotcha User fields beyond 'id', 'name', 'first_name', 'last_name' require explicit Facebook login scope permissions set on the frontend (e.g., user_birthday for birthday field).
fix Request the necessary scopes on the frontend Facebook login call.
npm install facebook-auth-nestjs
yarn add facebook-auth-nestjs
pnpm add facebook-auth-nestjs

Registers the FacebookAuthModule with credentials using forRoot and demonstrates using the service to fetch a user's ID and name.

// In your AppModule
import { Module } from '@nestjs/common';
import { FacebookAuthModule } from 'facebook-auth-nestjs';

@Module({
  imports: [
    FacebookAuthModule.forRoot({
      clientId: process.env.FACEBOOK_CLIENT_ID ?? '',
      clientSecret: process.env.FACEBOOK_CLIENT_SECRET ?? '',
    }),
  ],
})
export class AppModule {}

// In a service
import { Injectable } from '@nestjs/common';
import { FacebookAuthService } from 'facebook-auth-nestjs';

@Injectable()
export class AuthService {
  constructor(private readonly facebookAuth: FacebookAuthService) {}

  async getUser(accessToken: string): Promise<{ id: string; name: string }> {
    return this.facebookAuth.getUser(accessToken, 'id', 'name');
  }
}